Conditional Expression Syntax

I’m looking for the ability to use a conditional within an expression.  For example, if the github.head_ref starts with a certain prefix, I would like to change an environment variable for every job in the workflow.  (Yes, I could make two workflows, but since GitHub Actions / YAML in general doesn’t support including other YAML files, well, code duplication sucks no matter where it is…)

Something like:

env:
  CONTAINER: ${{ if(!startsWith(github.head_ref, "special/"), "org/acct:latest", "org/acct:special") }}

My use case is that depending on the head_ref, or perhaps on the content of the pull request content, I would enable a different behavior in the workflow through an environment variable (for example choosing a different docker container to use over the default).

What I’d really like to see is jinja2 expressions allowed in the YAML though.  That would allow for all sorts of flexible behavior that doesn’t look like an excel function.

2 Likes

For top level env, which is used for all jobs in the workflow, it doesn’t accept ‘if’ expression.  You cannot set conditional expression for it.

As an alternative, you can set if expression in ‘job’ level. It will exclude ‘conditional2’ jobs below:

Or you can set in step level.

Regarding the jinja2 expressions, it’s recommended to raise your idea here where github actions manager will take a review. Thanks.

3 Likes

Thanks, using ::set-env allows me to conditionally set them based on shell logic.

1 Like