Anyone know how to dynamically pass the name of the branch that triggered the workflow into the uses key?
Example
# my workflow
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run on branch
- uses: {org}/{repo}@{branch-name}
My repo is a Github Action. When I create a new branch to make changes to the action I have tests that run the action. But without specifying the branch name they run on the master branch, with the old code, instead of the branch that triggered the workflow.
2 Answers 2
While this does not answer how to pass dynamic values to the uses key, it does seem to have solved my problem.
The checkout action has a ref key you can provide to specify which branch of the repo is checked out.
Once I specified the branch and passed uses a relative path, commits made by the test workflow are being committed to the specified branch, and not master.
# my workflow
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- name: Make Test Changes
- uses: ./
Comments
If your shared action references other components in it's repo, then there's 2 components to the "shared action from branch".
- The branch for the workflow
- The branch for the rest of the payload in the shared action's repo.
For the workflow and workflow's repo payload to be present, you must pass the shared action's branch name as a parameter to the shared action itself.
eg:
step-using-shared-workflow:
needs: build-setup (job name of pre-req)
uses : <OWNER>/<SHARED_REPO_NAME>/.github/workflows/build.yml@<WORKFLOW_REPO_BRANCH>
# Optional parameter for use when above shared repo has changes outside of this workflow
with:
branch: <WORKFLOW_REPO_BRANCH> # Must match the branch of the shared repo in the 'uses: ...@{ref}' above
# other params
skip_unit_tests: ${{ needs.build-setup.outputs.skip_unit_tests == 'true' }}
secrets: inherit
Then, in the shared workflow, checkout BOTH the caller repo and the called action repo, at the branch that was passed as a parameter:
- name: Checkout caller repository
uses: actions/checkout@v4
with:
submodules: 'true'
token: ${{ secrets.TOKEN }}
- name: Checkout called repository
uses: actions/checkout@v4
with:
repository: <OWNER>/<SHARED_REPO_NAME>
path: shared-actions-path (where you want it put)
submodules: 'true'
token: ${{ secrets.XXX }}
# Ensure that the rest of the repo matches the shared workflow branch specified
ref: ${{ inputs.branch }}
Finally, when testing this, and using the "re-run ...." option.
You MUST choose the "ALL jobs" rather than "failed" or it skips the bit where it sync's the latest changes in the shared repo.
usesclause here means the script starts in the right branch)