Cannot access env vars in `with` of shared-workflow invocation?

740 Views Asked by At

I need to maintain a default that will be used for PRs and workflow_dispatch. Following is the pattern I'm trying to use, but I get an error when trying to access the env.var. How am I going wrong here and how else might I achieve the same result?

Error:

The workflow is not valid. .github/workflows/calling-workflow.yml (Line: 18, Col: 26): Unrecognized named-value: 'env'. Located at position 29 within expression: inputs.checkFoldersArray || env.checkFoldersArray

calling-workflow.yml

on:
  pull_request:

  workflow_dispatch:
    inputs:
      checkFoldersArray:
        description: 'The working directory to run the tests in'
        required: false
        default: (".")
env:
  checkFoldersArray: (".")

jobs:
  pr-merge-checks:
    uses: <owner>/<repo>/.github/workflows/called-workflow.yml
    with:
      checkFoldersArray: ${{ inputs.checkFoldersArray || env.checkFoldersArray }}

I believe if solved, I beleve this would represent an answer for Combine dynamic Github Workflow matrix with input values and predefined values.

1

There are 1 best solutions below

0
On

One solution I've come up with is to declare the env as your default value, then have a job that can output the ${{ a || b }} value, or however you need to apply a default vs input.

In this example, you can dispatch with a false value and PRs will use the default of true.

on:
  pull_request:

  workflow_dispatch:
    inputs:
      someBooleanFlag:
        description: 'Must be true or false. Default is true'
        required: false
        type: boolean
        default: true

env:
  someBooleanFlag: true

jobs:
  setup-env:
    outputs:
      someBooleanFlag: ${{steps.setup-env.outputs.someBooleanFlag}}
    steps:
      - name: Setup Environment
        id: setup-env
        run: |
          echo "::set-output name=someBooleanFlag::${{inputs.someBooleanFlag || env.someBooleanFlag}}";

  pr-merge-checks:
    needs: setup-env
    uses: <owner>/<repo>/.github/workflows/called-workflow.yml
    with:
      someBooleanFlag: ${{needs.setup-env.outputs.someBooleanFlag}}