GitHub Actions Adding Environment Variables

GitBeginner
Practice Now

Introduction

In GitHub Actions, environment variables are key-value pairs that are available to the steps in a workflow. You can define environment variables for an entire workflow, a specific job, or a specific step.

In this lab, you will learn how to:

  1. Create a GitHub Actions workflow file.
  2. Define an environment variable at the workflow level.
  3. Access and use the environment variable in a step.
  4. Update the variable and trigger the workflow again.

This lab builds on the repository you created in the previous labs. You will clone the github-actions-demo repository and create a workflow that uses environment variables.

Add env section with a variable GREETING set to 'Hello'

In this step, you will clone the repository and create a new GitHub Actions workflow file with a global environment variable.

  1. On your GitHub repository page for github-actions-demo, click the green Code button.
  2. Ensure the HTTPS tab is selected and copy the URL. It should look like https://github.com/your-username/github-actions-demo.git.
  3. Open the terminal in the LabEx environment. The default path is ~/project.
  4. Use the git clone command to download the repository. Replace your-username with your actual GitHub username.
cd ~/project
git clone https://github.com/your-username/github-actions-demo.git
  1. Navigate into the cloned repository:
cd ~/project/github-actions-demo
  1. Create a new file named env-demo.yml inside the .github/workflows directory:
touch .github/workflows/env-demo.yml
  1. Open .github/workflows/env-demo.yml in the WebIDE and add the following content. This defines the workflow name, the trigger event (push), and the env section with a variable named GREETING.
name: Environment Variable Demo

on: [push]

env:
  GREETING: "Hello"
  • name: The name of your workflow.
  • on: Specifies that this workflow runs when a push event occurs.
  • env: Defines a map of environment variables that are available to all jobs and steps in the workflow. Here, we set GREETING to the string 'Hello'.

Add a step to echo the variable with run echo "${{ env.GREETING }}, World!"

In this step, you will define a job and a step to use the environment variable you created. You access environment variables in the workflow file using the ${{ env.VARIABLE_NAME }} syntax.

Open .github/workflows/env-demo.yml in the WebIDE and append the following content to the end of the file:

jobs:
  print-greeting:
    runs-on: ubuntu-latest
    steps:
      - name: Print Greeting
        run: echo "${{ env.GREETING }}, World!"

Your complete file should look like this:

name: Environment Variable Demo

on: [push]

env:
  GREETING: "Hello"

jobs:
  print-greeting:
    runs-on: ubuntu-latest
    steps:
      - name: Print Greeting
        run: echo "${{ env.GREETING }}, World!"
  • jobs: Defines the jobs that the workflow runs.
  • print-greeting: The ID of the job.
  • runs-on: Configures the machine type (runner) to run the job.
  • steps: Groups together all the steps that run in the job.
  • run: Executes a command in the runner's shell. We use echo to print the value of GREETING followed by , World!.

Commit and push the updated workflow with env

In this step, you will commit the new workflow file and push it to GitHub. Pushing this file will register the workflow with GitHub Actions and trigger the first run.

  1. Ensure you are in the repository directory:
cd ~/project/github-actions-demo
  1. Stage the workflow file:
git add .github/workflows/env-demo.yml
  1. Commit the changes:
git commit -m "Add workflow with environment variable"
  1. Push the changes to the remote repository on GitHub:
git push

Note on Authentication: When you run git push, the WebIDE will automatically prompt you to authenticate. Follow these detailed steps:

  1. A popup will appear with the message: "The extension 'GitHub' wants to sign in using GitHub." Click Allow.
  2. A new notification will appear. Click "Copy&Continue to GitHub", then click "Open" in the next prompt.
  3. Log in to your GitHub account in the browser window that opens, and enter the authorization code that was copied. After confirming the authorization, the page will automatically close.
  4. Wait a few seconds, and you will see the terminal successfully complete the push operation.

Privacy Note: The WebIDE will request full access to your GitHub account for authentication purposes. You don't need to worry about privacy concerns - the LabEx VM will be immediately destroyed after you complete the current lab, and your credentials and authorization information will not be retained.

Trigger workflow by pushing a change

The workflow is configured to run on push events. Although the previous push already triggered the workflow, let's see how easy it is to update the environment variable and trigger it again.

  1. Open .github/workflows/env-demo.yml in the WebIDE.
  2. Change the value of GREETING from "Hello" to "Hi".
env:
  GREETING: "Hi"
  1. Commit and push this change:
git add .github/workflows/env-demo.yml
git commit -m "Update greeting variable"
git push

Note on Authentication: If you haven't authenticated recently, the WebIDE will automatically prompt you to authenticate when you run git push. Follow the same authentication steps as described in the previous step.

Privacy Note: The WebIDE will request full access to your GitHub account for authentication purposes. You don't need to worry about privacy concerns - the LabEx VM will be immediately destroyed after you complete the current lab, and your credentials and authorization information will not be retained.

This push will trigger a new run of the workflow with the updated variable.

Verify environment variable output in run logs on GitHub

After pushing your code, the workflow will automatically trigger on GitHub. You can view the workflow run logs to verify that the environment variable is being used correctly.

  1. Visit your repository on GitHub in a web browser: https://github.com/your-username/github-actions-demo
  2. Click on the Actions tab at the top of the repository page.
  3. You should see a new workflow run listed, likely named "Update greeting variable".

Note: When you push code, multiple Actions may be triggered if you have multiple workflow files in your repository. Please identify the correct workflow run by checking the workflow name in the left sidebar or using the workflow run list on the right side.

  1. Click on the run title, then click on the print-greeting job.
  2. Expand the Print Greeting step to see the output.

You should see the command execution:

Run echo "Hi, World!"
  echo "Hi, World!"
  shell: /usr/bin/bash -e {0}
Hi, World!

Notice that ${{ env.GREETING }} was replaced by its new value Hi.

GitHub Actions logs

Summary

In this lab, you have successfully created a GitHub Actions workflow that uses environment variables. You learned how to:

  1. Define environment variables using the env keyword at the workflow level.
  2. Access these variables in your steps using the ${{ env.VARIABLE_NAME }} syntax.
  3. Update the variable and trigger the workflow again by pushing changes.

Environment variables are essential for managing configuration values that might change between environments or runs, without changing the hardcoded command logic.