Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 5

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?

 

4 Replies
Copilot Lvl 3
Message 2 of 5

Re: conditional jobs and workflows

Copilot Lvl 3
Message 3 of 5

Re: conditional jobs and workflows

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

 

Copilot Lvl 3
Message 4 of 5

Re: conditional jobs and workflows

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.

Pilot Lvl 2
Message 5 of 5

Re: conditional jobs and workflows

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.