5

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.

1
  • you can use inouts for this. Commented Oct 27, 2021 at 11:10

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

As GuiFalourd says:

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

If you just want to use the ${{ github.sha }}, and you only need the workflow from your local repo, don't specify the ref. As shown here:

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

call-workflow-2-in-local-repo:
    uses: ./.github/workflows/workflow-2.yml

You can find more info here. Unfortunately I can't find any information on whether they are planning to support this one day.

I am also facing this issue when calling a build workflow from a deployment workflow. I think the solution for me is to pass the branch as a param to the reusable build workflow and checkout that branch if it's specified. I am just going to have to accept that I'm not using the workflow file from the branch specified.

I appreciate I'm late with this and it's a very specific scenario, but hope it may help you in some way!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.