conditional jobs and workflows

GitHub Actions v2 currently support conditional steps (jobs.<job_id>.steps.if). However I have entire jobs and workflows that should only run if they meet a certain condition. Is there a way to do achieve this?

Currently I use the same condition:

if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'

on each step:

on:
  check_suite:
    types: [completed]

jobs:
  audit:
    runs-on: ubuntu-latest
    steps:

      - name: Git checkout
        if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'
        uses: actions/checkout@master

      - name: Install Node
        if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'
        uses: actions/setup-node@v1
        with:
          node-version: 10.x

      - name: Install npm dependencies
        if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'
        run: npm install

      - name: Run Audit
        if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'
        uses: ./.github/actions/run-audit

This means this workflow runs on every completed check suite even if it’s not meeting the condition. It simply skips all the steps. But it does run and creates a lot of clutter in the list of checks on a PR.

Ideally I would be able to do this for the entire workflow:

on:
  check_suite:
    types: [completed]

if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'

jobs:
...

Or for an entire job:

on:
  check_suite:
    types: [completed]

jobs:
  audit:
    if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'

Is something like this possible or is there another way to achieve this?

10 Likes

Found a relatic thread: https://github.community/t5/GitHub-Actions/Github-Actions-CI-CD-Need-optional-jobs/m-p/29696

How about using success()?

success() - Job status check functions

- name: Git checkout
        if: github.event.check_suite.app.name == 'Netlify' && github.event.check_suite.conclusion == 'success'
        uses: actions/checkout@master

      - name: Install Node
        if: success()
        uses: actions/setup-node@v1
        with:
          node-version: 10.x

      - name: Install npm dependencies
        if: success()
        run: npm install

      - name: Run Audit
        if: success()
        uses: ./.github/actions/run-audit
2 Likes

Thanks, that does make the repetitive conditional statement more readable. But the workflow and job are still triggered and still pollute the commit check list everywhere. So it doesn’t solve my actual problem.

Also having a similar issue. Our Continuous Deployment should only be triggered whenever a pull request was merged. I now have this condition on every step of the deployment process:

if: github.event.pull_request.merged

This works as intended, but it would be very nice if I could set this condition on the entire job or even the workflow.

1 Like

It is possible now with jobs.<job_id>.if - just to make it clear here in this thread

3 Likes