Lab: List Environment Variables
Introduction
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
Instructions
Step 1: Create a YAML Workflow File
-
In your repository, create a directory
.github/workflows
if it doesn’t exist. -
Inside
.github/workflows
, create a new file namedenv-var-list.yml
. -
Add the following content to
env-var-list.yml
: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
-
Commit the file to the
main
branch.
Step 2: Understanding the Workflow
- Name: The workflow is named
Env Var List
. -
Triggers (
on
):workflow_dispatch
enables manual triggering from GitHub Actions tab.push
triggers whenenv-var-list.yml
changes.
-
Jobs:
ubuntu
runs onubuntu-latest
.mac
runs onmacos-latest
.windows
runs onwindows-latest
.
- Steps: Each job runs
printenv | sort
to display environment variables in alphabetical order.
Step 3: Commit and Trigger the Workflow
- After committing, go to the Actions tab in your GitHub repository.
- Select the
Env Var List
workflow. - Click the Run workflow button.
- Choose the
main
branch and click Run workflow to start.
Step 4: View Workflow Results
- Once the workflow completes, open the run details.
- Click on each job (
ubuntu
,mac
,windows
) to view their logs. - Expand the step named Display Environment Variables to see the output for each operating system.
Step 5: (Optional) Use a Matrix Strategy for Parallel Execution
You can simplify the workflow using a matrix to run jobs in parallel on all three OSes:
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
- Replace the
jobs
section with the above. - Commit and rerun the workflow.
Summary
You created a workflow file from scratch, configured jobs on Ubuntu, macOS, and Windows runners, displayed environment variables, triggered workflows manually and on push, and optionally used a matrix strategy for cleaner parallel execution.