Github Actions tips
5 May 2022Context & Knowing the Current Branch
https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
What is the difference between github.head_ref
and github.ref
in github actions? Which value should you use?
- When the workflow is a pull_request,
head_ref
contains the source ref, and ref contains the target branch. - When the workflow is a push,
ref
contains the branch pushed.
An example with a simple debug workflow:
on: [push, pull_request]
name: Test refs!
jobs:
run_test:
runs-on: ubuntu-latest
steps:
- name: show the refs
shell: bash
run: |
echo github.head_ref ${{ github.head_ref }}
echo github.ref ${{ github.ref }}
echo github.ref_name ${{ github.ref_name }}
Outputs when I push to a branch:
github.head_ref
github.ref refs/heads/pr-with-ref
github.ref_name pr-with-ref
Output when I create a PR with that same branch:
github.head_ref pr-with-ref
github.ref refs/pull/22/merge
github.ref_name 22/merge
So it looks like, most of the time, you should use ${{ github.head_ref || github.ref_name }}
which will give you the branch.
Examples:
- https://github.com/laurentsenta/pl-github/actions/runs/2542594933
- https://github.com/laurentsenta/pl-github/actions/runs/2542596311
Evaluate Expressions
I have an action that takes a URL input, like
https://docs.github.com/en/enterprise-cloud@latest/actions/learn-github-actions/expressions
on:
workflow_call:
inputs:
url_endpoint: # http://my-endpoint.com/something
required: false
type: string
jobs:
defaults:
run:
shell: bash
steps:
- name: Checkout sources
uses: actions/checkout@v2
with:
path: some-path
repository: ${{ env.TEST_PLAN_REPO }}
ref: ${{ env.TEST_PLAN_BRANCH }}
- name: call external action
uses: some/[email protected]
with:
backend_addr: ${{ ??? }} # my-endpoint.com/something
backend_proto: ${{ ??? }} # http
How to tell if an expression is empty?
Say I have a job that sets an ouput, how to run a step if and only if, the output is set?
name: Test libp2p Compatibility
on:
workflow_call:
inputs:
testsuite:
required: true
type: string
jobs:
run_test:
name: Test
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: prepare env variable
run: |
echo "::set-output name=my_var::helloworld"
shell: bash
- name: collect result
if: ${{ failure() && steps.run.outputs.my_var == null }}
run: |
testground collect ${{ steps.run.outputs.output_id }} --extended
Example of github actions expressions
Laurent Senta
I wrote software for large distributed systems, web applications, and even robots.
These days I focus on making developers, creators, and humans more productive through IPDX.co.