How to access env variable in "contains" arguments

Is there a way to access env variables inside contains arguments in an if statement? Something like:

name: Ubuntu

on: [push]

env:
  MSG: ${{ github.event.head_commit.message }}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - if: contains(${{ MSG }}, "ci skip")
        run: echo $MSG

That should work using the env context, like ${{ env.MSG }}.

Here’s the error I’m getting

The workflow is not valid. .github/workflows/ci-ubuntu.yml (Line: 11, Col: 9): Unrecognized named-value: 'MSG'. Located at position 1 within expression: MSG

This should work, you have to use single quotes for the string - see https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#literals:

name: Ubuntu

on: [push]

env:
  MSG: ${{ github.event.head_commit.message }}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - if: contains(env.MSG, 'ci skip')
        run: echo $MSG
name: Ubuntu

on: [push]

env:
  MSG: ${{ github.event.head_commit.message }}

jobs:
  build:

    runs-on: ${{ matrix.operating-system }}
    if: contains(env.MSG, 'ci skip')

I get the following error:
The workflow is not valid. .github/workflows/ci-ubuntu.yml (Line: 14, Col: 9): Unrecognized named-value: 'env'. Located at position 10 within expression: contains(env.MSG, 'ci skip')

@ma-sadeghi,

According to the description from the docs about the env context:

You can only use the env context in the value of the with and name keys, or in a step’s if conditional.

You can’t use the env context in a job’s if conditional.
You can directly use the github context in the job’s if conditional.

jobs:
  build:
    . . .
    runs-on: ${{ matrix.operating-system }}
    if: contains(github.event.head_commit.message, 'ci skip')
1 Like

For anyone else stumbling on this that was as confused as I was, and going round in circle’s, the key part of “in a step’s if conditional” is the word “step”.

So you can use env variables in a “step’s if conditional” but not the higher level “jobs if conditional” (to skip the whole action for example).

Wasted far too much time on that!!

2 Likes