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
More on Contexts, Expressions, Env & Outputs
List the available contexts and what each holds.BeginnerWhich contexts can you use in which keys? Give two examples that trip people up.AdvancedWhat are the expression operators and how do comparisons coerce types?IntermediateHow do you write a ternary in GitHub Actions expressions?IntermediateWhat do `contains`, `startsWith`, `endsWith`, `format` and `join` do?BeginnerWhat are object filters (`.*`) and how are they useful?Intermediate