Home > Back-end >  Using `GITHUB_REF` when triggering a reusable workflow
Using `GITHUB_REF` when triggering a reusable workflow

Time:01-01

I'm trying to call multiple workflows from a single one and I want to use the commit sha, or the branch name or something to get the workflow file with the recent changes on the branch the workflows are triggered on.

For example, I am on a branch <feature_branch> and I want to trigger the workflow's on that branch. I want the content of the later called workflows also to be the one from that branch. For that reason, I tried the following:

// My repository structure's essential part

repo_folder
> .github
| > workflows
| | > main-ci.yml
| | > other-workflow.yml
# main-ci.yml

name: Main CI workflow
on: ...
jobs:
  uses: <my_repo>/.github/workflows/other-workflow.yml@$GITHUB_REF
  ...
# other-workflow.yml
...

The issue is that when GitHub parses the main-ci workflow it doesn't seem to resolve the $GITHUB_REF environment variable before trying to call the workflow, and reports a problem

error parsing called workflow "<my_repo>/.github/workflows/other_workflow.yml@$GITHUB_REF": failed to fetch workflow: reference to workflow should be either a valid branch, tag, or commit

I tried with context variables too (like ${{ github.sha }}) but with that syntax, it asks for removing the spaces from the version field.

CodePudding user response:

Just figured this out after heading out to the workflow syntax section of the GitHub actions' documentation. It says:

If you use the second syntax option (without {owner}/{repo} and @{ref}) the called workflow is from the same commit as the caller workflow.

And the example shows

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

So, all I needed to do is to change

  uses: <my_repo>/.github/workflows/other-workflow.yml@$GITHUB_REF

to

  uses: ./.github/workflows/other-workflow.yml
  • Related