How can I reuse a GitHub Actions workflow from the branch I'm currently working on?

Viewed 3193

I'd like to abstract some of my GitHub Actions with a reusable workflow.

In order to do this, I need to call my newly defined callable workflow in the format {owner}/{repo}/{path}/{filename}@{ref}

e.g. (from the docs)

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@172239021f7ba04fe7327647b213799853a9eb89
  call-workflow-2:
    uses: octo-org/another-repo/.github/workflows/workflow-2.yml@v1

However, in practice, I'm working on my branch, some-branch-name, where I'm working on my workflow-1.yml file and I'd like to be able to run my actions as defined in my current branch, Given

`{ref} can be a SHA, a release tag, or a branch name.

It seems like I'd need to use

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@some-branch-name

But, I'd like this to work for any branch, so

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@${{ github.ref }}

But, it turns out this isn't possible as expressions can't be used in the uses attributes.

How can I achieve this?

5 Answers

It's as you said: It can't be done at the moment as Github Actions doesn't support expressions with uses attributes.

There is no workaround (yet?) because the workflow interpreter (that also checks the workflow syntax when you push the workflow to the repository) can't get the value from the expression at that moment.

It could maybe work if the workflow was recognized by the interpreter, but it doesn't event appear on the Actions tab as it's considered invalid.

For the moment, you can only use tag, branch ref or commit hash after the @ symbol, the same way you use any action.

Note that someone else had the same issue here

Update: DO NOT USE THIS, see other answer.


A really hacky (and untested) way to workaround this.

Based on the fact that a workflow can contain multiple jobs, and assuming a low-churn PR environment one could set up something like this:

jobs:
  tag-my-workflow:
    steps:
      - name: git tag ${{ github.ref }} as "current-branch"
        uses: see for example https://levelup.gitconnected.com/how-to-move-a-git-tag-using-github-actions-e23a523eb325

  call-workflow:
    needs: tag-my-workflow
    uses: my/repo/.github/workflows/called.yml@current-branch

I used the workflow-dispatch github action described here to successfully trigger a workflow B in the same repo (note that you need to set up a personal access token secret and include ref: ${{ github.event.pull_request.head.ref }} to refer to the current branch).

But unfortunately workflow A continues once workflow B has been triggered, rather than waiting around to see if workflow B succeeds. As suggested in this thread it might be possible to use another github action to make workflow A wait for workflow B to successfully finish, but that seemed like overkill for me... maybe this'll be useful to someone else though. Fingers crossed it gets supported instead :D

We all know that this is untenable and must have a solution in the long term: You're going to want to establish a common pipeline and have perhaps hundreds of repos calling the same workflow. Having to commit to 'main' on your reusable workflow to test it could break every pipeline that depends on it. So GHA will pick up this functionality soon enough simply because it has to. Workaround: What about forking instead of branching for now? You can develop and test on the fork and submit a PR when it works.

Related