Getting branch name in Github Actions based on workflow trigger
I’ve recently had to get the current branch or the target branch of a PR in Github actions. It took a bit of researching, so I thought it’s worth sharing.
The code below will get the short branch name if the trigger was a push or pull request.
Push request contains the full HEAD of the branch ${GITHUB_REF##*/}
will extract only the short name.
After the step runs, you can access the branch name by calling
${{ steps.extract_branch.outputs.branch }}
while in the same job.
If you need to pass that value between jobs, add an outputs block to job1, add a needs block to job2.
You can access the value in job2 by accessing the value in
${{ needs.job1.outputs.extract_branch }}
See the example below.