Run job if branch (using environment variables)


I’ve got a private repo with multiple branches & a workflow that needs to trigger on push.
The workflow has 2 jobs, build & deploy.
Build job needs to run for each branch on push, however deploy needs to run only for master and release branch.

I’ve defined two envvars for the workflow, PROD_BRANCH & DEV_BRANCH.
I intended to create a condition for the Deploy job as follows:

    runs-on: ubuntu-latest
    if: ${{contains(github.ref, env.PROD_BRANCH) || contains(github.ref, env.DEV_BRANCH)}}
    needs: build

However this yields an error: The workflow is not valid. .github/workflows/build-deploy-glow-codebuild.yaml (Line: 80, Col: 9): Unrecognized named-value: 'env'. Located at position 22 within expression: contains(github.ref, env.PROD_BRANCH) || contains(github.ref, env.DEV_BRANCH)

What I think about it is that the workflow is unable to access an environment variable to evaluate the expression. How do I solve this without explicitly specifying the branch names in the expressions for the conditional? Thanks!

You could either let the build job set an output based on the if conditions you tried or add another job between build and deploy that does this, and then change the condition to e.g. if: fromJSON(needs.<job_id>.outputs.should_run). Note that outputs are always strings, so casting from 'true' to true is necessary unless you use a condition like if: needs.<job_id>.outputs.should_run == 'true'.