in Technology by
How to use filters to target specific branches for pull request events in Github Actions?

1 Answer

0 votes
by

When using the pull_request and pull_request_target events, you can configure a workflow to run only for pull requests that target specific branches.

Use the branches filter when you want to include branch name patterns or when you want to both include and exclude branch names patterns. Use the branches-ignore filter when you only want to exclude branch name patterns. You cannot use both the branches and branches-ignore filters for the same event in a workflow.

If you define both branches/branches-ignore and paths/paths-ignore, the workflow will only run when both filters are satisfied.

The branches and branches-ignore keywords accept glob patterns that use characters like *, **, +, ?, ! and others to match more than one branch name. If a name contains any of these characters and you want a literal match, you need to escape each of these special characters with \.

Example: Including branches

The patterns defined in branches are evaluated against the Git ref's name. For example, the following workflow would run whenever there is a pull_request event for a pull request targeting:

  • A branch named main (refs/heads/main)
  • A branch named mona/octocat (refs/heads/mona/octocat)
  • A branch whose name starts with releases/, like releases/10 (refs/heads/releases/10)
on:
  pull_request:
    # Sequence of patterns matched against refs/heads
    branches:    
      - main
      - 'mona/octocat'
      - 'releases/**'

Example: Excluding branches

When a pattern matches the branches-ignore pattern, the workflow will not run. The patterns defined in branches are evaluated against the Git ref's name. For example, the following workflow would run whenever there is a pull_request event unless the pull request is targeting:

  • A branch named mona/octocat (refs/heads/mona/octocat)
  • A branch whose name matches releases/**-alpha, like releases/beta/3-alpha (refs/heads/releases/beta/3-alpha)
on:
  pull_request:
    # Sequence of patterns matched against refs/heads
    branches-ignore:    
      - 'mona/octocat'
      - 'releases/**-alpha'

Example: Including and excluding branches

You cannot use branches and branches-ignore to filter the same event in a single workflow. If you want to both include and exclude branch patterns for a single event, use the branches filter along with the ! character to indicate which branches should be excluded.

If you define a branch with the ! character, you must also define at least one branch without the ! character. If you only want to exclude branches, use branches-ignore instead.

The order that you define patterns matters.

  • A matching negative pattern (prefixed with !) after a positive match will exclude the Git ref.
  • A matching positive pattern after a negative match will include the Git ref again.

The following workflow will run on pull_request events for pull requests that target releases/10 or releases/beta/mona, but not for pull requests that target releases/10-alpha or releases/beta/3-alpha because the negative pattern !releases/**-alpha follows the positive pattern.

on:
  pull_request:
    branches:    
      - 'releases/**'
      - '!releases/**-alpha'

Related questions

0 votes
    How to use filters to target specific branches or tags for push events in GitHub Actions?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    How to use events activity types in github Actions?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    Merge the Pull request in GitHub for the updates made to starfeature branch. Which channel would receive notification ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 23, 2022 in Technology by JackTerrance
0 votes
    Merge the Pull request in GitHub for the updates made to starfeature branch. Which channel would receive notification ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 16, 2022 in Education by JackTerrance
0 votes
    How to use filters to target specific paths for pull request or push events?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    Create a new Pull request. Review the changes between master and starfeature branch, and confirm by clicking Create ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 23, 2022 in Technology by JackTerrance
0 votes
    I have created a branch named dev. I have done a pull request to send dev code to master, when I do ... to resolve these conflicts. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
0 votes
    My git workspace is dirty, there are some local modifications. When I use the command git pull origin master it ... Ansible git module? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    @RequestMapping annotation is used to map a HTTP request method (GET or POST) to a specific class or method in the ... which will handle the respective request. A. True B. False...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    Can we use different filters in different pages?...
asked Nov 21, 2020 in Technology by JackTerrance
0 votes
    Can we use non-used columns (columns that are not used in reports but used in data source) in Tableau Filters?...
asked Oct 29, 2020 in Technology by JackTerrance
0 votes
    Snapchat filters use _____ and _____ to enhance your selfie with flowers, cat ears etc. a) machine learning ... and computer vision Select the correct answer from above options...
asked Nov 12, 2021 in Education by JackTerrance
...