diff --git a/CHANGELOG.md b/CHANGELOG.md index 2188518f..a9213b7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/TerraformApplyCommand.groovy b/src/TerraformApplyCommand.groovy index 9aea198c..661d08d9 100644 --- a/src/TerraformApplyCommand.groovy +++ b/src/TerraformApplyCommand.groovy @@ -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" } diff --git a/test/TerraformApplyCommandTest.groovy b/test/TerraformApplyCommandTest.groovy index 93311719..ff93d793 100644 --- a/test/TerraformApplyCommandTest.groovy +++ b/test/TerraformApplyCommandTest.groovy @@ -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