Sikau

GitHub Actions · Day 6 · about 35 min

Variables, contexts and conditions

env at three levels, github context, if, and $GITHUB_ENV.

Workflows that adapt

The same workflow behaves differently on main versus a pull request, or for a tag. contexts (github, env, matrix, steps, needs…) expose facts, expressions combine them, and if: decides whether a step or job runs.

env, contexts, if

name: Contexts
on: [push, pull_request]
env:
  APP: cafe-api
jobs:
  info:
    runs-on: ubuntu-latest
    env:
      STAGE: ${{ github.ref_name == 'main' && 'production' || 'preview' }}
    steps:
      - run: echo "$APP on $STAGE (event ${{ github.event_name }})"
      - name: Only on main
        if: github.ref == 'refs/heads/main'
        run: echo "This is main"
      - name: Only on pull requests
        if: github.event_name == 'pull_request'
        run: echo "PR #${{ github.event.pull_request.number }}"
      - run: echo "BUILD_ID=build-${{ github.run_number }}" >> "$GITHUB_ENV"
      - run: echo "Later steps see $BUILD_ID"
        env:
          GREETING: hi

The rest of this lesson

  • Expressions and functionsexplain
  • Passing values between stepsexplain
  • Which steps run on a PR?predict
  • Which context?match
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 12 lessons of GitHub Actions