How to run GitHub Actions Workflow only for new tags

Hello.

I have a Workflow that I only want to run when a new tag is pushed to the repository.

I have set the following “on” condition:

on:
  create:
    tags:

But my workflow is being executed also for branches.

What I am missing?

Thank you.

7 Likes

+1

I’ve the same issue. It seems the tag filtering is not considered.

on:
  create:
    tags:
      - v*
5 Likes

When I try to use on: I get a syntax error, e.g.

workflow "MyWorkflow" {
  on:
    create:
      tags:
        - v*
  resolves = ["MyAction"]
}

gives me 

unable to parse
Line 2: illegal char

This is in .github/main.workflow, right?

Ah so GitHub have changed the syntax to YML, I’ve used their migration tool to get it “working”. Well, for me this means that using the syntax above means that for every tag push, I get a failed build and a message saying No event triggers defined in on.

Hmmm, that’s super weird, somehow I got it working with the exact code from @provok .

At first it wasn’t working for me, then I tried the exact code from GitHub’s workflow on syntax and specified an exact tag v1. This worked, then I changed back to @provok’s code and it started working. Not sure what it was.

My steps now are: change some code, commit, push code. Change some new code, commit again and push. Then tag when I’m happy, push the tag. The latest commit will get the code build, but the first one will not, as expected.

Perhaps its because “create” also triggers when a branch is created, so it will run the filter on the first commit, then not until you actually create and push a new tag.

@brokge can you confirm that when you created the branch the workflow did not get triggered by the “create” action?

It seems now that I cant even run workflows for tags:

I have this:

on:
  push:
    tags:
       - '*'

But the workflow is not running at all when a new tag is pushed

3 Likes

@brpaz the code snippet you shared is for workflow
when a push happend on any tagged branch…

I don’t think it will work for "when a new tag is created "

you code is similar  to the sample code from here  onpushpull_requesttagsbranches

2 Likes

None of this is working.

This is triggered even when branches are created. The tags part is just ignored:

on:
  create:
    tags:
      - *

This will never be triggered:

on:
  push:
    tags:
      - *

This will never be triggered either:

on:
  push:
    branches:
      - 'refs/tags/*'

I’m creating tags with GitHub’s own “Create release” button.

Can anyone document this feature? 

In your second example, you need to quote the asterisk, that’s a YAML syntax requirement.  This should work:

on:
  push:
    tags:
    - '*'
26 Likes

Does the same apply to branches as well? Because this doesn’t work for me:

on:
  push:
    branches:
      - '*'

It does apply to branches, but if your branch might include / then you need to match on that too (for as many levels as you may have - */*, */*/*, */*/*/* etc.) e.g.:

on:
  push:
    branches:
      - '*'
- '*/*'

The proposed solutuon doesnt seem to work for me.

:

on:
  push:
    tags:
    - '*'
3 Likes

The proposed solution doesn’t seem to work for me too :confused:

I am sorry. It is indeed working for me. Thank you 

@artalat Use two asterisks, that’ll match sub-paths:

on:
  push:
    branches:
      - '**'
1 Like

Hi @ethomson this solution appears to trigger when pushing to a new branch.

eg. git push --set-upstream origin test will cause it to trigger. Subsequent pushes will not cause it to trigger. Is this intended? 

UPDATE:

This problem appears to have be fixed on the GitHub side?

where this needs to be updated.

I need to create trigger only when the tag is pushed/created in Github.any suggestion

The assembly by the tag does not work, then ci starts when creating the branch

Hi @ethomson 

What’s the recomended way to run a step (or even a job) based on a Git tag?

The esiest way I found out is as follows:

steps:
-name: Check out from Git
 uses: actions/checkout@v1

-name: Check if Git tag exists
 run: echo "::set-env name=HEAD_TAG::$(git tag --points-at HEAD)"

-name: Runs only if Git tag exists
 run: echo"This runs because Git tag points at HEAD ..."
 if: env.HEAD_TAG != '' 

Would be handy if I could do like this:

steps:
-name: Check out from Git
 uses: actions/checkout@v1

- name: Runs only if Git tag exists
    run: echo "This runs because Git tag points at HEAD ..."
    if:github.tag != '' 

Best Wishes,

  • Oleg
2 Likes