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

Contexts and Expressions

${{ }} explained.

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

Dollar, double curly braces, is an expression. GitHub works it out before the step runs, and replaces it with a value. Expressions can read contexts, which are groups of information about the run, like github, env, runner, and secrets.

Contexts and Expressions
From the video: Contexts and Expressions
  • Dollar, double braces
  • Worked out before the step
  • Contexts hold the facts

Reading contexts

Github dot event name, and github dot actor, tell us what started the run, and who. Github dot repository is the owner and name. Runner dot o s is the operating system. Expressions also have functions. Format fills in a template, and starts with is used here in an if condition.

.github/workflows/contexts.ymlyaml
name: Contexts
on: push
jobs:
  show:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Event ${{ github.event_name }} by ${{ github.actor }}"
      - run: echo "Repo ${{ github.repository }} on ${{ runner.os }}"
      - run: echo "${{ format('Hello {0}, from {1}', github.actor, runner.os) }}"
      - if: startsWith(github.ref_name, 'release/')
        run: echo "This is a release branch"

The values

Here's the run. The event, the actor, the repository, and the operating system are all filled in. On main, the release step is skipped. Run again as a release branch, with dash dash ref, and starts with is true, so the step runs.

Terminal
run-steps .github/workflows/contexts.yml
Workflow: Contexts  (event: push, branch: main)
Job: show
  ✓ Run echo "Event push by sam"
      Event push by sam
  ✓ Run echo "Repo sam/shop on Linux"
      Repo sam/shop on Linux
  ✓ Run echo "Hello sam, from Linux"
      Hello sam, from Linux
  - Run echo "This is a release branch"  (skipped)
  Job succeeded
run-steps .github/workflows/contexts.yml --ref release/2.0 | tail -3
  ✓ Run echo "This is a release branch"
      This is a release branch
  Job succeeded

Summary

Expressions in double curly braces read contexts like github and runner, and can call functions like starts with. Next, the variables GitHub sets for you.

Key takeaways
  • ${{ github.actor }}
  • ${{ runner.os }}
  • startsWith(), format()
Next up: Default Variables — set for free