GitHub Actions (S1E05)

Expression
Use expressions to programmatically set environment variables in workflow files and to access contexts.
#Example setting an environment variable
env:
MY_ENV_VAR: ${{ <expression> }}
The syntax for expression is
${{ <expression> }}
Commonly used with the conditional
if
keyword in a workflow file to determine whether a step should run.
name: CI
on: push
jobs:
prod-check:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to production server on branch $GITHUB_REF"
Status Check Functions
- use the following status check functions as expressions in
if
conditionals. A default status check ofsuccess()
is applied unless you include one of these functions
success(): Returns true
when all previous steps have succeeded.
steps:
...
- name: The job has succeeded
if: ${{ success() }}
always() : Causes the step to always execute, and returns true
, even when canceled
if: ${{ always() }}
failure() : You can include extra conditions for a step to run after a failure, but you must still include failure()
to override the default status check of success()
that is automatically applied to if
conditions that don't contain a status check function.
steps:
...
- name: Failing step
id: demo
run: exit 1
- name: The demo step has failed
if: ${{ failure() && steps.demo.conclusion == 'failure' }}
cancelled(): Returns true
if the workflow was canceled.
Logs
If the workflow logs do not provide enough detail to diagnose why a workflow, job, or step is not working as expected, you can enable additional debug logging.
Runner Diagnostic logging
Provides additional log files that contain information about how a runner is executing a job. Two extra log files are added
The runner process log: Includes information about coordinating and setting up runners to execute jobs.
The worker process log: Logs the execution of a job.
Enable
In the repository that contains the workflow set the variable/secret. : ACTIONS_RUNNER_DEBUG
to true
The secret value takes precedence.
To download runner diagnostic logs, download the log archive of the workflow run. The runner diagnostic logs are contained in the runner-diagnostic-logs
folder
Step Debug logging
Increases the verbosity of a job's logs during and after a job's execution.
Enable
In the repository that contains the workflow set the variable/secret. : ACTIONS_STEP_DEBUG
to true
The secret value takes precedence.
From UI : We can rerun the job and select the Enable debug logging option. This will also enable runner diagnostic logs.
Cache
To make your workflows faster and more efficient, you can create and use caches for dependencies and other commonly reused files.
To cache dependencies for a job, you can use GitHub's
cache
action. The action creates and restores a cache identified by a unique key.The
cache
action will attempt to restore a cache based on thekey
you provide on a hit and creates one on a miss.
- name: Cache Gradle packages
uses: actions/cache@v3
with:
name:
path: |
~/.gradle/caches
~/.gradle/wrapper
Using expressions to create a key
allows you to automatically create a new cache when dependencies change. Below examples creates a new cache when the packages in package-lock.json
file change, or when the runner's operating system changes.
name: Caching with npm
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-cache-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Test
run: npm test
Limits
Cache entries not accessed in over 7 days is removed. Upto 10Gb of cache is stored after which the old entries are deleted.
Subscribe to my newsletter
Read articles from Jeason Joseph directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jeason Joseph
Jeason Joseph
I am an DevOps Engineer working since 2018. Had the opportunity to provide consulting services to many IT service companies. Passionate about learning new technology.