9

I have the following in my Github Actions YAML. It runs fine when the first check evaluates true,

    - name: Check version is up to date
      run: sh -c "
        (git diff --quiet HEAD^ VERSION && git diff --quiet HEAD^ src/**) || \
          ! git diff --quiet HEAD^ src/VERSION
        "

but when it evaluates false, the later sections gives me

sh: 1:  !: not found

only in GitHub Actions. The command starting (and including) sh -c doesn't error on my computer. I don't understand how that could be since I very explicitly mention the shell.

14
  • @Ferrybig thanks, I'm familiar with bash vs sh, but is ! bash? And if so, why does it work on my machine even with sh -c? Commented Nov 13, 2024 at 11:02
  • 2
    GitHub Actions: Split Long Command into Multiple Lines Commented Nov 13, 2024 at 12:16
  • 2
    This question is similar to: How do I break a string in YAML over multiple lines?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Context: As explained in this answer you're using the plain style which doesn't preserve newlines, hence a backslash at the end of your line doesn't work as a line continuation character. Commented Nov 14, 2024 at 14:35
  • 1
    I understand the logic behind closing this as a duplicate, but I don't agree with it. stackoverflow.com/questions/3790454/… may offer many alternative syntaxes that work to achieve what the OP wanted, but without the explanation in @AbdulAzizBarkat's comment, a reader is going to be none the wiser about what was actually wrong with the syntax that the OP used - i.e. how the backslash gets interpreted and how that ultimately leads to the error the OP saw. That explanation belongs in an answer, IMO, not a dupe closure comment. Commented Nov 15, 2024 at 11:54
  • 1
    @philipxy That boilerplate comment gives no hint of what aspect of an MRE you think is missing (and includes some stuff that is nonsensical about variables; there are none here). For reasons I've remarked before (see my answer at meta.stackoverflow.com/q/367798/1709587), I find such boilerplate MRE comments unhelpful and obnoxious. If you think the code is non-minimal, say that; if not enough code is given to repro the error, say that; if the expected and actual behavior aren't clear, say that. But none of those things obviously applies, and you don't say which you mean. Commented Nov 15, 2024 at 12:06

1 Answer 1

15

I think this is a formatting error/quirk. If I add a new line before the command, with the | syntax, as

      run: |
        sh -c "
        (git diff --quiet HEAD^ VERSION && git diff --quiet HEAD^ src/**) || \
          ! git diff --quiet HEAD^ src/VERSION
        "

it works.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, the original YAML results in || \ ! git causing the backslash to escape the space causing the shell to look for an executable that has a space as the first character and ! as the second. The syntax used by the answer preserves the newline causing the backslash to be used for line continuation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.