Skip to content

Commit 198c029

Browse files
authored
Merge pull request #345 from duckpuppy/add_plan_only_param
Issue 344: Add PLAN_ONLY parameter to PlanOnlyPlugin
2 parents 21188a3 + a9d931a commit 198c029

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
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

3+
* [Issue #344](https://github.com/manheim/terraform-pipeline/issues/344) Add PLAN_ONLY parameter to PlanOnlyPlugin
4+
* **BREAKING CHANGE** This change is a breaking change. Prior to this update, applying the PlanOnlyPlugin would restrict the pipeline to only running `terraform plan`. This update changes behavior to simply providing a `PLAN_ONLY` boolean parameter that can be set to restrict the build behavior. It defaults to `false`.
5+
36
# v5.15
47

58
* [Issue #172](https://github.com/manheim/terraform-pipeline/issues/172) Feature: ConditionalApplyPlugin - can allow apply for specific environments on all branches/PRs.

docs/PlanOnlyPlugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## [PlanOnlyPlugin](../src/PlanOnlyPlugin.groovy)
22

3-
Enable this plugin to change pipeline functionality to `terraform plan`.
3+
Enable this plugin to add a parameter to the build which will restrict pipeline functionality to `terraform plan` only.
44

55
```
66
// Jenkinsfile

src/PlanOnlyPlugin.groovy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class PlanOnlyPlugin implements TerraformEnvironmentStagePlugin, TerraformPlanCo
66
public static void init() {
77
PlanOnlyPlugin plugin = new PlanOnlyPlugin()
88

9+
BuildWithParametersPlugin.withBooleanParameter([
10+
name: "PLAN_ONLY",
11+
description: 'Run `terraform plan` only, skipping `terraform apply`.'
12+
])
13+
914
BuildWithParametersPlugin.withBooleanParameter([
1015
name: "FAIL_PLAN_ON_CHANGES",
1116
description: 'Plan run with -detailed-exitcode; ANY CHANGES will cause failure'
@@ -23,8 +28,10 @@ class PlanOnlyPlugin implements TerraformEnvironmentStagePlugin, TerraformPlanCo
2328

2429
@Override
2530
public void apply(TerraformEnvironmentStage stage) {
26-
stage.decorateAround(CONFIRM, skipStage(CONFIRM))
27-
stage.decorateAround(APPLY, skipStage(APPLY))
31+
if (Jenkinsfile.instance.getEnv().PLAN_ONLY == 'true') {
32+
stage.decorateAround(CONFIRM, skipStage(CONFIRM))
33+
stage.decorateAround(APPLY, skipStage(APPLY))
34+
}
2835
}
2936

3037
@Override

test/PlanOnlyPluginTest.groovy

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class PlanOnlyPluginTest {
2626
}
2727

2828
@Test
29-
void addsParameter() {
29+
void addsFailPlanOnChangesParameter() {
3030
PlanOnlyPlugin.init()
3131

3232
def parametersPlugin = new BuildWithParametersPlugin()
@@ -39,23 +39,50 @@ class PlanOnlyPluginTest {
3939
description: 'Plan run with -detailed-exitcode; ANY CHANGES will cause failure'
4040
]))
4141
}
42+
43+
@Test
44+
void addsPlanOnlyParameter() {
45+
PlanOnlyPlugin.init()
46+
47+
def parametersPlugin = new BuildWithParametersPlugin()
48+
Collection actualParms = parametersPlugin.getBuildParameters()
49+
50+
assertThat(actualParms, hasItem([
51+
$class: 'hudson.model.BooleanParameterDefinition',
52+
name: "PLAN_ONLY",
53+
defaultValue: false,
54+
description: 'Run `terraform plan` only, skipping `terraform apply`.'
55+
]))
56+
}
4257
}
4358

4459
@Nested
4560
public class Apply {
4661

4762
@Test
48-
void decoratesTheTerraformEnvironmentStage() {
63+
void decoratesTheTerraformEnvironmentStageWhenPlanOnlyTrue() {
4964
PlanOnlyPlugin plugin = new PlanOnlyPlugin()
5065
def environment = spy(new TerraformEnvironmentStage())
66+
MockJenkinsfile.withEnv('PLAN_ONLY': 'true')
5167
plugin.apply(environment)
5268

5369
verify(environment, times(1)).decorateAround(eq(TerraformEnvironmentStage.CONFIRM), any(Closure.class))
5470
verify(environment, times(1)).decorateAround(eq(TerraformEnvironmentStage.APPLY), any(Closure.class))
5571
}
5672

5773
@Test
58-
void addsArgumentToTerraformPlan() {
74+
void doesNotDecorateTheTerraformEnvironmentStageWhenPlanOnlyTrue() {
75+
PlanOnlyPlugin plugin = new PlanOnlyPlugin()
76+
def environment = spy(new TerraformEnvironmentStage())
77+
MockJenkinsfile.withEnv('PLAN_ONLY': 'false')
78+
plugin.apply(environment)
79+
80+
verify(environment, times(0)).decorateAround(eq(TerraformEnvironmentStage.CONFIRM), any(Closure.class))
81+
verify(environment, times(0)).decorateAround(eq(TerraformEnvironmentStage.APPLY), any(Closure.class))
82+
}
83+
84+
@Test
85+
void addsFailPlanOnChangesArgumentToTerraformPlan() {
5986
PlanOnlyPlugin plugin = new PlanOnlyPlugin()
6087
TerraformPlanCommand command = new TerraformPlanCommand()
6188
MockJenkinsfile.withEnv('FAIL_PLAN_ON_CHANGES': 'true')
@@ -68,7 +95,7 @@ class PlanOnlyPluginTest {
6895
}
6996

7097
@Test
71-
void doesNotAddArgumentToTerraformPlan() {
98+
void doesNotAddFailPlanOnChangesArgumentToTerraformPlan() {
7299
PlanOnlyPlugin plugin = new PlanOnlyPlugin()
73100
TerraformPlanCommand command = new TerraformPlanCommand()
74101
MockJenkinsfile.withEnv('FAIL_PLAN_ON_CHANGES': 'false')

0 commit comments

Comments
 (0)