From 8fbc7433079b15acf5b07b4c8f87a6d27f09b8d9 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 12 Feb 2021 14:51:00 -0500 Subject: [PATCH 1/5] Update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5432353e..1eaa72636 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ # Unreleased +* [Issue #329](https://github.com/manheim/terraform-pipeline/issues/329) Feature: ConditionalApplyPlugin - can be disabled to allow apply on all branches/PRs. * [Issue #320](https://github.com/manheim/terraform-pipeline/issues/320) Bug Fix: GithubPRPlugin should stop on plan errors, and display error to the user. * [Issue #331](https://github.com/manheim/terraform-pipeline/issues/331) Bug Fix: terraform-pipeline should be usable even if Docker-Pipeline-plugin is not installed (and Docker featuers are not used) * [Issue #335](https://github.com/manheim/terraform-pipeline/issues/335) Testing: Rename DummyJenkinsfile to MockWorkflowScript to better distinguish Jenkinsfile references. * [Issue #271](https://github.com/manheim/terraform-pipeline/issues/271) Testing: Cleanup Test resets for any static state. New Resettable and ResetStaticStateExtension available. -* [Issue #332](https://github.com/manheim/terraform-pipeline/issues/332) Upgrade from Junit 4 to 5.7, upgrade hamcrest from 1.3 to 2.2, remove junit-hierarchicalcontextrunner dependency +* [Issue #332](https://github.com/manheim/terraform-pipeline/issues/332) Testing: Junit 4 to 5.7, upgrade hamcrest from 1.3 to 2.2, remove junit-hierarchicalcontextrunner dependency # v5.14 From 4828e5b093194787899f6c012ce3a56506c02987 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 12 Feb 2021 14:52:46 -0500 Subject: [PATCH 2/5] Update docs to give an example of how the feature works --- docs/ConditionalApplyPlugin.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/ConditionalApplyPlugin.md b/docs/ConditionalApplyPlugin.md index e0ba167ce..2cd1eba79 100644 --- a/docs/ConditionalApplyPlugin.md +++ b/docs/ConditionalApplyPlugin.md @@ -4,3 +4,12 @@ This plugin is enabled by default. Changes should be applied through one and only one branch - master. The ConditionalApplyPlugin enforces this by making the "Confirm" and "Apply" steps of a TerraformEnvironmentStage visible only on the master branch. You can continue to use branches and PullRequests, however, branches and PullRequests will only run the Plan step for each environment, and skip over the Confirm/Apply steps. +Disable this plugin, if you want to allow "Confirm" and "Apply" on any branch or PullRequest. + +``` +@Library(['terraform-pipeline']) _ + +Jenkinsfile.init(this) +ConditionalApplyPlugin.disable() +... +``` From 129463725c7a1f64e5fb0fd03d944056497e2021 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 12 Feb 2021 15:02:55 -0500 Subject: [PATCH 3/5] Run Apply on master when ConditionalApplyPlugin disabled --- src/ConditionalApplyPlugin.groovy | 10 ++++++++++ test/ConditionalApplyPluginTest.groovy | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/ConditionalApplyPlugin.groovy b/src/ConditionalApplyPlugin.groovy index 67309e151..299fb0a73 100644 --- a/src/ConditionalApplyPlugin.groovy +++ b/src/ConditionalApplyPlugin.groovy @@ -3,6 +3,7 @@ import static TerraformEnvironmentStage.APPLY public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin, Resettable { + private static enabled = true private static DEFAULT_BRANCHES = ['master'] private static branches = DEFAULT_BRANCHES @@ -10,8 +11,13 @@ public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin, branches = enabledBranches.clone() } + public static disable() { + enabled = false + } + public static void reset() { branches = DEFAULT_BRANCHES + enabled = true } @Override @@ -31,6 +37,10 @@ public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin, } public boolean shouldApply() { + if (!enabled) { + return true + } + if (branches.contains(Jenkinsfile.instance.getEnv().BRANCH_NAME)) { println("Current branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}' matches expected branches '${branches}', stage branch-condition is met and will run.") return true diff --git a/test/ConditionalApplyPluginTest.groovy b/test/ConditionalApplyPluginTest.groovy index 790cba59b..5075c32df 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -69,6 +69,27 @@ class ConditionalApplyPluginTest { assertTrue(plugin.shouldApply()) } + + @Nested + class WhenDisabled { + @Test + void returnsTrueWhenBranchIsMaster() { + ConditionalApplyPlugin.disable() + MockJenkinsfile.withEnv(BRANCH_NAME: 'master') + def plugin = new ConditionalApplyPlugin() + + assertTrue(plugin.shouldApply()) + } +/* + void returnsTrueWhenBranchIsAnything() { + + } + + void returnsTrueWhenBranchIsUnknown() { + + } +*/ + } } } From 143704ac69a223f04e877ad4a0ca54aab11e8917 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 12 Feb 2021 15:04:01 -0500 Subject: [PATCH 4/5] Run Apply on any other branch when ConditionalApplyPlugin disabled --- test/ConditionalApplyPluginTest.groovy | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/ConditionalApplyPluginTest.groovy b/test/ConditionalApplyPluginTest.groovy index 5075c32df..afc390aab 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -80,11 +80,16 @@ class ConditionalApplyPluginTest { assertTrue(plugin.shouldApply()) } -/* - void returnsTrueWhenBranchIsAnything() { + void returnsTrueWhenBranchIsAnythingOtherThanMaster() { + ConditionalApplyPlugin.disable() + MockJenkinsfile.withEnv(BRANCH_NAME: 'anyPossibleBranch') + def plugin = new ConditionalApplyPlugin() + + assertTrue(plugin.shouldApply()) } +/* void returnsTrueWhenBranchIsUnknown() { } From fce58b5a017a5f2118ad8d8f731c840618ce4041 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 12 Feb 2021 15:05:37 -0500 Subject: [PATCH 5/5] Run Apply when ConditionalApplyPlugin disabled even if branch not known --- test/ConditionalApplyPluginTest.groovy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/ConditionalApplyPluginTest.groovy b/test/ConditionalApplyPluginTest.groovy index afc390aab..4984e122a 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -89,11 +89,13 @@ class ConditionalApplyPluginTest { assertTrue(plugin.shouldApply()) } -/* void returnsTrueWhenBranchIsUnknown() { + ConditionalApplyPlugin.disable() + MockJenkinsfile.withEnv() + def plugin = new ConditionalApplyPlugin() + assertTrue(plugin.shouldApply()) } -*/ } } }