62

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

friederbluemle
38.1k17 gold badges125 silver badges120 bronze badges
asked Nov 11, 2020 at 6:22

9 Answers 9

74

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.event.repository.default_branch) == github.ref }}
McKayla
6,9896 gold badges39 silver badges50 bronze badges
answered Jul 16, 2021 at 19:29
Sign up to request clarification or add additional context in comments.

9 Comments

The ${{ github.event.repository.default_branch }} expression can be used inside jobs, but not to define an event trigger under the on: statement as this question requests
This seems not to work with schedule triggers.
This approach worked for me without the always(). Just if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch).
you can avoid the format string by using this instead github.event.ref_name == github.event.repository.default_branch
I think that should be github.ref_name == github.event.repository.default_branch
|
37

$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]

Blog post: https://github.blog/changelog/2020-07-22-github-actions-better-support-for-alternative-default-branch-names/

answered Jan 14, 2021 at 16:59

1 Comment

Damn! They could have done it right and add a placeholder for default branch.
19
- 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"

Edit 2023

As Elijah pointed out in the comments, a new ref_name field is now available. Simplified:

- if: github.ref_name == github.event.repository.default_branch
 run: echo "On the default branch"
- if: github.ref_name != github.event.repository.default_branch
 run: echo "Not on the default branch"
Bergi
671k162 gold badges1k silver badges1.5k bronze badges
answered Nov 16, 2021 at 20:48

4 Comments

Oddly, I looked at the example docs to print out the context and it looks like this is found in github.event.repository.master_branch
@ChrisKnoll - Do you have a link to the build? That does sound odd indeed.
Sorry for late reply, but I'm having trouble finding the reference in the doucmentation, however, to inspect what was in the payload object, I 'echoed' the contents of the github.event JSON and found the element in the object graph.
Use github.ref_name == github.event.repository.default_branch to avoid the format string.
7

You can run jobs conditionally on the default branch:

jobs:
 job-one:
 if: github.ref_name == github.event.repository.default_branch
 ...

You can run steps conditionally on the default branch:

jobs:
 job-one:
 steps:
 - name: step one
 if: github.ref_name == github.event.repository.default_branch
 ...
answered Aug 22, 2023 at 4:55

Comments

2

It can be achieved reaching GitHub context and particularly

github.event.repository.default_branch
answered Nov 11, 2020 at 9:20

1 Comment

Please delete your answer. github.event.repository.default_branch is the way to go
2

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.ref == format('refs/heads/{0}', 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.

answered May 1, 2022 at 21:36

4 Comments

github.event.push.ref is "". I used if: github.ref_name == github.event.repository.default_branch instead
@Elijah github.event.push.ref only works on push events, which is what the OP was asking about.
My GH Actions was also push driven: github.com/elibroftw/blog.elijahlopez.ca/blob/master/.github/…. Not to mention even the GH Actions vscode extension adds a linting error for github.event.push.ref. here is proof that the event.push.ref is faulty github.com/elibroftw/blog.elijahlopez.ca/actions/runs/…. Also, because you are defensive, your format string is also incorrect github.com/elibroftw/blog.elijahlopez.ca/actions/runs/….
@Elijah Thank you for the corrections. I have updated the answer accordingly.
1

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!"
answered Jun 15, 2021 at 1:03

Comments

1

Even though this does not work in the specific code of the question, it does cover the title and main question written.

The easier thing you can do, is just specify an environment variable:

env:
 DEFAULT_BRANCH: refs/heads/${{ github.event.repository.default_branch }}
answered Apr 1, 2023 at 21:29

Comments

0

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 }}
answered May 7, 2021 at 22:43

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.