Examining environment variables in GitHub CI workflows
Environment variables in GitHub Actions provide a way to store and access sensitive information, configuration values, or temporary data across jobs and steps in CI/CD workflows. These variables are critical for managing secrets, configuring build environments, and ensuring secure handling of sensitive data.
Types of Environment Variables
GitHub-Defined Variables: Automatically available in workflows (e.g.,
GITHUB_TOKEN,GITHUB_REPOSITORY,GITHUB_SHA).Custom Environment Variables: Defined and managed within the workflow (
envkey in jobs or steps).Secrets: Encrypted environment variables that are defined and stored in GitHub Secrets (e.g., API keys, database credentials).
1. GitHub-Defined Environment Variables
GitHub provides built-in environment variables that are useful for workflow metadata and GitHub-specific actions:
| Variable | Description |
|---|---|
GITHUB_WORKFLOW | Name of the current workflow. |
GITHUB_RUN_ID | Unique ID of the current workflow run. |
GITHUB_REPOSITORY | Full name of the repository (owner/repo). |
GITHUB_SHA | SHA of the current commit. |
GITHUB_REF | The reference (branch or tag) for the current run. |
GITHUB_TOKEN | A GitHub token with permissions to perform actions on the repository. |
RUNNER_OS | Operating system on the runner. |
2. Custom Environment Variables
Custom environment variables are defined within the workflow using the env key at the job level or step level:
Example Workflow with Custom Environment Variables:
xxxxxxxxxx221nameCI Workflow2on3 push4 branches5main6jobs7 build8 runs-onubuntu-latest9 env10 NODE_ENVproduction11 API_URLhttps//api.example.com12 steps13nameCheckout Repository14 usesactions/checkout@v315nameSetup Node.js16 usesactions/setup-node@v317 with18 node-version1619nameInstall Dependencies20 runnpm install21nameRun Tests22 runnpm testIn this example:
NODE_ENV and API_URL are custom environment variables.
3. Using Secrets as Environment Variables
GitHub Secrets are encrypted environment variables that are useful for storing sensitive information like API keys, tokens, or credentials.
Defining a Secret:
Navigate to the Settings tab of your repository.
Go to Secrets.
Add a new secret (e.g.,
MY_API_KEY).
Example Using Secrets:
xxxxxxxxxx151nameDeploy Workflow2on3 push4 branches5main6jobs7 deploy8 runs-onubuntu-latest9 env10 API_KEY$ secrets.MY_API_KEY 11 steps12nameCheckout Repository13 usesactions/checkout@v314nameDeploy to Production15 run./deploy.sh --api-key $ env.API_KEY In this case, MY_API_KEY is securely accessed via the GitHub Secrets system.
4. Accessing Environment Variables in Steps
You can access both custom environment variables and secrets within different steps of a workflow.
Example Access in a Step:
xxxxxxxxxx51steps2namePrint Environment Variables3 run4 echo "NODE_ENV: $NODE_ENV"5 echo "API_URL: $API_URL"or
xxxxxxxxxx31steps2namePrint Secret3 runecho "My API Key is: $MY_API_KEY"5. Conditional Logic with Environment Variables
You can define conditional logic based on the value of environment variables.
Example: Conditional Step Execution
xxxxxxxxxx131jobs2 deploy3 runs-onubuntu-latest4 env5 DEPLOY_ENVproduction6 steps7nameCheck Deployment Environment8 run9 if [ "$DEPLOY_ENV" == "production" ]; then10 echo "Deploying to production"11 else12 echo "Not deploying to production"13 fiBest Practices for Using Environment Variables
Use Secrets for Sensitive Data: Never hard-code sensitive information into workflows. Use encrypted secrets for storing API keys, tokens, or credentials.
Limit Access: Use
permissionsin workflows to restrict which environment variables are exposed to specific steps or jobs.Environment Variables in Matrix Testing: Use dynamic values in environment variables to handle different configurations or matrix testing scenarios.
Avoid Hardcoding: Always define environment variables declaratively within the workflow YAML file for maintainability.
Debugging with Debug Logs: Enable debugging (
ACTIONS_STEP_DEBUG) for detailed information on environment variables in use.
Summary
By leveraging environment variables effectively, GitHub Actions ensure secure, flexible, and dynamic CI workflows.






















Leave a Reply