Writing multiline if conditions for steps in GitHub Actions
Published on May 5, 2023
You might end up with a really long if
condition for your GitHub Action's step, like so:
1 - name: Example2 if: ${{ github.ref == 'refs/heads/custom' && steps.Build.outcome == 'success' && steps.RetrieveBuildCache.outcome == 'success' && steps.RestoreCache.outputs.cache-hit }}3 run: echo "Hello world"
As it turns out, it's not so straightforward to break that condition into multiple lines. I kept running into weird "invalid syntax" errors while trying to format the code. Eventually, by trial-and-error, I figured out that the trick is to vertically align all conditions to the first one, like this:
1 - name: Example2 if: ${{3 github.ref == 'refs/heads/custom' &&4 steps.Build.outcome == 'success' &&5 steps.RetrieveBuildCache.outcome == 'success' &&6 steps.RestoreCache.outputs.cache-hit7 }}8 run: echo "Hello world"
Or this:
1 - name: Example2 if: ${{ github.ref == 'refs/heads/custom' &&3 steps.Build.outcome == 'success' &&4 steps.RetrieveBuildCache.outcome == 'success' &&5 steps.RestoreCache.outputs.cache-hit }}6 run: echo "Hello world"
As showcased above, you also need to either
- Vertically align the closing
$}}
token to the opening${{
token, or - Put the
$}}
token at the end of the last condition's line