in Technology by
How to use filters to target specific branches or tags for push events in GitHub Actions?

1 Answer

0 votes
by

When using the push event, you can configure a workflow to run on specific branches or tags.

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.

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

If you define only tags/tags-ignore or only branches/branches-ignore, the workflow won't run for events affecting the undefined Git ref. If you define neither tags/tags-ignore or branches/branches-ignore, the workflow will run for events affecting either branches or tags. If you define both branches/branches-ignore and paths/paths-ignore, the workflow will only run when both filters are satisfied.

The branches, branches-ignore, tags, and tags-ignore keywords accept glob patterns that use characters like *, **, +, ?, ! and others to match more than one branch or tag 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 and tags

The patterns defined in branches and tags are evaluated against the Git ref's name. For example, the following workflow would run whenever there is a push event to:

  • 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)
  • A tag named v2 (refs/tags/v2)
  • A tag whose name starts with v1., like v1.9.1 (refs/tags/v1.9.1)
on:
  push:
    # Sequence of patterns matched against refs/heads
    branches:    
      - main
      - 'mona/octocat'
      - 'releases/**'
    # Sequence of patterns matched against refs/tags
    tags:        
      - v2
      - v1.*

Example: Excluding branches and tags

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

  • A branch named mona/octocat (refs/heads/mona/octocat)
  • A branch whose name matches releases/**-alpha, like beta/3-alpha (refs/releases/beta/3-alpha)
  • A tag named v2 (refs/tags/v2)
  • A tag whose name starts with v1., like v1.9 (refs/tags/v1.9)
on:
  push:
    # Sequence of patterns matched against refs/heads
    branches-ignore:    
      - 'mona/octocat'
      - 'releases/**-alpha'
    # Sequence of patterns matched against refs/tags
    tags-ignore:        
      - v2
      - v1.*

Example: Including and excluding branches and tags

You can't use branches and branches-ignore to filter the same event in a single workflow. Similarly, you can't use tags and tags-ignore to filter the same event in a single workflow. If you want to both include and exclude branch or tag patterns for a single event, use the branches or tags filter along with the ! character to indicate which branches or tags 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. Similarly, if you define a tag with the ! character, you must also define at least one tag without the ! character. If you only want to exclude tags, use tags-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 pushes to releases/10 or releases/beta/mona, but not on releases/10-alpha or releases/beta/3-alpha because the negative pattern !releases/**-alpha follows the positive pattern.

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

Related questions

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
    How to use filters to target specific branches for pull request events in Github Actions?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    What is the use of GitHub Actions?...
asked Apr 1, 2023 in Technology by JackTerrance
0 votes
    How to automatically trigger GitHub Actions workflows?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    How do we create a workflow to automatically trigger GitHub Actions jobs?...
asked Apr 1, 2023 in Technology by JackTerrance
0 votes
    How do you push a file from your local system to the GitHub repository using Git?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    What is CODEOWNERS feature in GitHub Action?...
asked Apr 1, 2023 in Technology by JackTerrance
0 votes
    What are GitHub Action workflow templates?...
asked Apr 1, 2023 in Technology by JackTerrance
0 votes
    For discovering the OS running on the target system, the scanning has a specific term. What is it? (a) ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 2, 2021 in Education by JackTerrance
0 votes
    How to use events activity types in github Actions?...
asked Jun 19, 2023 in Technology by JackTerrance
0 votes
    How to use Push command in Git?...
asked Aug 24, 2021 in Technology by JackTerrance
0 votes
    How you can help to reduce the risk of script injection?...
asked Apr 1, 2023 in Technology by JackTerrance
0 votes
    Can we use different filters in different pages?...
asked Nov 21, 2020 in Technology by JackTerrance
...