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

Default Variables

Set for free.

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

Every step also gets a set of environment variables for free. They start with GITHUB underscore, and hold the same facts as the github context. They're handy inside scripts, where expressions aren't available.

Default Variables
From the video: Default Variables
  • Free variables in every step
  • GITHUB_ names
  • Useful in scripts

Listing them

This step lists the default variables. The branch name, the event, the actor, the repository, and the run number. And CI is set to true, which many tools check, to behave differently in a pipeline, like turning off colours and prompts.

Terminal
run-steps .github/workflows/defaults.yml
Workflow: Defaults  (event: push, branch: main)
Job: show
  ✓ Run env | grep -E "^(GITHUB_(REF_NAME|EVENT_NAME|ACTOR|REPOSITORY|RUN_NUMBER)|CI|RUNNER_OS)=" | sort
      CI=true
      GITHUB_ACTOR=sam
      GITHUB_EVENT_NAME=push
      GITHUB_REF_NAME=main
      GITHUB_REPOSITORY=sam/shop
      GITHUB_RUN_NUMBER=1
      RUNNER_OS=Linux
  Job succeeded

Expression or variable?

Both give the same information, but they're used in different places. Expressions are replaced before the step runs, and work anywhere in the YAML, including if conditions. Environment variables only exist while the command runs, and are perfect inside shell scripts and programs.

${{ github.ref_name }}

  • Replaced before running
  • Works anywhere in YAML
  • Needed for if:

$GITHUB_REF_NAME

  • Read while running
  • Inside scripts
  • Any program can read it

Summary

GitHub sets GITHUB_variables, and CI equals true, in every step. Use expressions in YAML, and variables in scripts. Next, keeping passwords safe with secrets.

Key takeaways
  • GITHUB_SHA, GITHUB_REF_NAME
  • CI=true
  • Expressions vs variables
Next up: Secrets — passwords stay hidden