Skip to content

Commit

Permalink
Merge pull request #342 from kmanning/issue_329
Browse files Browse the repository at this point in the history
Issue 329: ConditionalApplyPlugin - disable to allow apply on any branch
  • Loading branch information
kmanning authored Feb 12, 2021
2 parents e6f0fc9 + fce58b5 commit 520ce89
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 9 additions & 0 deletions docs/ConditionalApplyPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
...
```
10 changes: 10 additions & 0 deletions src/ConditionalApplyPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import static TerraformEnvironmentStage.APPLY

public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin, Resettable {

private static enabled = true
private static DEFAULT_BRANCHES = ['master']
private static branches = DEFAULT_BRANCHES

public static void withApplyOnBranch(String... enabledBranches) {
branches = enabledBranches.clone()
}

public static disable() {
enabled = false
}

public static void reset() {
branches = DEFAULT_BRANCHES
enabled = true
}

@Override
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/ConditionalApplyPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}

0 comments on commit 520ce89

Please sign in to comment.