2

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.

asked Oct 10, 2020 at 10:23
1
  • Can the script that needs to know the branch name obtain it itself by doing a git command? (Assuming that the uses clause here means the script starts in the right branch) Commented Oct 10, 2020 at 15:36

2 Answers 2

1

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: ./
answered Oct 11, 2020 at 1:24
Sign up to request clarification or add additional context in comments.

Comments

0

If your shared action references other components in it's repo, then there's 2 components to the "shared action from branch".

  1. The branch for the workflow
  2. 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.

answered Mar 16, 2024 at 16:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.