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