question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting job environment dynamically doesn't work when using key-value format

See original GitHub issue

Describe the bug I was trying to set the job environment dynamically so I can use my GitHub production & staging environment secrets in a single workflow.

To Reproduce Steps to reproduce the behavior:

name: Set Job Enviroment Dynamically 
on:
  workflow_dispatch:
jobs:
  set_environment:
    outputs:
      my_env: ${{ steps.setenv.outputs.my_env }}
    runs-on: ubuntu-latest
    steps:
    - id: setenv
      run: echo "::set-output name=my_env::production"

  use_outputs_environemnt_directly:    
    needs: set_environment
    environment: ${{ needs.set_environment.outputs.my_env }}
    runs-on: ubuntu-latest
    steps:
    - name: Testing job environment
      run: echo "Hello From Production Envrionment." 

See failing workflow: failure.yaml

Expected behavior The environment should be set to production.

What’s not working?

I get the below error: The workflow is not valid. .github/workflows/job-environment-test-ko.yml (Line: 15, Col: 18): Unrecognized named-value: 'needs'. Located at position 1 within expression: needs.set_environment.outputs.en

Workaround found

Using object format for jobs environment fixes the issue

 environment: 
      name: ${{ needs.set_environment.outputs.my_env }}

See successful workflow: success.yaml

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
cornwe19commented, Nov 30, 2022

Will being able to dynamically specify environment name be added at any point? This makes selecting an environment from a workflow_dispatch event a lot more complicated.

25reactions
yaananthcommented, Mar 30, 2021

Thanks for the report!

We support expressions only in https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-using-environment-name-and-url with mappings.

environment: blah exists for short form notations, and it doesn’t support contexts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use dynamic input value for Environment in GitHub Actions ...
I'm trying to make a GitHub Actions workflow where one job has a dynamic value to its environment setting. https://docs.github.com/en/actions/ ...
Read more >
Environment Variables section not showing in Dynamic Content
I'm testing using environment variables in a Flow and when I try to reference the environment variable in a Flow, the "Environment.
Read more >
Artifacts Filename Cannot Be Set with Dynamic Variables
It looks like custom variables are not being exposed correctly to the job resulting in the wrong artifact file name. Do you know...
Read more >
Environment Variables: What They Are and How To Use ...
Environment variables are used to store app secrets and configuration data, which are retrieved by your running app when needed.
Read more >
Dynamic body with dynamic variables - Just getting started
Hello, I need to send a dynamic body depending on the environment where I'm working, but since the amount of things that will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found