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 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() +... +``` 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..4984e122a 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -69,6 +69,34 @@ 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 returnsTrueWhenBranchIsAnythingOtherThanMaster() { + ConditionalApplyPlugin.disable() + MockJenkinsfile.withEnv(BRANCH_NAME: 'anyPossibleBranch') + def plugin = new ConditionalApplyPlugin() + + assertTrue(plugin.shouldApply()) + } + + void returnsTrueWhenBranchIsUnknown() { + ConditionalApplyPlugin.disable() + MockJenkinsfile.withEnv() + def plugin = new ConditionalApplyPlugin() + + assertTrue(plugin.shouldApply()) + } + } } }