0

So i have a workflow with multiple jobs and environment variables. For example one variable called BRANCH and it's value ${{ github.ref }} .

The workflow has multiple jobs and each with a condition:

development:
name: Deploy to dev
needs: build
if: ${{ needs.build.outputs.environment == 'dev' }}
runs-on: ubuntu-latest

This works fine for each job, but as soon as I apply a second condition unexpected things happens..

development:
name: Deploy to dev
needs: build
if: ${{ needs.build.outputs.environment == 'dev' }} && env.BRANCH == 'loreem ipsum'
runs-on: ubuntu-latest

This always resolvs to true, tested echo it out like:

echo ${{ needs.build.outputs.environment == 'dev' }} && env.BRANCH == 'loreem ipsum'

My initial guess is that it resolvs to <if expression> && true || false But writing like below will not work, as 'env' is unknown to job.if.

if: ${{ needs.build.outputs.environment == 'dev' && env.BRANCH == 'loreem ipsum' }}

I even tried with

if: ${{ needs.build.outputs.environment == 'dev' }} && ${{ env.BRANCH == 'loreem ipsum' }}

So is this expected or is my syntax incorrect?

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45

1 Answers1

0

From the docs the correct way to use the && expression is,

if: ${{ needs.build.outputs.environment == 'dev' && env.BRANCH == 'loreem ipsum' }}

But you might be getting an error saying that env is not available under jobs.development.if. This is because only following contexts are available under jobs.<job_id>.if.

github, needs, inputs

See context availability table for more information.

So the solution is to use the github context(as it's available under jobs.<job_id>.if) and access github.ref direclty to perform the branch comparison.

if: ${{ needs.build.outputs.environment == 'dev' && github.ref == 'loreem ipsum' }}
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
  • Docs says ```&&``` is an AND operator. So since env is not in context the solution would be to output in another step and use it with ```needs```. Branch was just an example of an environment variable – Baked Inhalf Nov 09 '22 at 11:49
  • 1
    @BakedInhalf If that is the case [this will](https://stackoverflow.com/a/67049122/6699447) helps – Abdul Niyas P M Nov 09 '22 at 11:55