GitHub Actions by Example: Context Variables

Contexts are collections of variables that are accessible outside of the run commands. You can think of them as variables which can be templated into the workflow file itself. See the full list of contexts here.

name: contexts-example
on:
  push:
  pull:
jobs:
  use-contexts:
    strategy:
      matrix:
        greeting: [Hello, Howdy, Hey]
    runs-on: ubuntu-latest
    steps:
      -
        name: Print greeting

Use the matrix context to print the job’s greeting.

        run: echo $GREETING
        env:
          GREETING: ${{ matrix.greeting }}
      -
        name: Do work with a secret
        run: ./workRequiringASecret.sh

Use a secret from your repository as an env var.

        env:
          A_SECRET: ${{ secrets.A_SECRET }}
      -
        name: Run only for pulls

Contexts can also be part of expressions.

        if: ${{ github.event == "pull" }}
        run: echo "Triggered by a pull request"          

Next example: Context Expressions