Post-job cleanup

If our current action follows a simple

jobs:
  build:
    steps:
    -name: step1
    -name: step2
    -name: cleanupStep

How to best ensure cleanupStep always executes (even if step1 or step2 fail)? post seems promising, but I’m struggling to find any workflow examples using it, and documentation isn’t making it easy to understand how to plug it into the existing structure above.

How about adding an if: ${{ always() }} condition to the cleanup step?

1 Like

Thanks, that seems to work!

Hmm, but what if I need an additional condition applied (for example, if there are different cleanup steps, based on the runner OS)?

if: runner.os == 'Linux'

doesn’t seem to execute, if previous steps failed – is there a way to combine always() with other conditions?

And, by the same token, if: ${{ always() }} && runner.os == 'Linux' doesn’t seem to take the other condition into account, and always executes.

if: ${{ always() && runner.os == 'Linux' }} should work, with the whole condition marked for evaluation.

Awesome, many thanks!

1 Like

You can leave out ${{}} in case of if, which makes things easier:

if: always() && runner.os == 'Linux'