runatlantis / atlantis

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

Auto plan if `atlantis apply` fails #2474

Open nitrocode opened 2 years ago

nitrocode commented 2 years ago

Community Note


Describe the user story

If a plan has 5 resources to create and then an apply creates 4 resources and fails on the 5th, you may be tempted to run another atlantis apply. If you do, it will say that the original plan is stale.

Ran Apply for dir: terraform/redacted workspace: default Apply Error

exit status 1: running "terraform apply -no-color $PLANFILE" in "/atlantis/repos/redacted/redacted/18/default/terraform/redacted": 

Error: Saved plan is stale

The given plan file can no longer be applied because the state was changed by another operation after the plan was created.

Describe the solution you'd like

Instead, when the apply fails, detect the failure, re-plan it and post the new plan, then if another atlantis apply is submitted, it should not fail with a stale plan.

Perhaps we can have a new input?

# atlantis's server.yaml
autoplan_if_apply_fails: true

or a new hook

# repo's atlantis.yaml
version: 3
projects:
- dir: terraform/redacted
  workflow: custom
workflows:
  custom:
    apply:
      # if apply fails
      steps_if_error:
        # repeat the previous plan
        - plan

Describe the drawbacks of your solution

I cannot think of any.

Describe alternatives you've considered

Try to remember to always run atlantis plan or atlantis plan -d xyz after a failed apply so I can run a subsequent atlantis apply

m0ps commented 2 years ago

This feature will be extremely useful when there is a terragrunt monorepo with mocks. That's the only thing that is currently missing to implement a "run-all apply" style workflow.