Interview Prep/GitHub Actions/Contexts, Expressions, Env & Outputs

How do you set an environment variable whose value depends on the branch?

BeginnerConceptContexts, Expressions, Env & Outputs
Answer

Either an expression at job or step level:

env:
  TARGET_ENV: ${{ github.ref_name == 'main' && 'staging' || 'dev' }}

or compute in a step and write to $GITHUB_ENV:

case "$GITHUB_REF_NAME" in
  main) echo "TARGET_ENV=staging" >> "$GITHUB_ENV" ;;
  *)    echo "TARGET_ENV=dev" >> "$GITHUB_ENV" ;;
esac
All GitHub Actions questions Roadmap: GitHub Actions

More on Contexts, Expressions, Env & Outputs