Skip to main content
In this guide, you’ll discover how to harness context variables and if expressions in GitHub Actions workflows to run jobs conditionally. This is essential for optimizing CI/CD pipelines, reducing unnecessary steps, and ensuring deployments only occur on the desired branch.

What Are Context Variables?

When a workflow runs, GitHub makes a set of context variables available in JSON format. You can reference these contexts with expressions like ${{ github.ref }} or ${{ env.VAR_NAME }}.
For a deep dive into expressions and context variables, see the GitHub Actions Expressions docs.

Common Contexts in Workflows

Sample Workflow: Build and Conditional Deploy

Below is a workflow that builds a Docker image on every push but only deploys when the push targets the main branch.
The deploy job is guarded by the if expression. It only runs when github.ref equals refs/heads/main.

Observing Workflow Runs

  1. Push to a feature branch:
  1. Notice that the docker job succeeded but the deploy job is skipped:
If your if condition is malformed or compares the wrong context, the job will silently skip. Always verify your branch references.
  1. Open a pull request from your feature branch into main:
  1. Ensure all status checks pass before merging:
  1. After merging into main, observe the full workflow including deploy:
This end-to-end example illustrates how to use if expressions and contexts to drive conditional job execution in your CI/CD pipelines.

Watch Video