Run a cleanup job after all jobs finished (either failed or not)

Consider a job with two tests, one failing one succeeding:

name: misc
on: [push]
jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - shell: bash
        run: |
          echo "hello world"
  test:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - shell: bash
        run: |
          exit 0
  fail:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - shell: bash
        run: |
          exit 1
  cleanup:
    runs-on: ubuntu-latest
    needs: [test, fail]
if: always()
    steps:
      - name: Cleanup
        run: echo "cleanup needed"

How do I ensure cleanup job always runs? I understand always() only works on step level. I guess I could do a mutex after each step, but that’s mad lot of yaml. I could use continue-on-error, but that means the test will have green checkmark which is not really wanted…

Edit: Updated with valid syntax… Delete post if needed.

2 Likes

@sjurgis ,

The status check function “always()” is now available to job-level. I just have tested and it can work fine.

So, you can feel free to use “always()” in the if conditional on job-level, like you have posted on your above comment.

cleanup:
    runs-on: ubuntu-latest
    needs: [test, fail]
    if: always()
    steps:
      - name: Cleanup
        run: echo "cleanup needed"

I’ve had the same need and I found this GitHub Action that do exactly what you want: https://github.com/technote-space/workflow-conclusion-action

:tada: