if expressions with context variables to run jobs only when certain conditions are met—such as deploying only on the main branch.
Understanding Context Variables
During a workflow run, GitHub provides a set of context variables. Here’s a sample dump of some available variables:| Context Variable | Description | Sample Value |
|---|---|---|
github.ref | Full Git ref of the workflow event | refs/heads/main |
github.sha | Commit SHA of the current run | ab3cb9cc32c801545e48e279bad3cf8c646 |
github.repository | Owner and repository name | sidd-harth-7/actions-1 |
github.event_name | Event name that triggered the workflow | push |
For full details on expressions, see the GitHub Actions expressions documentation.

Example Workflow: Build and Deploy
Below is a basic workflow that builds and publishes a Docker image on every push or manual dispatch, then deploys it:Adding a Conditional if Expression
To ensure the deploy job only runs on the main branch, add an if condition that checks github.ref:
github.ref holds the full reference (e.g., refs/heads/main). If the condition is false, the deploy job is skipped.
Viewing Skipped Jobs for Feature Branches
When you push to a feature branch, the Docker build and publish steps run, but the deploy job is marked as skipped:Check the Actions tab in your repository to see which jobs succeeded or were skipped.


Merging into main to Trigger Deployment
- Create a pull request from your feature branch into
main. - After merging and approval, the PR page shows all checks passing.
- Once merged, delete the branch if desired.
Pull request details display commit statuses, merge confirmation, and branch deletion options.


main starts—both the docker and deploy jobs execute:
Monitor the Actions tab to confirm that the deploy job ran successfully.


Summary
Usingif expressions with context variables like github.ref lets you implement branch-specific logic in your workflows. This approach ensures deployments occur only when code reaches the intended branch, enhancing control and security in your CI/CD pipeline.