Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 531
kharandziuk Avatar asked Nov 11 '20 06:11

kharandziuk


People also ask

How do I set an environment variable in Git action?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

Do GitHub Actions run as root?

/github/workspace - Note: GitHub Actions must be run by the default Docker user (root).

How do I pass variables from one workflow to another in GitHub Actions?

Passing values between steps and jobs in a workflow If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job.


2 Answers

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

like image 149
ddelange Avatar answered Sep 20 '22 19:09

ddelange


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 }}
like image 23
antonmos Avatar answered Sep 20 '22 19:09

antonmos