runatlantis / atlantis

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

YAML anchor not working #3983

Open WJay-tec opened 10 months ago

WJay-tec commented 10 months ago

Community Note

Overview of the Issue

I am trying to implement yaml anchors with my atlantis.yaml file, but an error shown below is thrown when atlantis plan is ran. parsing atlantis.yaml: version: is required. If you've just upgraded Atlantis you need to rewrite your atlantis.yaml for version 3. See www.runatlantis.io/docs/upgrading-atlantis-yaml.html.

example anchor:

projects:
  - &template
    name: template
    dir: template
    workflow: default
    repo_locking: true
    autoplan:
      enabled: true
      when_modified:
        - "*.tf"
        - ".terraform.lock.hcl"

  - <<: *template
    name: terraform/test/organizations_policies
    dir: terraform/test/organizations_policies

Does using pre_workflow_hooks to modify atlantis.yaml prevents me from using yaml anchors? Cause i am using pre_workflow_hook now to dynamically generate some parts of my atlantis.yaml entries.

Logs

{"level":"error","ts":"2023-11-16T09:28:13.843Z","caller":"events/instrumented_project_command_builder.go:75","msg":"Error building auto plan commands: parsing atlantis.yaml: version: is required. If you've just upgraded Atlantis you need to rewrite your atlantis.yaml for version 3. See www.runatlantis.io/docs/upgrading-atlantis-yaml.html.","json":{},"stacktrace":"github.com/runatlantis/atlantis/server/events.(*InstrumentedProjectCommandBuilder).buildAndEmitStats\n\tgithub.com/runatlantis/atlantis/server/events/instrumented_project_command_builder.go:75\ngithub.com/runatlantis/atlantis/server/events.(*InstrumentedProjectCommandBuilder).BuildAutoplanCommands\n\tgithub.com/runatlantis/atlantis/server/events/instrumented_project_command_builder.go:26\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).runAutoplan\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:85\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).Run\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:290\ngithub.com/runatlantis/atlantis/server/events.(*DefaultCommandRunner).RunAutoplanCommand\n\tgithub.com/runatlantis/atlantis/server/events/command_runner.go:177"}

Environment details

WJay-tec commented 10 months ago

after digging some more, i realise the pre-workflow-hook actually failed, but the logs outputted it as successfully ran.

{"level":"info","ts":"2023-11-17T03:20:43.133Z","caller":"runtime/pre_workflow_hook_runner.go:80","msg":"successfully ran \"sh -c cd terraform/L0/01-accounts\\nterraform init\\nterraform output -json | jq '[.short_name_account_ids.value | to_entries[] | .key]' > /tmp/$HEAD_COMMIT.yaml\\necho '---' | cat - /tmp/$HEAD_COMMIT.yaml > temp && mv temp /tmp/$HEAD_COMMIT.yaml\\necho '#@data/values' | cat - /tmp/$HEAD_COMMIT.yaml > temp && mv temp /tmp/$HEAD_COMMIT.yaml\\n\" in \"/atlantis-data/repos/dbmy/sre/infrastructure/2411/default\"","json":{"repo":"dbmy/sre/infrastructure","pull":"2411"}}{"level":"info","ts":"2023-11-17T03:20:44.179Z","caller":"runtime/pre_workflow_hook_runner.go:80","msg":"successfully ran \"sh -c ytt -f atlantis.yaml -f /tmp/$HEAD_COMMIT.yaml > atlantis2.yaml\\nmv atlantis2.yaml atlantis.yaml\\n\" in \"/atlantis-data/repos/dbmy/sre/infrastructure/2411/default\"","json":{"repo":"dbmy/sre/infrastructure","pull":"2411"}}

When i run the same exact command with the same data locally, i get an error.

ytt: Error: 
- __ytt_tpl3_start_node: expected key 'name' to not be specified again (unless 'yaml/map-key-override' annotation is added)

Feel like the error handling here did not throw the error when the workflow run failed, do correct me if i am wrong. The logs made debugging harder since i thought the workflow run was successful.