DEV Community

Cover image for Mastering GitHub Actions: Environment Variables and Secrets Management
Kafeel Ahmad (kaf shekh)
Kafeel Ahmad (kaf shekh)

Posted on

Mastering GitHub Actions: Environment Variables and Secrets Management

GitHub Actions is a powerful tool for automating your software development workflows directly from your GitHub repository. In this article, I will show you how to leverage environment variables and secrets to enhance your workflows’ customization and security.

Environment Variables in GitHub Actions

Environment variables are a key aspect of customizing your workflows. They allow you to define variables that can be used across different jobs and steps within your GitHub Actions workflows.

Why Use Environment Variables?

  • Flexibility: Easily adapt your workflow to different environments (development, testing, production).
  • Reusability: Define once, use multiple times within your workflow.
  • Consistency: Ensure consistent values across various parts of your workflow.

How to Set Environment Variables

You can set environment variables at different levels within your workflow:

  1. Workflow Level:
name: CI Workflow
on: [push]
env:
NODE_ENV: production
API_URL: https://api.example.com
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install

2. Job Level:

jobs:
build:
runs-on: ubuntu-latest
env:
NODE_ENV: production
API_URL: https://api.example.com
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install

3. Step Level:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set environment variable
run: |
echo "NODE_ENV=production" >> $GITHUB_ENV
echo "API_URL=https://api.example.com" >> $GITHUB_ENV
- name: Install dependencies
run: npm install

Also you are able to set custom variables for use in a single workflow or multiple workflows as described in the official Github Documentation.

To create a new variable on Github, follow the steps:

  • Navigate to your repository on GitHub.
  • Go to Settings > Secrets & Variables and click Actions
  • Select Variables tab
  • Click on New Repository Value
Example variable
  • Enter a name for your variable and the value.

You are able to view the value of the variable. Also you are allowed to edit the value. In order to be able to use it in your workflows check out the following example:

jobs:
build:
runs-on: ubuntu-latest
env:
NODE_ENV: production
API_URL: ${{ vars.API_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install

Secrets Management in GitHub Actions

Managing sensitive information such as API keys, tokens, and passwords securely is crucial. GitHub Secrets allows the user to store and use sensitive data securely within your workflows.

Why Use GitHub Secrets?

  • Security: Keep sensitive information out of your codebase.
  • Accessibility: Easily access secrets within your workflows.
  • Confidentiality: Secrets are encrypted and only exposed to workflows that need them.

How to Set Up GitHub Secrets

  1. Add Secrets to Your Repository:
  • Go to Settings > Secrets & Variables and click Actions
  • Select Secrets tab
  • Click on New Repository Secret
List of Repo variables
  • Enter a name for your secret and the secret value.

Here you are not allowed to view the secret value, but only the secret key. You are able to make changes to the secret.

2. Using secrets in your workflows:

name: CI Workflow
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Use secret
env:
API_KEY: ${{ secrets.API_KEY }}
run: echo "Using API key: $API_KEY"

Best Practices for Secrets Management

  • Limit Scope: Only expose secrets to workflows and jobs that need them.
  • Regular Rotation: Regularly update and rotate your secrets to minimize risk.
  • Audit Usage: Monitor and audit the usage of secrets within your workflows.

Advanced Tips for Organizations and Specific Repositories

When managing environment variables and secrets across multiple repositories within an organization, consider using GitHub’s Organization Secrets. This feature allows you to share secrets across all repositories in an organization, reducing redundancy and ensuring consistency. For repository-specific configurations, use repository-level secrets to tailor workflows to the unique needs of each project. Additionally, you can use GitHub Actions Environment Files to create environment-specific variables, ensuring that each workflow runs under the appropriate conditions. By strategically organizing your environment variables and secrets, you can streamline your CI/CD processes while maintaining high security standards.

Conclusion

By leveraging environment variables and secrets management, you can enhance the flexibility, reusability, and security of your GitHub Actions workflows. Proper use of these features ensures that your workflows are not only efficient but also secure, maintaining the integrity of your sensitive information.

Top comments (0)