Setting Environment Variables in GitHub Actions
Introduction
In this tutorial, you will learn how to set environment variables in GitHub Actions. Environment variables provide a way to store and pass information between steps in your GitHub Actions workflows. Properly managing and setting environment variables can help optimize your workflow code and make it more efficient.
GitHub Actions Set Environment Variables: Basic Usage
To set environment variables in GitHub Actions, you can use the `env` context object. To illustrate this, let's break down the configuration into three parts: the environment variable name, the value, and the step or job to apply it to. The following is a simple workflow file that demonstrates how you can set an environment variable in a GitHub Actions workflow:
name: GitHub Actions Set Environment Variables
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set environment variable
run: echo "Hello, $MY_VARIABLE!"
env:
MY_VARIABLE: World
In this example, `MY_VARIABLE` is set to the value `World`. The environment variable is then accessed using the `$MY_VARIABLE` syntax within the `echo` command.
Setting Global Environment Variables
If you want to set an environment variable that can be accessed by all jobs and steps in your GitHub Actions workflow, you can use the `env` attribute at the top level of your workflow file. This way, all jobs and steps can access the environment variable without the need to redeclare it. Here's an example:
name: GitHub Actions Global Environment Variables
on: [push]
env:
GLOBAL_VARIABLE: All-Jobs-Access
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Access global variable
run: echo "Hello, $GLOBAL_VARIABLE!"
test:
runs-on: ubuntu-latest
steps:
- name: Access global variable
run: echo "Hello again, $GLOBAL_VARIABLE!"
In this example, the `GLOBAL_VARIABLE` is set globally and can be accessed by both the `build` and `test` jobs.
Setting Environment Variables with Secrets
When you want to securely set environment variables in your GitHub Actions workflows, you can use secrets. Secrets are encrypted environment variables that cannot be accessed by users with read access to your repository. Here's how you can set an environment variable using a secret:
name: GitHub Actions Set Environment Variables with Secrets
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set environment variable from secret
run: echo "Hello, $MY_SECRET_VARIABLE!"
env:
MY_SECRET_VARIABLE: ${{ secrets.SECRET_VARIABLE }}
In this example, the value of the `MY_SECRET_VARIABLE` environment variable is retrieved from the `SECRET_VARIABLE` secret.
Conclusion
This tutorial has shown you how to set environment variables in GitHub Actions, including setting them at the step level, globally for all jobs, and using secrets. Properly managing and setting environment variables can help make your workflow code more efficient and secure. Begin implementing these techniques to optimize your GitHub Actions workflows today!