Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* [Issue #25](https://github.com/manheim/terraform-pipeline/issues/25) Allow 'apply' on branches other than master for some environments

# v5.13

* [Issue #321](https://github.com/manheim/terraform-pipeline/issues/321) Upgrade groovy from 2.4.11 to 2.4.12 (fix travisCi failures)
Expand Down
17 changes: 11 additions & 6 deletions src/ConditionalApplyPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import static TerraformEnvironmentStage.APPLY

public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {

private String branch
private static DEFAULT_BRANCHES = ['master']
private static branches = DEFAULT_BRANCHES

ConditionalApplyPlugin() {
branch = 'master'
public static void withApplyOnBranch(String... enabledBranches) {
branches = enabledBranches.clone()
}

public static void reset() {
branches = DEFAULT_BRANCHES
}

@Override
Expand All @@ -20,14 +25,14 @@ public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {
if (shouldApply()) {
closure()
} else {
echo "This stage can only be run on the '${branch}' branch, but this pipeline is currently running on branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}'. Skipping stage."
echo "This stage can only be run on the '${branches}' branches, but this pipeline is currently running on branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}'. Skipping stage."
}
}
}

public boolean shouldApply() {
if (branch == Jenkinsfile.instance.getEnv().BRANCH_NAME) {
println("Current branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}' matches expected branch '${branch}', stage branch-condition is met and will run.")
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
} else if (null == Jenkinsfile.instance.getEnv().BRANCH_NAME) {
println("Current branch is null - you're probably using a single-branch job which doesn't make your branch name available. Assume that apply should be enabled.")
Expand Down
32 changes: 30 additions & 2 deletions test/ConditionalApplyPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ConditionalApplyPluginTest {
@After
public void reset() {
Jenkinsfile.instance = null
ConditionalApplyPlugin.reset()
}

private configureJenkins(Map config = [:]) {
Expand All @@ -33,21 +34,48 @@ class ConditionalApplyPluginTest {

class ShouldApply {
@Test
void returnsTrueForMaster() {
void returnsTrueForMasterByDefault() {
configureJenkins(env: [ BRANCH_NAME: 'master' ])
def plugin = new ConditionalApplyPlugin()

assertTrue(plugin.shouldApply())
}

@Test
void returnsFalseForNonMaster() {
void returnsFalseForNonMasterByDefault() {
configureJenkins(env: [ BRANCH_NAME: 'notMaster' ])
def plugin = new ConditionalApplyPlugin()

assertFalse(plugin.shouldApply())
}

@Test
void returnsTrueForFirstConfiguredBranch() {
configureJenkins(env: [ BRANCH_NAME: 'qa' ])
ConditionalApplyPlugin.withApplyOnBranch('qa', 'someOtherBranch')
def plugin = new ConditionalApplyPlugin()

assertTrue(plugin.shouldApply())
}

@Test
void returnsTrueForOtherConfiguredBranches() {
configureJenkins(env: [ BRANCH_NAME: 'someOtherBranch' ])
ConditionalApplyPlugin.withApplyOnBranch('qa', 'someOtherBranch')
def plugin = new ConditionalApplyPlugin()

assertTrue(plugin.shouldApply())
}

@Test
void returnsFalseForNonMatchingBranch() {
configureJenkins(env: [ BRANCH_NAME: 'notQa' ])
ConditionalApplyPlugin.withApplyOnBranch('qa', 'someOtherBranch')
def plugin = new ConditionalApplyPlugin()

assertFalse(plugin.shouldApply())
}

@Test
void returnsTrueWhenBranchIsUnknown() {
configureJenkins(env: [ : ])
Expand Down