Can't access the env object from job.if

hello everyone, just wondering if this is a bug or not, and if so where should I open the issue.

I have a workflow defined as such

name: Nodebrick

on:  
# Trigger the workflow on push or pull request,  
 # but only for the master branch  
push:  
 paths:  
 - '!.ci/\*\*'  
- '!.docker/\*\*'  
- '!.github/\*\*'  
- '!documentation/\*\*'  
- 'packages/\*\*/src/\*\*'  
- 'packages/\*\*/tests/\*\*'  
- 'packages/nodebrick-cli/assets/\*\*'  
- 'packages/\*\*/package.json'  
  
env:  
 PUBLISH: false

jobs:  
  
 test\_job:  
  
 runs-on: ubuntu-latest

steps:  
 - name: Checkout Repository
uses: actions/checkout@v2

- name: Run test suite
run: |
yarn test:all

- name: Checking changed paths for source
id: source-changed
uses: marceloprado/has-changed-path@master
with:  
 paths: packages/ **/src/** packages/nodebrick-cli/assets/ **packages/** /package.json

- if: steps.source-changed.outputs.changed == 'true'
name: set environment variables
run: |
echo "::set-env name=PUBLISH::true"

# this will only run if files in those paths are changed - packages/\*\*/src/\*\* packages/nodebrick-cli/assets/\*\* packages/\*\*/package.json  
publish\_job:  
  
 if: env.PUBLISH == 'true' && github.ref == 'master'

env: # Or as an environment variable  
GH\_TOKEN: ${{ secrets.GITHUB_TOKEN }}

runs-on: ubuntu-latest

steps:  
 - name: Checkout Repository
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v1
with:  
 node-version: '12.x'  
registry-url: 'https://registry.npmjs.org'  
  
- name: Add node_modules/.bin to path
run: |
echo "::add-path::$RUNNER_WORKSPACE/nodebrick/node_modules/.bin"

- name: Deploying to NPM registry
env:  
 NODE\_AUTH\_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: |
yarn config set workspaces-experimental true
yarn
.ci/scripts/publish.sh
shell: bash

it complains saying it can’t find the env object

Error evaluating expression 'env.PUBLISH == 'true' && github.ref == 'master'' - ReferenceError: 'env' is not defined

If I move this if in a step it doesn’t complain.

Have I missed something obvious here?

Reading the doc I can see that workflow/job/step and read/write env.
Any help would be greatly appreciated.

code here: https://github.com/nodebrick/nodebrick

Thanks

2 Likes

I am afraid I need to tell you that using env context in job.if is not supported. Please refer to this document: 

https://help.github.com/en/actions/reference/contexts-and-expression-syntax-for-github-actions#env-context 

I would encourage you to combine test_job and publish_job to one job. And add if condition to all the publish steps. 

I know this is not an ideal solution, you could ask for supporting use env context in job if condition in the Feedback form for GitHub Actions.

1 Like