Skip to content

Commit d06a9ba

Browse files
authored
Merge pull request #415 from jleopold28/issue_379
Add chdir argument to support Terraform 0.15+
2 parents ac19d97 + 1587598 commit d06a9ba

14 files changed

+208
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Unreleased
22
- [Issue #422](https://github.com/manheim/terraform-pipeline/issues/422) TerraformPlugin and TerraformPluginVersion should implement TerraformInitCommandPlugin
3+
- [Issue #379](https://github.com/manheim/terraform-pipeline/issues/379) Support Terraform 0.15. Added `-chdir` argument.
4+
- [Issue #395](https://github.com/manheim/terraform-pipeline/issues/395) Support Terraform 1.0. Added `-chdir` argument.
5+
36

47
# v5.18
58

docs/TerraformDirectoryPlugin.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
This plugin allows Terraform to run in a specific directory so that the number of files at the root of any given project can be limited.
44

5-
It works by appending the directory to the end of any Terraform command run by terraform-pipeline. You can either specify a directory when initializing the plugin, or it will default to the `./terraform/` directory.
5+
It works by appending `-chdir=<directory>` before any Terraform sub-command (`init`, `plan`, etc.) run by terraform-pipeline. You can either specify a directory when initializing the plugin, or it will default to the `./terraform/` directory.
6+
7+
### Terraform Version Notes
8+
Previous versions of this plugin would append the directory to the end of the Terraform commands. This has been removed as of Terraform 0.15 and replaced with the `-chdir` argument.
69

710
```
811
// Jenkinsfile

src/TerraformApplyCommand.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
77
private suffixes = []
88
private args = []
99
private String directory
10+
private boolean chdir_flag = false
1011
private Closure variablePattern
1112
private Closure mapPattern
1213
private static plugins = []
@@ -75,17 +76,25 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
7576
return this
7677
}
7778

79+
public TerraformApplyCommand withChangeDirectoryFlag() {
80+
this.chdir_flag = true
81+
return this
82+
}
83+
7884
public String toString() {
7985
applyPlugins()
8086
def pieces = []
8187
pieces += prefixes
8288
pieces << terraformBinary
89+
if (directory && chdir_flag) {
90+
pieces << "-chdir=${directory}"
91+
}
8392
pieces << command
8493
if (!input) {
8594
pieces << "-input=false"
8695
}
8796
pieces += args
88-
if (directory) {
97+
if (directory && !chdir_flag) {
8998
pieces << directory
9099
}
91100

src/TerraformInitCommand.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class TerraformInitCommand implements TerraformCommand, Resettable {
88
private backendConfigs = []
99
private boolean doBackend = true
1010
private String directory
11+
private boolean chdir_flag = false
1112
private static plugins = []
1213
private appliedPlugins = []
1314

@@ -35,6 +36,11 @@ class TerraformInitCommand implements TerraformCommand, Resettable {
3536
return this
3637
}
3738

39+
public TerraformInitCommand withChangeDirectoryFlag() {
40+
this.chdir_flag = true
41+
return this
42+
}
43+
3844
public TerraformInitCommand withBackendConfig(String backendConfig) {
3945
this.backendConfigs << backendConfig
4046
return this
@@ -50,6 +56,9 @@ class TerraformInitCommand implements TerraformCommand, Resettable {
5056
def pieces = []
5157
pieces += prefixes
5258
pieces << terraformBinary
59+
if (directory && chdir_flag) {
60+
pieces << "-chdir=${directory}"
61+
}
5362
pieces << command
5463
if (!input) {
5564
pieces << "-input=false"
@@ -61,7 +70,7 @@ class TerraformInitCommand implements TerraformCommand, Resettable {
6170
} else {
6271
pieces << "-backend=false"
6372
}
64-
if (directory) {
73+
if (directory && !chdir_flag) {
6574
pieces << directory
6675
}
6776

src/TerraformPlanCommand.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class TerraformPlanCommand implements TerraformCommand, Resettable {
77
private suffixes = []
88
private arguments = []
99
private String directory
10+
private boolean chdir_flag = false
1011
private String errorFile
1112
private Closure variablePattern
1213
private Closure mapPattern
@@ -37,6 +38,11 @@ class TerraformPlanCommand implements TerraformCommand, Resettable {
3738
return this
3839
}
3940

41+
public TerraformPlanCommand withChangeDirectoryFlag() {
42+
this.chdir_flag = true
43+
return this
44+
}
45+
4046
public TerraformPlanCommand withArgument(String argument) {
4147
this.arguments << argument
4248
return this
@@ -81,12 +87,15 @@ class TerraformPlanCommand implements TerraformCommand, Resettable {
8187
def pieces = []
8288
pieces = pieces + prefixes
8389
pieces << terraformBinary
90+
if (directory && chdir_flag) {
91+
pieces << "-chdir=${directory}"
92+
}
8493
pieces << command
8594
if (!input) {
8695
pieces << "-input=false"
8796
}
8897
pieces += arguments
89-
if (directory) {
98+
if (directory && !chdir_flag) {
9099
pieces << directory
91100
}
92101

src/TerraformPlugin.groovy

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
6161
// if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) should be used
6262
// here. Unit tests pass with the above, but running Jenkinsfile in a pipeline context
6363
// does not. Debug statements show that the above will return 0 when it should return 'true'.
64-
if ((new SemanticVersion(version) <=> new SemanticVersion('0.12.0')) >= 0) {
64+
if ((new SemanticVersion(version) <=> new SemanticVersion('0.15.0')) >= 0) {
65+
return new TerraformPluginVersion15()
66+
}
67+
else if ((new SemanticVersion(version) <=> new SemanticVersion('0.12.0')) >= 0) {
6568
return new TerraformPluginVersion12()
6669
}
67-
68-
return new TerraformPluginVersion11()
70+
else {
71+
return new TerraformPluginVersion11()
72+
}
6973
}
7074

7175
static void withVersion(String userVersion) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class TerraformPluginVersion15 extends TerraformPluginVersion12 {
2+
3+
public void apply(TerraformValidateCommand command) {
4+
super.apply(command.withChangeDirectoryFlag())
5+
}
6+
7+
public void apply(TerraformInitCommand command) {
8+
super.apply(command.withChangeDirectoryFlag())
9+
}
10+
11+
public void apply(TerraformPlanCommand command) {
12+
super.apply(command.withChangeDirectoryFlag())
13+
}
14+
15+
public void apply(TerraformApplyCommand command) {
16+
super.apply(command.withChangeDirectoryFlag())
17+
}
18+
}

src/TerraformValidateCommand.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TerraformValidateCommand implements Resettable {
55
private prefixes = []
66
private suffixes = []
77
private String directory
8+
private boolean chdir_flag = false
89
private static plugins = []
910
private appliedPlugins = []
1011

@@ -31,16 +32,24 @@ class TerraformValidateCommand implements Resettable {
3132
return this
3233
}
3334

35+
public TerraformValidateCommand withChangeDirectoryFlag() {
36+
this.chdir_flag = true
37+
return this
38+
}
39+
3440
public String toString() {
3541
applyPlugins()
3642
def pieces = []
3743
pieces = pieces + prefixes
3844
pieces << terraformBinary
45+
if (directory && chdir_flag) {
46+
pieces << "-chdir=${directory}"
47+
}
3948
pieces << command
4049
for (String argument in arguments) {
4150
pieces << argument
4251
}
43-
if (directory) {
52+
if (directory && !chdir_flag) {
4453
pieces << directory
4554
}
4655
pieces += suffixes

test/TerraformApplyCommandTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ class TerraformApplyCommandTest {
155155
def actualCommand = command.toString()
156156
assertThat(actualCommand, endsWith(" foobar"))
157157
}
158+
159+
@Test
160+
void addsDirectoryArgumentWithChangeDirectoryFlag() {
161+
def command = new TerraformApplyCommand().withDirectory("foobar")
162+
.withChangeDirectoryFlag()
163+
164+
def actualCommand = command.toString()
165+
assertThat(actualCommand, containsString(" -chdir=foobar"))
166+
}
158167
}
159168

160169
@Nested

test/TerraformInitCommandTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ class TerraformInitCommandTest {
9090
def actualCommand = command.toString()
9191
assertThat(actualCommand, endsWith(" foobar"))
9292
}
93+
94+
@Test
95+
void addsDirectoryArgumentWithChangeDirectoryFlag() {
96+
def command = new TerraformInitCommand().withDirectory("foobar")
97+
.withChangeDirectoryFlag()
98+
99+
def actualCommand = command.toString()
100+
assertThat(actualCommand, containsString(" -chdir=foobar"))
101+
}
93102
}
94103

95104
@Nested

0 commit comments

Comments
 (0)