Skip to content

Commit 48af506

Browse files
authored
Merge pull request #442 from duckpuppy/ignore_vars_with_planfile
Fixes #436 - Omit variables and variable files from apply command when using a plan file
2 parents cfbebd3 + e2ca360 commit 48af506

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build
66
*.iml
77
/out
88
.history
9+
bin/
910
tags
1011
.vscode
1112
shell.nix

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
TerraformTaintPlugin: Support main and master branches
88
- [Issue #432](https://github.com/manheim/terraform-pipeline/issues/432) pass TagPlugin through `-var-file={env}-tags.tfvars`
99
- [Issue #417](https://github.com/manheim/terraform-pipeline/issues/417) DestroyPlugin & PassPlanFilePlugin - Terraform Destroy can't be called with a plan file
10+
- [Issue #436](https://github.com/manheim/terraform-pipeline/issues/436) Bug Fix: Omit variables and variable files from apply command if a plan file is specified
1011

1112
# v5.19
1213

src/TerraformApplyCommand.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
66
private prefixes = []
77
private suffixes = []
88
private args = []
9+
private vars = []
910
private String directory
1011
private String planFile
1112
private boolean chdir_flag = false
@@ -39,7 +40,7 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
3940

4041
public TerraformApplyCommand withVariable(String key, String value) {
4142
def pattern = variablePattern ?: { myKey, myValue -> "-var '${myKey}=${myValue}'" }
42-
this.args << pattern.call(key, value).toString()
43+
this.vars << pattern.call(key, value).toString()
4344
return this
4445
}
4546

@@ -51,7 +52,7 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
5152
}
5253

5354
public TerraformApplyCommand withVariableFile(String fileName) {
54-
this.args << "-var-file=./${fileName}"
55+
this.vars << "-var-file=./${fileName}"
5556
return this
5657
}
5758

@@ -119,6 +120,11 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
119120
pieces << "-input=false"
120121
}
121122
pieces += args
123+
// If we have a plan file, we cannot pass vars, they're already
124+
// baked into the plan.
125+
if (!planFile) {
126+
pieces += vars
127+
}
122128
if (directory && !chdir_flag && !planFile) {
123129
pieces << directory
124130
}

test/TerraformApplyCommandTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,27 @@ class TerraformApplyCommandTest {
245245
def actualCommand = command.toString()
246246
assertThat(actualCommand, startsWith("terraform apply"))
247247
}
248+
249+
@Test
250+
void ignoresVarsIfPlanFilePresent() {
251+
def command = new TerraformApplyCommand().withPlanFile("foobar")
252+
.withVariable("foo", "bar")
253+
.withVariableFile("varfile")
254+
255+
def actualCommand = command.toString()
256+
assertThat(actualCommand, not(containsString("-var")))
257+
assertThat(actualCommand, not(containsString("-var-file")))
258+
}
259+
260+
@Test
261+
void usesVarsIfPlanFileNotPresent() {
262+
def command = new TerraformApplyCommand().withVariable("foo", "bar")
263+
.withVariableFile("varfile")
264+
265+
def actualCommand = command.toString()
266+
assertThat(actualCommand, containsString("-var"))
267+
assertThat(actualCommand, containsString("-var-file"))
268+
}
248269
}
249270

250271
@Nested

0 commit comments

Comments
 (0)