GitHub Actions: Harnessing the Power of Variables in a Workflow

GitHub Actions: Harnessing the Power of Variables in a Workflow

Hello, GitHub enthusiasts! Following up from our previous tutorial on creating a workflow using GitHub Actions, today we will delve into a fundamental aspect of creating dynamic and efficient workflows – the use of variables.

Understanding Variables in GitHub Actions

Variables in GitHub Actions can be used to dynamically set and use data in your workflows. They can be leveraged to make workflows reusable across different projects and contexts, making them more flexible and less prone to errors due to hard-coded values.

In GitHub Actions, you’ll encounter two main types of variables:

  1. Environment Variables: These are available to all the steps in a job. You can use them to share data between steps and avoid repetition.
  2. Step Output Variables: These are variables that are produced as outputs from a specific step and can be used as inputs in subsequent steps.

Utilizing Environment Variables

Environment variables can be set at multiple levels in a workflow: per workflow, per job, or per step.

Setting Environment Variables

Here’s an example of setting an environment variable at the job level:

In this example, MY_ENV_VAR is set as an environment variable and its value is “Hello, World!”. This variable is then accessed in a later step using ${{ env.MY_ENV_VAR }}.

Using GitHub’s Default Environment Variables

GitHub sets default environment variables for each GitHub Actions run. You can use these to access information about the workflow run and the environment. For example, you can use github.run_id to get the unique identifier of the specific run.

Utilizing Step Output Variables

Output variables allow you to create more complex workflows where steps are dependent on the results of previous steps.

Setting Step Output Variables

Here’s an example of setting and using an output variable:

In this example, RANDOM_NUMBER is set as an output variable in the first step, which has an id of random_step. This variable’s value is then accessed in a later step using ${{ steps.random_step.outputs.RANDOM_NUMBER }}.


Using variables in GitHub Actions can make your workflows more dynamic, readable, and reusable. They allow you to share data between steps, make your workflows more generic, and facilitate complex workflows with dependencies between steps.

Remember, learning to effectively use variables is a stepping stone to mastering GitHub Actions. Start small, experiment, and gradually create more complex workflows. As always, happy coding!