Unable to use `env` variable inside of matrix definition

In a larger workflow yml file, the following snippet of code causes an error with my workflow. I can’t figure out why, though. Is there a valid reason for this to cause an error? Since my env setting is at the top level of the workflow, it seems like it should be available for defining a matrix inside of a job inside the workflow.

Can somebody help me fix this, or is it a workflow bug?

name: Test and possibly Publish

on:
  pull_request:
  push:

env:
  default-python: 3.8

jobs:
  tests:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        python-version: [3.6, 3.7, 3.8, 3.9]
        include:
          - os: windows-latest
            python-version: ${{ env.default-python }}
          - os: macos-latest
            python-version: ${{ env.default-python }}

[...]

Github gives me the following error for the line above

Invalid workflow file

The workflow is not valid. 
.github/workflows/on-push.yml (Line: 59, Col: 29): Unrecognized named-value: 'env'. Located at position 1 within expression: env.default-python,
.github/workflows/on-push.yml (Line: 61, Col: 29): Unrecognized named-value: 'env'. Located at position 1 within expression: env.default-python

The documentation on the env context says this:

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

So I’m afraid accessing env variables in a matrix definition is not supported.

Ah ok! Thanks for pointing that out. Everywhere else I tried to use it, it worked, so I didn’t realize it had such limited applicability.

1 Like

It would be nice if it were more generally applicable. It’s nice to use as a “constant” definition at the top of the file, so I don’t have to go chasing down places all throughout the file to change values (and possibly miss some instances where a value needs to be changed.)

1 Like