'GitHub actions: default branch variable
Is there any smart way to determine the default branch in GitHub actions?
Now I need to write something like:
on:
push:
branches:
- master
is there a way to write something like the code below?
on:
push:
branches:
- $default-branch
I tried to google but found nothing
Solution 1:[1]
$default-branch
can be used in Workflow templates, but not in Workflows. The branch will become hard-coded in the Workflow upon initialization, and will have to be manually maintained. [1]
Solution 2:[2]
I accidentally found a really nice way to solve this. That evalutes to the branch name, e.g. master.
${{ github.event.repository.default_branch }}
Also, found out the hard way that that always()
is side-effecting: my job was getting skipped if always()
was not called even though the other clause was true.
This works to run a job only when running on default branch
if: ${{ always() && format('refs/heads/{0}', ) == github.ref }}
Solution 3:[3]
- if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
run: echo "On the default branch"
- if: github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
run: echo "Not on the default branch"
Solution 4:[4]
This is not possible at the moment. Please check this topic on github community
You simply can reach variable at this level
The workflow is not valid. .github/workflows/so-004-variables-in-trigger.yaml (Line: 7, Col: 9): Unrecognized named-value: 'env'. Located at position 1 within expression: env.default-branch
You may consider addition filterint based on the branch name like here but at the moment you can't do what you want.
Solution 5:[5]
Add this step to your job:
- name: Determine default branch
run: |
DEFAULT_BRANCH=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_ENV
echo "default_branch_ref=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
That will add a default_branch
and a default_branch_ref
variable to the env
enivronment variables.
You can then access the default branch name with ${{ env.default_branch }}
in subsequent steps.
The default_branch_ref
variable is useful for directly comparing against github.ref
to determine whether you are on the default branch.
This method uses the current method of setting environment variables to use in later steps [1] and JoeLinux's method for determining the default branch name [2].
Full example workflow:
name: ci
on: [push, pull_request]
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Determine default branch
run: |
DEFAULT_BRANCH=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_ENV
echo "default_branch_ref=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
- name: debug
run: echo ${{ env.default_branch }}
- name: Deploy
if: github.ref == env.default_branch_ref
run: echo "Run!"
Solution 6:[6]
You can use $default-branch
in a template, and then when that template is rendered into a new repo, it will be replaced with the (then) default branch name for the repo, but that is a very limited use case and still does not help you when the name of the default branch changes. The best I have come up with is to list the all the default branch names in the organization, like this:
on:
push:
branches:
- master
- main
- root
- default
- production
and then you can either trust that the repos will not have non-default branches with those names, or start the jobs and then filter them by adding an if
condition like
if: github.event.push.ref == format('refs/heads/{}', github.event.repository.default_branch)
Side note
For most events
${{ github.event.repository.default_branch }}
is available and works fine, but not when running schedule
events via cron
. When github.event_name == "schedule"
the only element in github.event
is schedule
(the cron
string that triggered the run).
When running inside a GitHub action on at GitHub runner with gh
available, this more reliably gets you the default branch name:
gh repo view --json defaultBranchRef --jq .defaultBranchRef.name
However, this does not help the OP when you want to make the default branch the target that triggers the run.
Solution 7:[7]
Hopefully, there will be a better way to do this in the future. Until then, you can use the GitHub API and save the result in a named step output.
e.g.
- name: Extract default branch name
shell: bash
run: |
owner="my-org"
repo="repo_x"
branch=$(curl -L -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
https://api.github.com/repos/${owner}/${repo} \
| jq .default_branch)
echo "##[set-output name=default_branch;]$(echo ${branch})"
id: repo_x
...
${{ steps.repo_x.outputs.default_branch }}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ddelange |
Solution 2 | |
Solution 3 | |
Solution 4 | Krzysztof Madej |
Solution 5 | |
Solution 6 | |
Solution 7 | soberg |