kharandziuk
kharandziuk

Reputation: 12900

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

Upvotes: 57

Views: 35334

Answers (9)

friederbluemle
friederbluemle

Reputation: 37147

- 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"

Upvotes: 16

Old Pro
Old Pro

Reputation: 25557

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.

Upvotes: 2

Krzysztof Madej
Krzysztof Madej

Reputation: 40829

It can be achieved reaching GitHub context and particularly

github.event.repository.default_branch

Upvotes: 2

antonmos
antonmos

Reputation: 1046

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 }}

Upvotes: 70

Vicente Olivert Riera
Vicente Olivert Riera

Reputation: 1220

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
        ...

Upvotes: 7

froblesmartin
froblesmartin

Reputation: 1891

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 }}

Upvotes: 1

scottclowe
scottclowe

Reputation: 2302

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!"

Upvotes: 1

Scott O
Scott O

Reputation: 1

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 }}

Upvotes: 0

ddelange
ddelange

Reputation: 1585

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

Upvotes: 37

Related Questions