Home > OS >  GitHub Context Variables Not Evaluating for Reusable Workflow Reference
GitHub Context Variables Not Evaluating for Reusable Workflow Reference

Time:10-28

I'm currently trying to build a reusable workflow where the reusable workflow is correctly setup with an on: workflow_call trigger and so forth.

When I'm calling the workflow though, I need to be able to dynamically populate the shared workflow file reference with the current branch or commit. I'd also like to populate the organisation and repo dynamically as well if possible with the current org/repo, but that's less essential. My call to the other workflow therefore looks like this:

jobs:
  call-build-and-test:
    uses: ${{github.repository}}/.github/workflows/cicd-build-and-test.yml@${{github.sha}}

Unfortunately however the two context variables, github.repository, and github.sha don't get evaluated and the workflow fails at runtime stating that a valid organisation and repo are required at the start of the string, or, if the organisation and repo are hardcoded so that they're valid, then it states the commit couldn't be found because that's not evaluated correctly either.

Can anyone explain why the context variables aren't getting evaluated? If this is not possible how can I dynamically populate these values? It's not sufficient to hardcode them as they need to execute against the version on the current branch, as the main branch has not had workflow merged in yet, so can't simply be hardcoded to that as in the examples in the Github docs here:

https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#calling-a-reusable-workflow

I believing using context variables such as github.* is the right approach, as environment variables are only available within the scope of a shell executing a job, however I have also tried environment syntax as well, i.e. GITHUB_REPOSITORY, and GITHUB_SHA with the same problem.

CodePudding user response:

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. Therefore, this has to be hardcoded at the moment.

  • Related