How to get current environment name?
Asked Answered
I

2

10

In GitHub Workflow I'm looking for a build-in variable which would contain the name of the current GH deployment environment (e.g. "staging") or be blank/unset if no such thing exist.

Since there doesn't seem to be a build-in variable for it (perhaps I'm overlooking it?), is there another way that this value can be obtained from within a pipeline step?

Here's an example workflow:

jobs:

  job1:
    runs-on: ubuntu-latest
    environment: uat

    steps:
      - name: 'Print name'
        run: echo "GH deployment env : ..."

  job2:
    runs-on: ubuntu-latest
    environment: staging

    steps:
      - name: 'Print name'
        run: echo "GH deployment env : ..."
Idiomorphic answered 10/5, 2023 at 12:59 Comment(6)
You can access environment name from deployment object. Ref How environments relate to deploymentsJusticeship
@Haridarshan: How would that be available to the pipeline? Can you make an example?Idiomorphic
I haven't tried it but I think you can check it by printing the github/job context object like this run: echo '${{ toJSON(github) }}'Justiceship
To be clear, the value needs (in my mind) to either be available as a build-in variable or indirectly in one of the contexts vars. Otherwise I just cannot figure out how I would use it from within a Step.Idiomorphic
Is there a deployment object in your job github context? If I remember correctly, in that case there would be 2 fields environment andoriginal_environment there (edit: I found an example in this workflow run, in the Dump Github Context step): Using event.deployment.environment should return the value in the workflow.Lopeared
The github context (when printed using ${{ toJSON(github) }}) does not have any deployment object, nor can I find any documentation that it is supposed to have that.Idiomorphic
A
2

I've tried different ways, but the only one that actually worked was using the matrix configuration.

https://docs.github.com/en/actions/learn-github-actions/contexts#matrix-context

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: 
  push:
  workflow_dispatch:

jobs:
  Deploy-DEV:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        environment: [DEV]    

    steps:            
      - run: echo "💡 Deploying to ${{ matrix.environment }}."            

  Deploy-QA:
    runs-on: ubuntu-latest
    needs: Deploy-DEV
    strategy:
      matrix:
        environment: [QA]    

    steps:            
      - run: echo "💡 Deploying to ${{ matrix.environment }}." 

Github workflow

Deploying to DEV

Araroba answered 27/3, 2024 at 15:52 Comment(2)
Thanks. I haven't tried your suggestion, but you've documented it well. Kudos. I'll accept your answer.Idiomorphic
Actually that does not work at all because it disable the functionality of the job.environment. matrix.environment is just a field value without any special meaning.Tva
T
0

I created an action for that use case https://github.com/qoomon/actions--context

Usage Example

jobs:
  example:
    runs-on: ubuntu-latest
    environment: playground
    permissions:
      actions:     read  # required for qoomon/actions--context action
      deployments: read  # required for qoomon/actions--context action
      contents: read
    steps:
      - uses: qoomon/actions--context@v1
        id: context

      - run: |
          echo "Current Environment: ${{ steps.context.outputs.environment }}"
          echo "Job Logs: ${{ steps.context.outputs.job_log_url }}"
Tva answered 2/8, 2024 at 15:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.