-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathTerraformEnvironmentStage.groovy
201 lines (166 loc) · 6.71 KB
/
TerraformEnvironmentStage.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class TerraformEnvironmentStage implements Stage, DecoratableStage, Resettable {
private Jenkinsfile jenkinsfile
private String environment
private StageDecorations decorations
private localPlugins
private static final DEFAULT_PLUGINS = [ new ConditionalApplyPlugin(), new ConfirmApplyPlugin(), new DefaultEnvironmentPlugin() ]
private static globalPlugins = DEFAULT_PLUGINS.clone()
private static Closure stageNamePattern
public static final String ALL = 'all'
public static final String INIT_COMMAND = 'init-command'
public static final String PLAN = 'plan'
public static final String PLAN_COMMAND = 'plan-command'
public static final String CONFIRM = 'confirm'
public static final String APPLY = 'apply'
public static final String APPLY_COMMAND = 'apply-command'
TerraformEnvironmentStage(String environment) {
this.environment = environment
this.jenkinsfile = Jenkinsfile.instance
this.decorations = new StageDecorations()
}
public Stage then(Stage nextStage) {
return new BuildGraph(this).then(nextStage)
}
public static withGlobalEnv(String key, String value) {
def plugin = new EnvironmentVariablePlugin()
plugin.withEnv(key, value)
addPlugin(plugin)
return this
}
public TerraformEnvironmentStage withEnv(String key, String value) {
def plugin = new EnvironmentVariablePlugin()
plugin.withEnv(key, value)
reconcileLocalAndGlobalPlugins(plugin)
return this
}
public void build() {
Jenkinsfile.build(pipelineConfiguration())
}
public Closure pipelineConfiguration() {
def initCommand = TerraformInitCommand.instanceFor(environment)
def planCommand = TerraformPlanCommand.instanceFor(environment)
def applyCommand = TerraformApplyCommand.instanceFor(environment)
applyPlugins()
def String environment = this.environment
return { ->
node(jenkinsfile.getNodeName()) {
deleteDir()
checkout(scm)
decorations.apply(ALL) {
stage(getStageNameFor(PLAN)) {
decorations.apply(PLAN) {
decorations.apply(INIT_COMMAND) {
sh initCommand.toString()
}
decorations.apply(PLAN_COMMAND) {
sh planCommand.toString()
}
}
}
decorations.apply("Around-${CONFIRM}") {
// The stage name needs to be editable
stage(getStageNameFor(CONFIRM)) {
decorations.apply(CONFIRM) {
echo "Approved"
}
}
}
decorations.apply("Around-${APPLY}") {
// The stage name needs to be editable
stage(getStageNameFor(APPLY)) {
decorations.apply(APPLY) {
decorations.apply(INIT_COMMAND) {
sh initCommand.toString()
}
decorations.apply(APPLY_COMMAND) {
sh applyCommand.toString()
}
}
}
}
}
}
}
}
public String getStageNameFor(String command) {
def pattern = stageNamePattern ?: { options -> "${options['command']}-${options['environment']}" }
def options = [ command: command, environment: environment ]
return pattern.call(options)
}
public void decorate(Closure decoration) {
decorations.add(ALL, decoration)
}
public decorate(String stageName, Closure decoration) {
decorations.add(stageName, decoration)
}
public decorateAround(String stageName, Closure decoration) {
decorations.add("Around-${stageName}", decoration)
}
public String toString() {
return environment
}
public static addPlugin(plugin) {
globalPlugins << plugin
}
public void applyPlugins() {
// Apply both global and local plugins, in the correct order
for (plugin in getAllPlugins()) {
plugin.apply(this)
}
}
public String getEnvironment() {
return environment
}
/* Returns global globalPlugins, in addition to all
* plugins that have been added to this instance
*/
public getAllPlugins() {
return reconcileLocalAndGlobalPlugins()
}
private reconcileLocalAndGlobalPlugins(TerraformEnvironmentStagePlugin newPlugin = null) {
if (localPlugins == null) {
if (newPlugin == null) {
// No local plugins were added - only global plugins take effect
return globalPlugins
}
// The first local plugin was added. It takes effective *after* every current global plugin
localPlugins = globalPlugins.clone()
localPlugins << newPlugin
return localPlugins
}
// We're here because a localPlugin was previously added. Check if any new global plugins
// have been added since.
// Start off with all global plugins
def remainingGlobalPlugins = globalPlugins.clone()
for (def plugin in localPlugins) {
// If all global plugins are accounted for, stop
if (remainingGlobalPlugins.isEmpty()) {
break;
}
// Cross off each global plugin that has not yet been accounted for
if (remainingGlobalPlugins.first() == plugin) {
remainingGlobalPlugins.remove(plugin)
}
// If the plugin was not in remainingGlobalPlugins, it means it was added locally
}
// Any global plugins that remain in this list have been added since the last time we checked.
// Add them now.
localPlugins.addAll(remainingGlobalPlugins)
// If we have a new plugin to add, do it now
if (newPlugin != null) {
localPlugins << newPlugin
}
return localPlugins
}
public static getPlugins() {
return globalPlugins
}
public static withStageNamePattern(Closure stageNamePattern) {
this.stageNamePattern = stageNamePattern
}
public static void reset() {
// This totally jacks with localPlugins
this.globalPlugins = DEFAULT_PLUGINS.clone()
this.stageNamePattern = null
}
}