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
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.
/github/workspace - Note: GitHub Actions must be run by the default Docker user (root).
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.
$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/
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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With