Trigger job on tag push only

I have a workflow that is triggered by pushes, pull requests, and tags. The workflow has multiple jobs. One of the jobs is responsible for creating a release on GitHub. I want to restrict the release job to run only when the event is a created/pushed tag.

How can I do that?

@msafi ,

Creating/pushing a new tag to the repository is a type of Push event. By default, both pushing commits and pushing tags will trigger workflow run for, if you don’t set branches filter and tags filter for the Push event. More details you can reference here.

When the wokflow run is triggered by pushing tag, the github.ref is the full name of the pushed tag ( refs/tags/<tag> ).
So if you want to check if the workflow is triggered by pushing tag, you can try using the following if conditional for the job.

if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

Related docs:

github context

Functions

2 Likes

I’ve spent the last two days on trying to make something like this work. Am I nuts or is this unreasonably hard?

Similar to you, all I want to be able to do is define some behavior that runs only when someone does a Release, and for that behavior to know the name of the branch it is running against.

I managed to make the first part of that work on something like the 30th attempt with an unholy and somewhat ridiculous combination of setting environment variables in one step using a shell, and then using them in a different step as new environment variables, and then finally using THOSE in an if: clause. As far as I can’t tell you can’t do that at the job level, but you can make individual steps conditional.

But once I got that working, it turned out that the branch name was no longer available because now the reference is to the release, not to the branch.

Sorry I don’t have better answers. This is my first experience with GitHub actions and I was really hoping that it would make it easy to convince my team to walk away from Jenkins, but my experience so far is that nothing has seemed obvious or easy. 

One of the biggest problems is that all of the supposed solutions come with restrictions on their context. I’m sure those restrictions are there for engineering reasons but this doesn’t feel designed – it feels hacked. Dear GitHub team, this forum is full of people who are trying to figure out an easy way to access basic information about the context in which their jobs are running, and to use those in conditional behavior. Personally, I’m going to have to fall back to running python scripts and just invoking those with Actions.

@brightran Thanks for the solution!

@kentquirk Your experience sounds very similar to mine. I was banging my head against the wall for the past two days trying to figure things out. I found GitHub Actions to be very confusing and missing some basic things that I’ve come to expect from other CI services. I’m hoping GitHub Actions will continue to mature and become easier with time.

So if I want to run some jobs on push to master, on push to a v* tag or on a pull request, I have to use the following if condition?

on:
    push:
    pull_request:

jobs:
    build:
        if: github.event_name == 'pull_request' || (github.event_name == 'push' && (contains(github.ref, '/heads/master') || contains(github.ref, '/tags/v')))
        ...

Seems pretty awkward. Maybe there’s a better solution?

Thanks for the tip @brightran ! However, it seems the push event is just triggered when pushing a tag for one of the last 3 commits. When creating a tag for an older commit and pushing it (just pushing a single tag), the push event is not triggered . Please, do you know how to trigger the push event for older commits?