How to get the Current Job Environment Name?

In the example below, how would I get the name of the current environment. My only reason for doing this is to keep the code DRY.

jobs:
  deploy-to-test:
    name: "Terraform deploy to test"
    runs-on: ubuntu-latest
    environment: test
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1
        with:
          cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
          
      - name: Terraform Workspace
        id: workspace
        run: echo "TF_WORKSPACE=${{ job.environment.name }}" >> $GITHUB_ENV

${{ job.environment.name }} doesn’t seem to contain anything and I can’t see it in the list of env variables either?

1 Like

jobs.<job_id>.name is mentioned in:

https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#context-availability

you could also set a custom variable to keep it DRY:

To set custom environment variables, you need to specify the variables in the workflow file. You can define environment variables for a step, job, or entire workflow using the jobs.<job_id>.steps[*].env , jobs.<job_id>.env , and env keywords. For more information, see “Workflow syntax for GitHub.”