bashgrepcontinuous-integrationgithub-actions

Github Actions: Why an intermediate command failure in shell script would cause the whole step to fail?


I have a step in a Github Actions job:

      - name: Check for changes
        run: |
          diff=$( git diff --name-only 'origin/main' )
          changed_files=$( echo $diff | grep -c src/my_folder ) # this fails

          # more script lines
          # that are unrelated

This fails with Error: Process completed with exit code 1. only if grep finds nothing. If there are matches in $diff, then this step works as intended. But of course it also needs to work without matches.

I can run this locally or inside a script without a problem, exit code is always 0 (on a Mac).

I fail to understand what the problem is. After some hours of trial and error and research I learned that apparently grep is tricky in Github actions, but I found no hint or proper documentation how I am supposed to solve this exact case.

If I change my failing line to

echo $( echo $diff | grep -c src/my_folder ) # this works and prints out the result

this gets executed without problems.

But how do I get my grep output into my variable even when there are no findings?


Solution

  • According to the doc, by default Github Actions enables set -e to run the step's commands. This is why an intermediate command failure may cause the whole step to fail. To take full control of your step's commands you can do like this:

      - name: Check for changes
        shell: bash {0}
        #^^^^^^^^^^^^^^
        run: |
          diff=$( git diff --name-only 'origin/main' )
          changed_files=$( echo $diff | grep -c src/my_folder )
    
          # ...
          # ...
    

    Or you can just disable the default set -e at the beginning of the step's script:

      - name: Check for changes
        run: |
          set +e  # revert the default `set -e` 
    
          diff=$( git diff --name-only 'origin/main' )
          changed_files=$( echo $diff | grep -c src/my_folder )
    
          # ...
          # ...