Skip to main content
How to access GitHub Actions Contexts
3 min read

How to access GitHub Actions Contexts

Introduction

We would like to share some insights regarding how contextual information is accessed in GitHub Actions workflows. This gives us an understanding of the different contexts available to us, which can help optimize how we manage workflow runs, jobs, and steps.

What’s the github context?

Contexts in GitHub Actions allow us to fetch details concerning the workflow runs, including variables, environment settings, jobs details, and so on. We will be able to access contexts through expressions like ${{ <context> }} that enable us to work with properties from various stages of the workflow.

For example, we can look at:

Context: Event data specific to GitHub, such as branch information (github.ref).

  • ENV context: Variables defined at workflow, job, or step levels.
  • Job and step contexts: Data particular to either the job that is running or the steps running within the context of that job.
  • Secrets context: The basic usage is to hold sensitive data, like tokens, to be used at execution time.
  • Matrix and strategy contexts: This is especially useful when a matrix job running different workflow variations, based on changes in file parameters.

Practical Usage

A common use is something like checking what branch is triggering the workflow, using ${{ github.ref }}, and then using that in conditional expressions to decide if certain jobs should run. Another common use is accessing the secrets context to securely pass authentication tokens in a job and keep sensitive information from being exposed. Another option is printing context values to logs for debugging purposes by converting objects to JSON with ${{ toJson(<context>)}}. That would be helpful in case of the investigation of issues during workflow execution.

Branch-based Conditional Job Execution

In this example, the workflow will trigger a deployment to the production server only if the push is on the main branch.

.github/workflows/conditional-job.yml
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"

Matrix Job Example

The following example uses matrix jobs to run the same CI for multiple Node.js versions and operating systems:

.github/workflows/matrix-job.yml
name: Test matrix
on: push
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [14, 16]
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Output node version
run: node --version

Printing Context Information for Debugging

This will dump the GitHub context, job context, and runner context into the log files for debugging:

.github/workflows/debug-context.yml
name: Context testing
on: push
jobs:
dump_contexts_to_log:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Dump job context
env:
JOB_CONTEXT: ${{ toJson(job) }}
run: echo "$JOB_CONTEXT"
- name: Dump runner context
env:
RUNNER_CONTEXT: ${{ toJson(runner) }}
run: echo "$RUNNER_CONTEXT"

Conclusion

Knowing how to take advantage of the different contexts available in a workflow makes our lives easier when it comes to scaling and maintaining our CI/CD pipelines. Using expressions based on the different contexts, we can streamline up configurations for jobs, reduce the redundancy in our workflows, and secure them against potential attacks by being cautious with untrusted inputs.

Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring