runatlantis / atlantis

Terraform Pull Request Automation
https://www.runatlantis.io
Other
7.85k stars 1.06k forks source link

Why Pre Workflow Hooks does not have WORKSPACE environment variable? #4927

Open hakuno opened 2 months ago

hakuno commented 2 months ago

Community Note


Describe the user story

As a Solutions Architect, I'd like to filter some commands (plan, apply etc) by workspace, but the docs say it doesn't have the WORKSPACE environment variable right there. Is it true?

Describe the solution you'd like

I'd like the WORKSPACE environment variable to be available in Pre Workflow Hooks.

aarozhkov commented 1 month ago

+1. Each workspace represents separate env and we need to use separate set of Creds(scoped) for various TF providres. Like mongo atlas, auth0 and etc. I want to use some identification of workspace within pre-hook which will set proper env variables.

IuryAlves commented 13 hours ago

Pre-workflow hooks run before Terraform is initialised, so no workspace setting is available.

Wouldn't customizing the workflows solve this problem? Example:

version: 3
projects:
  - name: dev
    dir: .
    workflow: dev
    workspace: dev
    apply_requirements: [mergeable, approved]
  - name: prod
    dir: .
    workflow: prod
    workspace: prod
    apply_requirements: [mergeable, approved]
workflows:
  dev:
    plan:
      steps:
      - init:
          extra_args:
            - "-upgrade"
      - plan:
          extra_args:
            - "-var"
            - "aws_role_arn='arn:aws:iam::<DEV-ACCOUNT-ID>:role/TerraformDeployer'"
            - "-var"
            - "env=dev"
  prod:
    plan:
      steps:
      - init:
          extra_args:
            - "-upgrade"
      - plan:
          extra_args:
            - "-var"
            - "aws_role_arn='arn:aws:iam::<PROD-ACCOUNT-ID>:role/TerraformDeployer'"
            - "-var"
            - "env=prod"
hakuno commented 9 hours ago

@IuryAlves

so no workspace setting is available

Actually it has. Remember it inherits that from projects block (in Yaml manifest) like you've already shown:

projects:
  - name: dev
    dir: .
    workflow: dev
    workspace: dev

We just need to retrieve the workspace: dev back.

IuryAlves commented 1 hour ago

The pre-workflow-hooks are defined at the repo level though, not at project level.

What if you create a custom workflow and add a run step:

version: 3
projects:
  - name: dev
    dir: .
    workflow: dev
    workspace: dev
    apply_requirements: [mergeable, approved]
workflows:
  dev:
    plan:
      steps:
      - init:
          extra_args:
            - "-upgrade"
      - run: echo $WORKSPACE # do something with workspace
      - plan:
          extra_args:
            - "-var"
            - "aws_role_arn='arn:aws:iam::<DEV-ACCOUNT-ID>:role/TerraformDeployer'"
            - "-var"
            - "env=dev"

@hakuno

IuryAlves commented 1 hour ago

Or maybe I misunderstood your use-case. Can you clarify it? What do you mean by filter some commands (plan, apply etc) by workspace?