AI Hints
Home›CI/CD with GitHub Actions Tutorial›Variables and Contexts
CI/CD with GitHub Actions · Lesson 37 of 138

Environment Variables

Settings for steps.

⏱ 2 min read▶ Video at 42:11🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Pipelines need settings. The app's name, a log level, a server address. Environment variables carry those settings into your commands. In a workflow, the env key sets them, at three different levels.

Environment Variables
From the video: Environment Variables
  • Pipelines need settings
  • Environment variables
  • Three levels

Three levels of env

Env at the top applies to every job. Env inside a job applies to its steps, and overrides the top level, so log level becomes debug. And env on a step applies only to that one step. In the commands, you read them like normal shell variables, with a dollar sign.

.github/workflows/env.ymlyaml
name: Env
on: push
env:
  APP_NAME: shop
  LOG_LEVEL: info
jobs:
  show:
    runs-on: ubuntu-latest
    env:
      LOG_LEVEL: debug
    steps:
      - run: echo "$APP_NAME logs at $LOG_LEVEL"
      - name: Step-level value
        env:
          GREETING: Hi from one step
        run: echo "$GREETING, $APP_NAME"
      - run: echo "GREETING is '${GREETING:-not set}'"

Which value wins

The first step sees the app name from the top, and debug, because the job level value wins over the workflow level. The second step has its own greeting. The third step can't see the greeting at all, because it belonged to one step only.

Terminal
run-steps .github/workflows/env.yml
Workflow: Env  (event: push, branch: main)
Job: show
  ✓ Run echo "$APP_NAME logs at $LOG_LEVEL"
      shop logs at debug
  ✓ Step-level value
      Hi from one step, shop
  ✓ Run echo "GREETING is '${GREETING:-not set}'"
      GREETING is 'not set'
  Job succeeded

Summary

Env sets variables at the workflow, job, or step level. The most specific level wins. Next, contexts and expressions.

Key takeaways
  • env: at three levels
  • Most specific wins
  • $NAME in commands
Next up: Contexts and Expressions — ${{ }} explained