Workflow_run not working as expected

We recently made an update to https://github.com/echobind/bisonapp that required us splitting into two separate workflows (one for tests that uses a matrix, and one for release that runs semantic release).

It seems like there is something strange related to branch triggering and merging.

Tests workflow snippet:

name: Run Tests
on: [push]

Release workflow snippet:

name: Release
on:
  workflow_dispatch:
  workflow_run:
    workflows: ["Run Tests"]
    branches: [main, canary]
    types:
      - completed

Our default branch is canary. Everything works fine when pull requests are merged there.

Once we try to merge canary into main though, it will not run the Release workflow properly. GitHub Actions seems to think that the branch is actually canary when it should be main.

Example:
The reference to canary in this screenshot should really be main (the workflow that ran right before it was from the main branch)
image

@cball,

The branch “canary” displayed in the screenshot you shared is the branch where the “Release” workflow was running on. It is not the branch where the “Run Tests” workflow was running on and triggered the “Release” workflow.

There is point you need to know:
The workflow_run event can trigger the workflow only when the workflow file is on the default branch.
This means the workflows trigged by the workflow_run event can run only on the default branch.

You may have noticed that in the docs about the workflow_run event, this point is not noted.

I have created an issue ticket (github/docs#799) to report this question to the appropriate engineering team for further investigation and evaluation.

You can follow this issue ticket and add your comments to it.

Thanks @brightran. If I’m following you correctly, you are saying the workflow I’m trying to setup is impossible?

I need to run my tests against a matrix of different node versions. I need to run a release command if the matrix is successful. And I need to do that on both my default branch (canary) and the main branch in order to release canary and release versions of my package.

If this isn’t supported, is there some kind of workaround I might be missing?

@cball,

If I’m following you correctly, you are saying the workflow I’m trying to setup is impossible?

Yes, due to the default branch in your repository is canary, the workflow_run event will always trigger the workflow on the canary branch.


If this isn’t supported, is there some kind of workaround I might be missing?

Sure, we have the workaround.
After a run of the"Run Tests" workflow completed on the main branch, then the workflow_run event triggers a run of the “Release” workflow on the default branch (canary).
From the github context of the “Release” workflow’s run, you can get the following properties:

  • github.event.workflow_run.head_branch - - the name of the head branch where the workflow which triggered the workflow_run event was running. In your case, it value is main, if the “Run Tests” workflow was running on main branch.

  • github.event.workflow_run.head_sha - - the SHA of the head commit on the github.event.workflow_run.head_branch. In your case, its value is the latest commit that triggered the “Run Tests” workflow on main branch.

To view more details, you can print the complete github context in the logs.

In your case, when you need using the information of the “Run Tests” workflow’s run in the run of the “Release” workflow, you can use the properties I mentioned above instead of directly use the default values.
For example:

name: Release
on:
  workflow_dispatch:
  workflow_run:
    workflows: ["Run Tests"]
    branches: [main, canary]
    types:
      - completed
jobs:
  job1:
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.event.workflow_run.head_branch }}
# After a run of the "Run Tests" workflow is completed on the main branch,
# a run of the "Release" workflow is triggered on the canary branch.
# This will check out the latest commit on the main branch.

      - name: Checkout
        uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.event.workflow_run.head_sha }}
# If using the head_sha as the ref of the checkout action ,it will check out the
# commit that triggered the run of the "Run Tests" workflow on the main branch.
1 Like

Thanks @brightran! I’ll give that a go.