Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 10

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

Solved! Go to Solution.

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-workf... 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!

9 Replies
Solution
Copilot Lvl 3
Message 2 of 10

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

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.

Copilot Lvl 3
Message 3 of 10

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

Should it be with `tags` instead of `branches`?

 

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

Will give this a try, thanks!

Copilot Lvl 3
Message 4 of 10

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

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:

Copilot Lvl 3
Message 5 of 10

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

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


Worked like a charm, thanks a lot  @PilotConway !

Copilot Lvl 2
Message 6 of 10

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

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.

Copilot Lvl 3
Message 7 of 10

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

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. 

Copilot Lvl 3
Message 8 of 10

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

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 ;)

GitHub Staff
Message 9 of 10

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

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

Copilot Lvl 3
Message 10 of 10

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

That are wonderful news. Thanks a lot!