FG
💻 Software☁️ Cloud & DevOpsMicrosoft

Using environment vars in job returns in empty strings

Fresh3 days ago
Mar 14, 20260 views
Confidence Score73%
73%

Problem

I'm trying to build a reusable workflow that dynamically picks the runner depending on the job environment. I'm triggering the job with a workflow dispatch with environment as an input, setting the environment of a job as "environment: ${{ inputs.environment }}" (but even if i hard code the environment it wont work). But the problem I have seems to be a bug with using environment vars at the job level. For example ${{ vars.MY_VAR }} always returns an empty string. Unless I add the variable as a repository variable instead of an environment variable. If I am to echo out ${{ vars.MY_VAR }} inside a step, the variable is there, but not when I'm trying to configure a job. To Reproduce Follow the example from the documentation. https://docs.github.com/en/actions/learn-github-actions/contexts#vars-context Setting a workflow environment variable works fine. But setting for example the job name or runs-on in a job would result in an empty string. Echoing the vars in a step works fine. Expected behavior Using vars.RUNNER (as in the example) would set the runner configured for that environment in the repository settings. Found some similar reports on this but not quite. Have been trying out different workarounds and gone through the documentation a few times now but none will work.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix Environment Variable Access in GitHub Actions Jobs

Medium Risk

GitHub Actions does not allow the use of environment variables defined in the 'vars' context at the job level for certain properties like 'runs-on' or job name. This is a limitation in the current implementation of GitHub Actions, where job-level properties cannot dynamically reference environment variables directly.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define Repository Variables

    Instead of using environment variables, define the required variables as repository variables. This allows them to be accessed at the job level without returning empty strings.

    yaml
    MY_VAR: 'value'
  2. 2

    Update Job Configuration

    Modify the job configuration to use the repository variables directly. For example, set the 'runs-on' property using the repository variable instead of trying to use 'vars'.

    yaml
    runs-on: ${{ secrets.MY_VAR }}
  3. 3

    Use Steps to Echo Variables

    If you still need to reference the environment variables within the job, use a step to echo the variable to confirm its value. This ensures that the variable is set correctly before proceeding with the job execution.

    yaml
    steps:
      - name: Check MY_VAR
        run: echo "MY_VAR is ${{ secrets.MY_VAR }}"
  4. 4

    Test the Workflow

    Run the workflow with the updated configuration to ensure that the job executes successfully and the variables are correctly referenced.

Validation

Confirm that the job runs successfully and that the 'runs-on' property is set to the expected runner. Additionally, verify that the echoed variable outputs the correct value.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

github-actionsci-cdrunnerbugawaiting-customer-response