-2

Essentially this is a question about best practice in Terraform YAML documents, and is probably only about formatting conventions, but I also want to check about GitHub and bash behaviour.

I've encountered a bash script inside a YAML document that's run as part of a GitHub Actions workflow, and the block is introduced with a "chomp" modifier:

- name: Terraform Apply
  run: |-
    TAGS=${{needs.tags.outputs.NEW_TAG_VERSION}}
    export SOMEVAR='hello'
    export ANOTHERVAR='etc'
    ...
    aws s3 crucial operation

I'm somewhat new to this application area for YAML, and the particular GitHub implementation, so I want to confirm that given my expected treatment of the bash script by GitHub, whether there are any genuine reasons why a "chomp" indicator is necessary or useful and whether there are any risks in omitting it:

  1. Using the "chomp" operator will strip the final newline from the script
  2. Using the "chomp" operator will not strip intervening newlines from the script
  3. The script will therefore run fine, including the last command
  4. That is, the last command runs even though there's no newline there, because it's run as a bash script, not as a sequence of typed-in commands
  5. However, there's no need to strip the last newline, the script will run with a trailing newline just as well
  6. But in fact, having a newline at the end of your script is the normal expectation and fits canonical approaches more uniformly

So if this is all true, there's no reason at all to add the chomp modifier, and following normal expectations it's marginally better without it, and 1 less character to boot, and one less thing to think about. So the script should rather be introduced with run: |, not run: |-.

If not, what does the "chomp" modifier achieve for this use-case?

NeilG
  • 3,886
  • 2
  • 22
  • 30
  • 1
    Basic YAML multiline is covered in e.g. https://stackoverflow.com/q/3790454/3001761. For the specific case you can find out if it _needs_ one pretty easily. As to "better" that's a matter of opinion. – jonrsharpe Aug 21 '23 at 14:45
  • As far as I can see, @jonrsharpe, the question you reference has no information relevant to my question. I think it's clear from my question that I understand how the chomp indicator affects the parsing of the content, my question arises from uncertainty about `bash` scripting in the GitHub workflow environment. This is not a question about YAML syntax. – NeilG Aug 21 '23 at 15:09
  • As for "better", again from my question, @jonrsharpe, I have specified the criteria for "better" in the question as because it's the normal expectation, because that is less distracting (less things to think about) and because it's shorter. This again is part of the question because I have a concern about how the script is processed through `bash` by GitHub with this specific YAML. In short "better" is clearer, less ambiguous, more conventional, and ultimately, more reliable therefore. There's no opinion involved. – NeilG Aug 21 '23 at 15:11
  • "I just want to confirm what I think I know:" - did you try *giving the document to a YAML parser and seeing if you get the result you expect*? "I think it's clear from my question that I understand how the chomp indicator affects the parsing of the content" - and yet, at least the first two of the things you "just want to confirm", apparently, are exactly expressing doubt about how it works. – Karl Knechtel Aug 21 '23 at 19:01
  • 1
    "Would "please confirm if you provided the xxxx-xxxx and why so I can improve" match the regex," Probably. To my understanding, it's based on looking for words like "please" along with considering the total length of the comment. However, *this is not the place* to discuss that - please take it to Meta. – Karl Knechtel Aug 21 '23 at 19:05
  • @KarlKnechtel, I've just read my question again because you are the second person who thinks I'm asking about how YAML is parsed. Points 1 and 2 are to establish the behaviour and further points go on to indicate why that's relevant. Instead of stopping half way through the question please go on to read points 3, 4, 5 and 6 which go on to establish expectations about how `bash` on GitHub performs this script, and ultimately the question about the chomp indicator being superfluous. If you want to go around reading half of a question you're only going to draw half-assed conclusions. – NeilG Aug 21 '23 at 23:34
  • @KevinB, I think you're losing track. The comments that were deleted were mine. I wish people would delete or correct their own comments once they realise they are incorrect, but there's no such enlightened behaviour here. I see I've kicked up a hornet's nest. – NeilG Aug 21 '23 at 23:36
  • 1
    Unrelated to chomping, but IMHO there should be no bash scripts in yaml, only one-liners or no more than a single command at most. It gets unwieldy to edit the bash in there, no syntax highlighting, no shellcheck. Move code out of markup as much as possible. – Robert Aug 21 '23 at 23:59
  • That makes sense to me @Robert but the alternatives seem worse? As I said I'm unfamiliar with the patterns and conventions of this environment but a [quick search](https://askubuntu.com/questions/992448/how-to-execute-a-bash-script-from-github) seems to suggest you have to make a web request to retrieve the script otherwise!? Or can you just put it in the `workflows` directory and call `./github/workflows/script.sh` ? – NeilG Aug 22 '23 at 00:23
  • 1
    I'd try that... my experience is with CircleCI and Gitlab. I'd assume the scripts and the yaml are checked in anyway, and at the begin of any build the whole repo gets cloned. If not, I'd file a feature request :-) – Robert Aug 22 '23 at 01:18
  • @NeilG i purposely used "the user" to refer to whoever the comment belonged to, not specifically theirs or yours. but also, we can't edit comments after a very short period of time anyway. – Kevin B Aug 22 '23 at 01:58

0 Answers0