Using environment vars in job returns in empty strings
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
Fix Environment Variable Access in GitHub Actions Jobs
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
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.
yamlMY_VAR: 'value' - 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'.
yamlruns-on: ${{ secrets.MY_VAR }} - 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.
yamlsteps: - name: Check MY_VAR run: echo "MY_VAR is ${{ secrets.MY_VAR }}" - 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
Alex Chen
2450 rep