Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 3

Support for use of environment variables in if expressions

It would be nice to be able to use environment variables in if conditionals.

 

While I can use an if condition in a runs definition, the step will still be marked as run (successfully, perhaps). If we could use if conditionals for the same thing, the step would be marked as ignored.

 

 

2 Replies
Copilot Lvl 3
Message 2 of 3

Re: Support for use of environment variables in if expressions

Any progress on this? When you're reading this documentation it sounds like `env` context is usable but following code still fails.

 

jobs:
  example-job:
    env:
      EVENT_LABEL: example-label
    if: github.event.label.name == env.EVENT_LABEL
    runs-on: ubuntu-latest
    steps:
      ...

 

causes

 

Your workflow file was invalid: The pipeline is not valid. Unrecognized named-value: 'env'. Located within expression: github.event.label.name == env.EVENT_LABEL

 

Setting value directly works like expected:

 

if: github.event.label.name == 'example-label'
Pilot Lvl 1
Message 3 of 3

Re: Support for use of environment variables in if expressions

It might be that job-level "env" context is populated after job-level "if" is tested. It's a long shot, but i'd try if setting EVENT_LABEL at workflow-level "env" would help.

 

If not, you still can get (kinda) wanted behavior by moving check step-level:

check-label:
 runs-on: ubuntu-latest
 env:
LABEL_FOO: foo
LABEL_BAR: bar
LABEL_HELP: needs-help
LABEL_BOOM: user-pc-exploded
steps:

 - run: do-foo-thing
if: github.event.label.name == env.LABEL_FOO

- run: do-bar-thing
if: github.event.label.name == env.LABEL_BAR

- run: post-on-stackoverflow
if: github.event.label.name == env.LABEL_HELP

- run: send-flowers --color random
if: github.event.label.name == env.LABEL_BOOM

 

Most likely not a solution for everyone; it starts workflow, after all, which might be unwanted for few reasons. Packing N different, complex tasks into single job can be meh too. But if various simple tasks share some step(s), it might be nice.