Skip to content

Commit affa2f1

Browse files
committed
Be able to configure conditional branches
1 parent 30710d2 commit affa2f1

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/ConditionalApplyPlugin.groovy

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import static TerraformEnvironmentStage.APPLY
33

44
public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {
55

6-
private String branch
6+
private static DEFAULT_BRANCHES = ['master']
7+
private static branches = DEFAULT_BRANCHES
78

8-
ConditionalApplyPlugin() {
9-
branch = 'master'
9+
public static void withBranchApplyEnabledFor(enabledBranches) {
10+
branches = enabledBranches.clone()
11+
}
12+
13+
public static void reset() {
14+
branches = DEFAULT_BRANCHES
1015
}
1116

1217
@Override
@@ -20,14 +25,14 @@ public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {
2025
if (shouldApply()) {
2126
closure()
2227
} else {
23-
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."
28+
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."
2429
}
2530
}
2631
}
2732

2833
public boolean shouldApply() {
29-
if (branch == Jenkinsfile.instance.getEnv().BRANCH_NAME) {
30-
println("Current branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}' matches expected branch '${branch}', stage branch-condition is met and will run.")
34+
if (branches.contains(Jenkinsfile.instance.getEnv().BRANCH_NAME)) {
35+
println("Current branch '${Jenkinsfile.instance.getEnv().BRANCH_NAME}' matches expected branches '${branches}', stage branch-condition is met and will run.")
3136
return true
3237
} else if (null == Jenkinsfile.instance.getEnv().BRANCH_NAME) {
3338
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.")

test/ConditionalApplyPluginTest.groovy

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ConditionalApplyPluginTest {
1616
@After
1717
public void reset() {
1818
Jenkinsfile.instance = null
19+
ConditionalApplyPlugin.reset()
1920
}
2021

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

3435
class ShouldApply {
3536
@Test
36-
void returnsTrueForMaster() {
37+
void returnsTrueForMasterByDefault() {
3738
configureJenkins(env: [ BRANCH_NAME: 'master' ])
3839
def plugin = new ConditionalApplyPlugin()
3940

4041
assertTrue(plugin.shouldApply())
4142
}
4243

4344
@Test
44-
void returnsFalseForNonMaster() {
45+
void returnsFalseForNonMasterByDefault() {
4546
configureJenkins(env: [ BRANCH_NAME: 'notMaster' ])
4647
def plugin = new ConditionalApplyPlugin()
4748

4849
assertFalse(plugin.shouldApply())
4950
}
5051

52+
@Test
53+
void returnsTrueForFirstConfiguredBranch() {
54+
configureJenkins(env: [ BRANCH_NAME: 'qa' ])
55+
ConditionalApplyPlugin.withBranchApplyEnabledFor(['qa', 'someOtherBranch'])
56+
def plugin = new ConditionalApplyPlugin()
57+
58+
assertTrue(plugin.shouldApply())
59+
}
60+
61+
@Test
62+
void returnsTrueForOtherConfiguredBranches() {
63+
configureJenkins(env: [ BRANCH_NAME: 'someOtherBranch' ])
64+
ConditionalApplyPlugin.withBranchApplyEnabledFor(['qa', 'someOtherBranch'])
65+
def plugin = new ConditionalApplyPlugin()
66+
67+
assertTrue(plugin.shouldApply())
68+
}
69+
70+
@Test
71+
void returnsFalseForNonMatchingBranch() {
72+
configureJenkins(env: [ BRANCH_NAME: 'notQa' ])
73+
ConditionalApplyPlugin.withBranchApplyEnabledFor(['qa', 'someOtherBranch'])
74+
def plugin = new ConditionalApplyPlugin()
75+
76+
assertFalse(plugin.shouldApply())
77+
}
78+
5179
@Test
5280
void returnsTrueWhenBranchIsUnknown() {
5381
configureJenkins(env: [ : ])

0 commit comments

Comments
 (0)