Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
deepakgc opened this issue Feb 26, 2021 · 9 comments
Closed
Labels
bug Something isn't working

Comments

@deepakgc
Copy link

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

@deepakgc deepakgc added the bug Something isn't working label Feb 26, 2021
@sqljim
Copy link

sqljim commented Mar 28, 2021

There are a couple of outstanding SO questions that relate to this too:

https://stackoverflow.com/questions/66830251/dynamically-setting-the-environment-context-in-github-actions?noredirect=1#comment118150164_66830251

https://stackoverflow.com/questions/65826284/use-dynamic-input-value-for-environment-in-github-actions-workflow-job

At the moment I'm working around it by using an IF statement:

jobs:

  deploy-develop:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    if: ${{ github.ref == 'refs/heads/develop' }}

    environment: develop

    - steps ........


    deploy-main:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    if: ${{ github.ref == 'refs/heads/main' }}

    environment: main

    - steps ........

But this of course means duplicating most of the code in the workflow.

@yaananth
Copy link
Contributor

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.

@cornwe19
Copy link

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.

@RevoluPowered
Copy link

RevoluPowered commented Mar 26, 2023

This issue is causing me to have to duplicate workflow files pointlessly.
I want to do environment: ${GITHUB_REF##*/}

@KodDobra
Copy link

KodDobra commented Mar 26, 2023 via email

ritave pushed a commit to MetaMask/snaps-registry that referenced this issue Mar 29, 2023
It's not possible to set the env using input variables - actions/runner#998
islami00 added a commit to chocolatenetwork/choc-js that referenced this issue Apr 23, 2023
@philomory
Copy link

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.

You already can, you just have to explicitly write it as

environment:
  name: ${{ some.dynamic.expression }}

... rather than environment: ${{ some.dynamic.expression }}, but, the results are exactly the behavior you're asking for.

@felipemouradev
Copy link

environment:
  name: ${{ some.dynamic.expression }}

do you have some example ?

@jdschleicher
Copy link

i have also had some luck without leveraging the name property using the "format" expression:

    environment: "${{ format('{0}{1}{2}', needs.get-parent-by-tag.outputs.parent_name, '_', 'child_one_environment') }}"

will finish as "dynamicValue_child_one_environment"

@Zambonilli
Copy link

environment:
  name: ${{ some.dynamic.expression }}

do you have some example ?

This worked for me.

on:
  workflow_dispatch:
    inputs:
      environment:
        required: true
        type: environment
jobs:
  deploy:
name: Deploy
    environment:
      name: ${{ inputs.environment }}          

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants