github actions exclude pull requests from a branch
Asked Answered
F

3

8

Say I have a workflow that runs on every PR to master which starts with:

on:
  pull_request:
    branches:
      - master

I'd like to skip all jobs if the PR is coming from the depbot branch. Something like:

on:
  pull_request:
    branches:
      - master
    head_ref-ignore:
      - depbot

I think you can skip all the steps (one at a time) using

 if: startsWith(github.head_ref, 'depbot') == false

But it is not what I want, as it would still initiate the job. How can I achieve that at the launch level?

Fiducial answered 15/7, 2021 at 5:38 Comment(0)
S
2

But it is not what I want, as it would still initiate the job.

That means you would need a "gatekeeper" job which would be initiated (and check github.head_ref), and, through job dependency, would call the second one only if the right condition is fulfilled.

But the point is: you need at least one job to start, in order to check a condition.

Swob answered 15/7, 2021 at 6:53 Comment(0)
M
2

According to the documentation, you can't achieve want you want at the workflow level, as it's based on base branches.

Therefore, those implementations below won't work.

on:
  pull_request:
    branches:    
      - 'master'    # matches refs/heads/master
      - '!depbot'   # excludes refs/heads/depbot

Or

on:
  pull_request:
    branches-ignore:    
      - 'depbot'   # ignore refs/heads/depbot

EDITED ANSWER

Workaround

A solution could be to use a setup job checking the github.head_ref context variable, and setting an output that would be used in an expression to run the following jobs if the condition is fulfilled.

Something like this:

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      condition: ${{ steps.condition.outputs.condition }}
    steps:
      - id: condition
        run: |
          if [[ ${{ github.head_ref }} == *"depbot"* ]]; then
             echo "condition=true" >> $GITHUB_OUTPUT
          else
             echo "condition=false" >> $GITHUB_OUTPUT  
          fi

  other-job:
    needs: [setup]
    runs-on: ubuntu-latest
    if: ${{ needs.setup.outputs.condition }} == 'true'
    steps:
       [ ... ]

The thing is, this workflow will always trigger, it will just not perform the desired operations if the condition isn't fulfilled.

Matabele answered 15/7, 2021 at 13:13 Comment(1)
This applies to the base branches specified. I was looking for a way to ignore PRs 'from' specific head branches.Fiducial
G
0

Bit late to the party, but you can add [skip ci] to the start of dependabot commit messages to stop them from triggering workflows. https://docs.github.com/en/actions/managing-workflow-runs/skipping-workflow-runs

Goingover answered 8/5 at 8:36 Comment(1)
Note this can appear anywhere in the commit message, not just at the beginning of it.Moltke

© 2022 - 2024 — McMap. All rights reserved.