You can use environment variables to store information that you want to reference in your workflow. You reference environment variables within a workflow step or an action, and the variables are interpolated on the runner machine that runs your workflow. Commands that run in actions or workflow steps can create, read, and modify environment variables.
You can set your own custom environment variables, you can use the default environment variables that GitHub sets automatically, and you can also use any other environment variables that are set in the working environment on the runner. Environment variables are case-sensitive.
To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for:
- The entire workflow, by using env at the top level of the workflow file.
- The contents of a job within a workflow, by using jobs.
.env. - A specific step within a job, by using jobs.
.steps[*].env.
The example above shows three custom environment variables being used in an echo command: $DAY_OF_WEEK, $Greeting, and $First_Name. The values for these environment variables are set, and scoped, at the workflow, job, and step level respectively.
Using contexts to access environment variable values
In addition to environment variables, GitHub Actions also allows you to set and read values using contexts. Environment variables and contexts are intended for use at different points in the workflow.
Environment variables are always interpolated on the virtual machine runner. However, parts of a workflow are processed by GitHub Actions and are not sent to the runner. You cannot use environment variables in these parts of a workflow file. Instead, you can use contexts. For example, an if conditional, which determines whether a job or step is sent to the runner, is always processed by GitHub Actions. You can use a context in an if conditional statement to access the value of an environment variable.
In this modification of the first example, we've introduced an if conditional. The workflow step is now only run if DAYS_OF_WEEK is set to "Monday". We access this value from the if conditional statement by using the env context.