Execute job only if env variable is true

Hi,

I’ve been struggling with this for a while now :sweat:

I’ve workflows for a React Native app implemented with Github Actions.

Sometimes I just want to release (beta | production) versions for (android | ios).

As so, I would like to have both jobs conditionally executed depending on the environment variable.

I tried the following according to this:

name: STAGING - CI Workflow

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  build-android:
    env:
      SHOULD_RUN: ${{ secrets.STAGING_RELEASE_ANDROID }}
    if: env.SHOULD_RUN == 'true'
  build-ios:
    env:
      SHOULD_RUN: ${{ secrets.STAGING_RELEASE_IOS }}
    if: env.SHOULD_RUN == 'true'

And the following error pops up:

The workflow is not valid. .github/workflows/staging-workflow.yml (Line: 51, Col: 9): Unrecognized named-value: 'env'. Located at position 1 within expression: env.SHOULD_RUN == 'true',.github/workflows/staging-workflow.yml (Line: 93, Col: 9): Unrecognized named-value: 'env'. Located at position 1 within expression: env.SHOULD_RUN == 'true'

How can I execute the job based on a condition that uses env vars?

According to this document: You can only use the env context in the value of the with and name keys, or in a step’s if conditional. It is not supported to use env context in job’s if conditional.

I was thinking of using job level outputs in job if conditional, but it is not working since the output contain secret. The secrets was protected by GitHub Actions, it is not allowed to include secrets in job output.

Which event do you use to trigger this workflow?
Instead of using secrets, is there any other way for you to determine which version (beta/production) need to release? Such as add the key words into commit message?

1 Like

@ yanjingzhu

Thanks for the reply.

I’m using push and pull_request as the trigger events for the workflow.

Well, I guess I could do that. I’m using conventional-changelog + commitzen for basic commit message formatting.

In this case I guess I could do something like:

name: STAGING - CI Workflow

...

jobs:
  build-android:
    if: contains(github.event.head_commit.message, '(beta)(android)')
    ...
  build-ios:
    if: contains(github.event.head_commit.message, '(beta)(ios)')

right?

Yeah, this idea could work. What I want to mention is that your if conditional is for pull_request event, for push event , you could use

if: contains(toJSON(github.event.commits.*.message), '(beta)(android)')

And your if conditional could update to

if: "contains(github.event.head_commit.message, '(beta)(android)') || contains(toJSON(github.event.commits.*.message), '(beta)(android)')"

Thanks for the reply again.

Just for reference, the syntax

if: contains(github.event.head_commit.message, '(beta)(android)')

did not work. I then dumped all the github context to get a glimpse of the whole thing:

- name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT" 

It turns out that I can use the title property of the pull_request object. Then I replaced my condition with:

if: contains(github.event.pull_request.title, '(beta)(android)') == true

Now it’s working.

:grin:

1 Like