Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
In this lab, you will learn how to display environment variables in Linux, macOS, and Windows operating systems using GitHub Actions.
GitHub Actions allows you to display environment variables in the logs. This can be useful for debugging purposes or to understand the environment in which your code is running. Please note that these environment variables are not secure and should not be used to store sensitive information. Additionally, they are case-sensitive.
Estimated Duration: 20-30 minutes
Navigate to the Environment Variables List Starter File.
Copy the content of the starter file:
name: Env Var List
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/env-var-list.yml'
In your repository, create a new workflow file under .github/workflows
and name it env-var-list.yml
.
Paste the copied content into the new file.
Commit the workflow file to the main
branch.
In the env-var-list.yml
file, define three jobs: ubuntu
, mac
, and windows
. These jobs will run the same task of displaying environment variables, but on different operating systems.
For each job, use the respective operating system as the runs-on
value:
runs-on: ubuntu-latest
runs-on: macos-latest
runs-on: windows-latest
Under each job, add a steps
section to display the environment variables. The printenv | sort
command will display environment variables for each operating system.
The workflow should look like this:
name: Env Var List
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/env-var-list.yml'
jobs:
ubuntu:
runs-on: ubuntu-latest
steps:
- name: Display Environment Variables on Ubuntu
run: printenv | sort
mac:
runs-on: macos-latest
steps:
- name: Display Environment Variables on MacOS
run: printenv | sort
windows:
runs-on: windows-latest
steps:
- name: Display Environment Variables on Windows
run: printenv | sort
shell: bash
Name: The name
field defines the name of the workflow. In this case, it’s set to Env Var List
, which will be displayed in the Actions tab.
Triggers: The on
field specifies the event that triggers the workflow. Here, the workflow is triggered either manually via workflow_dispatch
or whenever changes are made to the .github/workflows/env-var-list.yml
file.
Jobs: This workflow contains three jobs:
ubuntu
: Runs on ubuntu-latest
.mac
: Runs on macos-latest
.windows
: Runs on windows-latest
.Steps: Each job has a steps
section where commands are defined to display environment variables using the printenv | sort
command. The printenv
command lists all environment variables, and sort
orders them alphabetically.
Commit the changes to the main
branch.
Go to your repository on GitHub, and navigate to the Actions tab.
Find the Env Var List
workflow on the left sidebar.
Click on the Run workflow button, and select the main
branch.
Click the Run workflow button to trigger the workflow.
Once the workflow runs, click on the Display Environment Variables job to see the logs for the three operating systems: Ubuntu, macOS, and Windows.
Review the logs to see the environment variables displayed for each operating system. You should notice some common environment variables shared across all operating systems, along with specific environment variables for each OS.
You can also use the matrix strategy to run the same job across multiple operating systems in parallel. This is useful for testing your code across different environments simultaneously.
Replace the three individual jobs with a matrix strategy in the workflow file:
name: Env Var List
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/env-var-list.yml'
jobs:
display-environment-variables:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: $
steps:
- name: Display Environment Variables
run: printenv | sort
shell: bash
Commit the changes and rerun the workflow.
Congratulations! You have successfully created a workflow to display environment variables on Ubuntu, macOS, and Windows using GitHub Actions. You have also learned how to use the matrix strategy to run the same job across multiple operating systems in parallel.