2.Understanding env, vars, and Contexts in GitHub Actions: A Practical Guide

Amrit PoudelAmrit Poudel
4 min read

In the world of DevOps, automation is key, and GitHub Actions makes it easy to automate tasks for continuous integration and deployment. Recently, I’ve been diving deeper into GitHub Actions, particularly around using environment variables (env), GitHub repository variables (vars), and how to leverage contexts in workflows. This post captures what I’ve learned and how you can utilize these features to build dynamic workflows.

What are env and vars?

Before diving into code examples, let’s clarify these terms:

  • env: These are environment variables defined at either the workflow or job level. They allow you to pass information that can be accessed throughout the workflow or specific jobs.

  • vars: Repository-level variables that are defined in the repository’s settings. These can be accessed from any workflow file and are useful for storing constants or sensitive data.

  • Contexts: Special variables like ${{ github.event_name }}, ${{ github.ref }}, etc., which provide detailed information about the GitHub event triggering the workflow, metadata about the run, or access to predefined GitHub contexts.

Setting up a Workflow with env, vars, and Contexts

Let’s dive into an example workflow that illustrates how to use these features:

name: 06 - Contexts
run-name: My custom workflow run name - ${{ github.event_name }}

on:
  - push
  - workflow_dispatch

env:
    name: "amrit"
    task: "administration"

jobs:
  echo-data:
    runs-on: ubuntu-latest
    env:
        surname: "poudel"
        task: "system administration"

    steps:
      - name: Display Information
        run: |
          echo "Event Name: ${{ github.event_name }}"
          echo "Reference: ${{ github.ref }}"
          echo "SHA: ${{ github.sha }}"
          echo "Actor: ${{ github.actor }}"
          echo "Workflow: ${{ github.workflow }}"
          echo "Run ID: ${{ github.run_id }}"
          echo "Run Number: ${{ github.run_number }}"

      - name: Retrieve Variable
        run: |
          echo "MY_VAR: ${{ vars.MY_VAR }}"

      - name: retrieve env
        run: |
          echo "my env are ${{env.name}} ${{env.surname}} ${{env.task}}"

Breakdown of the Workflow

  • run-name: This sets a custom name for the workflow run. I’ve used ${{ github.event_name }} to make the name dynamic based on the GitHub event that triggers the workflow. This gives you a clear identifier in your Actions tab.

  • env at the Workflow Level: I’ve defined two global environment variables, name and task. These will be available across the entire workflow unless overridden at the job level.

  • env at the Job Level: Within the echo-data job, I’ve overridden task with a new value, "system administration", and introduced a new variable, surname. This demonstrates how you can scope environment variables to jobs.

  • Using Contexts: The first step displays GitHub context variables such as the event name, reference (branch), SHA, actor, workflow, run ID, and run number. These are super useful for debugging and tracking workflows in different environments.

  • vars.MY_VAR: This step retrieves a variable named MY_VAR that’s defined in the repository’s settings. If you want to store constants or even sensitive data securely, GitHub repository variables come in handy.

  • Displaying Environment Variables: In the final step, I retrieved and displayed the env variables. Notice how it uses both workflow-level and job-level environment variables, showing how they can be combined or overridden.

What I Learned

  1. Defining and Scoping Variables: Understanding how to use env variables at different levels (workflow vs job) helped me create more modular and reusable workflows.

  2. Dynamic Workflow Names: The use of run-name with context values like ${{ github.event_name }} makes it easy to identify workflow runs, especially when working with multiple events.

  3. Using Repository Variables: By accessing repository variables with vars, I can manage constants or sensitive data in a secure and centralized way, outside the workflow files.

  4. Leveraging Contexts: Contexts in GitHub Actions, like ${{ github.sha }}, ${{ github.ref }}, and others, provide a rich set of information about each workflow run, which can be valuable for conditional logic, debugging, or tracking.

Final Thoughts

This journey into GitHub Actions has shown me the importance of organizing workflows in a clean and dynamic way. Variables (env, vars) and contexts are key tools for managing complexity, allowing you to pass information between jobs, control behavior dynamically, and securely manage data.

By experimenting with different configurations, I’ve learned that mistakes are part of the process, and each one brings a new level of understanding. I hope this guide gives you a strong foundation in managing variables and contexts in GitHub Actions.


Conclusion

This article reflects my practical experience working with GitHub Actions and my approach of learning by doing. Making mistakes along the way has deepened my understanding, and I hope this post serves as a helpful guide for others venturing into the world of CI/CD.

0
Subscribe to my newsletter

Read articles from Amrit Poudel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amrit Poudel
Amrit Poudel