Skip to content

Commit

Permalink
Merge pull request #433 from duckpuppy/plan_file_on_destroy
Browse files Browse the repository at this point in the history
[Bugfix] Fixes #417 - Force terraform command to `apply` if a plan file is being used
  • Loading branch information
duckpuppy committed Apr 19, 2022
2 parents b4cec15 + 608d0d3 commit 20765fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- [Issue #432](https://github.com/manheim/terraform-pipeline/issues/432) pass TagPlugin through `-var-file={env}-tags.tfvars`
- [Issue #432](https://github.com/manheim/terraform-pipeline/issues/432) pass TagPlugin through `-var-file={env}-tags.tfvars`
- [Issue #417](https://github.com/manheim/terraform-pipeline/issues/417) DestroyPlugin & PassPlanFilePlugin - Terraform Destroy can't be called with a plan file

# v5.19

Expand Down
9 changes: 8 additions & 1 deletion src/TerraformApplyCommand.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
if (directory && chdir_flag) {
pieces << "-chdir=${directory}"
}
pieces << command
// If we have a plan file, apply is the only command that works.
// Even on destroy, you must use `terraform apply` for a plan file
// created with the `-destroy` argument.
if (planFile) {
pieces << "apply"
} else {
pieces << command
}
if (!input) {
pieces << "-input=false"
}
Expand Down
9 changes: 9 additions & 0 deletions test/TerraformApplyCommandTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ class TerraformApplyCommandTest {
def actualCommand = command.toString()
assertThat(actualCommand, endsWith(" foobar"))
}

@Test
void forcesApplyIfPlanFilePresent() {
def command = new TerraformApplyCommand().withPlanFile("foobar")
.withCommand("destroy")

def actualCommand = command.toString()
assertThat(actualCommand, startsWith("terraform apply"))
}
}

@Nested
Expand Down

0 comments on commit 20765fb

Please sign in to comment.