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

I’m facing an issue while using doing this:

- name: Fetch Tag Name
   id: tag_name
   run: echo ::set-output name=CI_COMMIT_TAG::${GITHUB_REF/refs\/tags\//}
 - name: "Deploy Produciton"
   if: (contains(steps.tag_name.outputs.CI_COMMIT_TAG, '_RC') || 
   contains(steps.tag_name.outputs.CI_COMMIT_TAG, '_PR')) != 'true'
   run: $something

this expression is not returning false. where CI_COMMIT_TAG is v0.0.8_PR

@mtilson, could you add a link to the thread you refer to, please?
(or am I missing something?)

@woter1832 It is this thread itself: conditional jobs and workflows
The question is in the first message of this thread


I have entire jobs and workflows that should only run if they meet a certain condition

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

I had to dig deep to the mentioned references to get the answer and just added the solution to this thread.

@jbmoelker, are you looking for this -

 build:
     if: github.event_name == 'push'
     runs-on: ubuntu-latest
  
     steps:
     - name: checkout code
       uses: actions/checkout@v2

@woter1832, I think @mtilson was talking about it