How to restrict execution of GitHub Actions workflow on tags creation only

Hey guys!

Been using GitHub Actions v2 (CI/CD) to implement publishing of npm packages when a new tag is pushed to our GitHub repo. I have looked into https://help.github.com/en/articles/workflow-syntax-for-github-actions#example-restricting-the-workflow-run-to-specific-refs-and-paths but can’t get it to work. Here’s a snippet of my workflow file:

on:
  push:
    tags:
    - my-tag
...

Problem is when I push a new commit even with no tags it still gets triggered. I also tried using the create event but still no dice, when a new branch is created it still triggers the workflow.

Am I doing something wrong with filters? Would you have an alternative perhaps on how to trigger a workflow only when a new tag is created?

Also, if I want to filter tags that have a @ sign, is that the way to do it?

on:
  push:
    tags:
    - \@orgname/*

Which should mean "Only run on tags that start with @orgname/".

Thanks!

2 Likes

I was just running into this. I solved just using a full ref path: 

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

So I’m pretty sure you can then restrict to scopes like this: 

on:
  push:
    branches:
      - refs/tags/@orgname/*

I basically wanted to wildcard any tag push only, but this is the only way I could get it to work right.

3 Likes

Should it be with tags instead of branches?

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

Will give this a try, thanks!

1 Like

Tags is just shorthand for refs/tags so branches: refs/tags/* is the equivalent and will do exactly what you wanted. I’ve been testing it for a bit this afternoon and it’s working.

My guess is tags: refs/tags/* might try to do something like refs/tags/refs/tags but not entirely sure. It might work. :shrug:

2 Likes
on:
  push:
    branches:
      - refs/tags/@orgname/*

Worked like a charm, thanks a lot  @pilotconway !

2 Likes

I just ran into this myself - seems like a bug in Actions itself? The branches workaround seems fine for now but not properly descriptive IMO.

3 Likes

I agree, I have a couple work flows I really want to do something like, on _any_ tag push or these specific branches, but the docs don’t have a good way to mix and match like that either. I’m not sure if its not enough documentation or a lack of understanding on yaml on my part. 

3 Likes

In fact it seems it works half and half, as in:

on:
  push:
    branches:
      - refs/tags/@orgname/*

will only trigger when new tag is pushed,  but , if I push a tag named _test-tag _for example it will still trigger the workflow. What I’m wondering is if this “filter” looks at all  existing  tags in refs/ and thus will match if you already have any “@orgname/*” tag present.

Still confused by this whole workflow execution restriction thing, but hey its still beta :wink:

1 Like

This is a bug that should be fixed this week. It should only trigger when a tag is pushed.

2 Likes

That are wonderful news. Thanks a lot!

1 Like

did you manage to fix this error ?

This is still an issue for me, using the following syntax:

on:
  push:
    branches:
      - master
      - refs/tags/*
jobs:
  build:

and I also tried the other suggestions. Doesn’t seem to work. I would like to build only for commits that are tagged. 

1 Like

Around here I can see the correct behavior (only execute only when the tag is pushed) when I fork the repo to my personal account, but it is triggered for every push on the organization’s repo.

This doesn’t work. It runs on every single commit regardless of whether the commit was tagged. Double-quotes, single-quotes - doesn’t matter.

on:
push:
branches:
- main
tags:
- ‘v*..