How do I refer to env context in a workflow file?

I’m banging my head against the desk trying to figure out the correct syntax for my workflow YAML. I am using a container on a self-hosted runner and I would like to specify the hostname for the container. This works:

jobs:
  myjob:
    runs-on: [ self-hosted, ubuntu, dataplatform ]
    container:
      image: image:tag
      options: --hostname myhostname

However I want to specify it dynamically by setting hostname to the value of GITHUB_RUN_NUMBER and I can’t figure out how to do it.

This:

jobs:
  myjob:
    runs-on: [ self-hosted, ubuntu, dataplatform ]
    container:
      image: image:tag
      options: --hostname $GITHUB_RUN_NUMBER

just sets hostname to the literal value “$GITHUB_RUN_NUMBER”, here is what gets returned from running docker inspect on the container

image

So I tried this instead:

jobs:
  myjob:
    runs-on: [ self-hosted, ubuntu, dataplatform ]
    container:
      image: image:tag
      options: --hostname ${{ env.GITHUB_RUN_NUMBER }}

but that just caused an error when attempting to start the workflow:

[ Invalid workflow file : .github/workflows/demo.yml#L29]
The workflow is not valid. .github/workflows/demo.yml (Line: 29, Col: 16): Unrecognized named-value: ‘env’. Located at position 1 within expression: env.GITHUB_RUN_NUMBER

Please can someone tell me the correct syntax to refer to env vars in jobs.<job_id>.container.options?

You don’t. The env context isn’t available outside steps:

You can use the env context in the value of any key in a step except for the id and uses keys.

got it, thank you. I have gotten around this by providing the GITHUB_RUN_NUMBER as an output from a previous job:

  github-run-number:
    name: Provide GITHUB_RUN_NUMBER
    outputs:
      github-run-number: ${{ steps.github-run-number.outputs.github-run-number }}
    steps:
      - id: github-run-number
        run: |
          echo "::set-output name=github-run-number::$GITHUB_RUN_NUMBER"
1 Like