Run workflow on push tag on specific branch

Hello,

How can I configure on push tag but only on the specific branch?

My current config file:

on:
  push:
    branches:
      - master
    tags:
      - 0.*
      - 1.*

But it runs on push on the master branch and on push tags.

3 Likes

There is not a way to run workflow on push tag on specfic branch.

As a workaround, you could remove branch filter settings just left tags filter. Then run a job with if condition to filter branch name . 

if: github.event.base_ref == 'refs/heads/master'

Please see my example: 

On push tags.png

When push tag on other branches, you could see the job is skipped.

3 Likes

This would have been better as a text example instead of a screenshot. Using screenshots like this makes it impossible to find topics containing the keywords used in the actual configurations. 

1 Like

@techdragon  Thank you for pointing this problem out. In the previous comment , I only show if conditional line in text not the whole workflow yml. 

I will keep it in mind. Now let me add the example again in code. 

name: Push
on:
  push:
    tags:
      - 0.*
      - 1.*
jobs:
   build:
     if: github.event.base_ref == 'refs/heads/master'
     runs-on: ubuntu-latest
     steps:
     - name: Dump GitHub context
       env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
       run: echo "$GITHUB_CONTEXT"
6 Likes

This doesn’t work. github.event.base_ref is null when the workflow trigger is by tag.

name: Deployments CI/CD
on:
  push:
    tags:
      - deploy/deployments/*
jobs:
  CICD:
    #if: github.event.base_ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

Output of echo "$GITHUB_CONTEXT"

{
 // deleted other properties
  "event": {
    "base_ref": null,
  }
}

I looked at every event property, there’s nothing that says “this event is for the master branch”.