Github Actions tips

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

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

If I push to a branch:

github.head_ref
github.ref refs/heads/pr-with-ref
github.ref_name pr-with-ref

If 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, usually you wan to use ${{ github.head_ref || github.ref_name }} which will give you the branch.

Examples:

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/action@v1.0
        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

https://www.actionsbyexample.com/context-expressions.html

https://sanderknape.com/2021/01/go-crazy-github-actions/


Laurent Senta

I wrote software for large distributed systems, web applications, and even robots. Now I focus on decentralization, overly-engineered software, and frugal innovation. I help companies around the world build products through SingularGarden.