logo
podcast Podcast
get help Get Unstuck

Why is this GitHub Action running on the wrong branch?

Read the docs very closely

Joel Clermont
Joel Clermont
2024-12-11

We had a relatively simple GitHub Action that was split into two parts:

  1. First we'd run our quality checks (code standards, static analysis, tests).
  2. If that passed AND we were on the develop or main branch, we'd deploy to the corresponding staging or production environment.

What I noticed is that even when the first workflow was triggered from a merge to the main branch, when the second workflow ran it was always running in the context of the develop branch. Why is this?

It's important to note that we are using two different triggers for these workflows:

  1. The first workflow is triggered on the push event. (docs)
  2. The second workflow is triggered on the workflow_run event. (docs)

Inside the second workflow, we would check the github.ref and use that to determine if we should skip deployment (feature branch), deploy to staging (develop branch), or deploy to production (main branch).

My assumption is that github.ref would be the branch that triggered the workflow, but that's not the case for the workflow_run event.

If you look at the table of event payload fields for that event in the docs, you'll notice that the GITHUB_REF is always set to the repo's default branch. In our case, develop is the default branch. So that's why this was always deploying to staging.

Now that I actually read the docs closely and understand that, what is the solution? I can instead look at github.event.workflow_run.head_branch to get the branch that triggered the workflow.

It was a simple change, but it did have me scratching my head for a little bit at first.

Here to help,

Joel

P.S. Reading these tips one at a time, day after day, is good for learning, but not great for long-term reference. Check out our volumes of tips instead for handy reference.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.