Introduction
GitHub Actions is a powerful workflow automation tool that allows you to build, test, and deploy your projects directly from your GitHub repository. Environment variables play a crucial role in configuring and customizing your GitHub Actions workflows. They enable you to store sensitive information securely and provide flexibility in configuring different aspects of your workflows. In this article, we will explore how to work with environment variables in GitHub Actions.
Setting Environment Variables
To set environment variables in a GitHub Actions workflow, you have multiple options:
Option 1: Repository Secrets
GitHub provides a feature called “Repository Secrets” that allows you to securely store sensitive information such as API keys, access tokens, or database credentials. To set a secret:
- Go to your GitHub repository.
- Navigate to “Settings” and click on “Secrets”.
- Click on “New Repository Secret”.
- Provide a name for the secret (e.g.,
API_KEY
) and enter its value. - Click on “Add Secret” to save it.
Once the secret is set, you can reference it in your workflow file using the secrets
context:
env:
API_KEY: ${{ secrets.API_KEY }}
Option 2: Workflow-level Environment Variables
You can define environment variables directly in your workflow file using the env
keyword. These environment variables are specific to the workflow and can be used across the steps within the workflow:
env:
API_KEY: your-api-key
Option 3: Job-level Environment Variables
You can also define environment variables at the job level, making them available only to a specific job within your workflow:
jobs:
build:
runs-on: ubuntu-latest
env:
API_KEY: your-api-key
Using Environment Variables in Steps
Once you have set your environment variables, you can use them in the steps of your GitHub Actions workflow:
jobs:
build:
runs-on: ubuntu-latest
env:
API_KEY: ${{ secrets.API_KEY }}
steps:
- name: Display API Key
run: echo $API_KEY
In this example, the environment variable API_KEY
is accessed using the $API_KEY
syntax within the run
field of the step.
Environment Variables Expansion
GitHub Actions supports variable expansion, allowing you to reference environment variables within strings and commands. This can be useful when you need to dynamically generate values based on environment variables. Here’s an example:
jobs:
build:
runs-on: ubuntu-latest
env:
API_KEY: ${{ secrets.API_KEY }}
BUILD_VERSION: v1.0
steps:
- name: Display Build Info
run: echo "Build version: $BUILD_VERSION, API key: $API_KEY"
In this example, the environment variables API_KEY
and BUILD_VERSION
are expanded within the run
field of the step. The values of these variables will be replaced in the resulting command.
Conditional Execution based on Environment Variables
You can also conditionally execute steps or jobs based on the values of environment variables. This allows you to control the flow of your workflow based on specific conditions. Here’s an example:
jobs:
build:
runs-on: ubuntu-latest
env:
API_KEY: ${{ secrets.API_KEY }}
steps:
- name: Run Tests
run: npm run test
- name: Deploy to Production
if: ${{ env.API_KEY && github.ref == 'refs/heads/master' }}
run: |
echo "Deploying to production..."
# Deployment steps go here
In this example, the “Deploy to Production” step will only be executed if the API_KEY
environment variable is set and the branch being built is the master
branch. The if
condition ensures that the step is conditionally executed based on the values of the environment variables.
Matrix Strategy with Environment Variables
GitHub Actions allows you to define a matrix strategy to run jobs with different combinations of variables. This can be useful when you want to run the same job with different environment configurations. Here’s an example:
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [12, 14]
runs-on: ${{ matrix.os }}
env:
NODE_VERSION: ${{ matrix.node }}
steps:
- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ env.NODE_VERSION }}
# Rest of the build steps
In this example, the job is executed for multiple combinations of the os
and node
variables defined in the matrix strategy. The NODE_VERSION
environment variable is set dynamically based on the matrix values.
Conclusion
Working with environment variables in GitHub Actions allows you to customize your workflows, securely store and use secrets, and control the execution flow based on specific conditions. By leveraging environment variables, variable expansion, conditional execution, and matrix strategies, you can build flexible and powerful automation pipelines tailored to your project’s requirements within your GitHub repository.