-
Notifications
You must be signed in to change notification settings - Fork 52
/
ParameterStoreBuildWrapperPlugin.groovy
60 lines (47 loc) · 1.94 KB
/
ParameterStoreBuildWrapperPlugin.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
import static TerraformEnvironmentStage.PLAN
import static TerraformEnvironmentStage.APPLY
class ParameterStoreBuildWrapperPlugin implements TerraformEnvironmentStagePlugin {
private static globalPathPattern
private static defaultPathPattern = { options -> "/${options['organization']}/${options['repoName']}/${options['environment']}/" }
public static void init() {
TerraformEnvironmentStage.addPlugin(new ParameterStoreBuildWrapperPlugin())
}
public static withPathPattern(Closure newPathPattern) {
globalPathPattern = newPathPattern
return this
}
@Override
public void apply(TerraformEnvironmentStage stage) {
def environment = stage.getEnvironment()
def parameterStorePath = pathForEnvironment(environment)
def options = [
path: parameterStorePath,
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
]
stage.decorate(PLAN, addParameterStoreBuildWrapper(options))
stage.decorate(APPLY, addParameterStoreBuildWrapper(options))
}
String pathForEnvironment(String environment) {
String organization = Jenkinsfile.instance.getOrganization()
String repoName = Jenkinsfile.instance.getRepoName()
def patternOptions = [ environment: environment,
repoName: repoName,
organization: organization ]
def pathPattern = globalPathPattern ?: defaultPathPattern
return pathPattern(patternOptions)
}
public static Closure addParameterStoreBuildWrapper(Map options = []) {
def Map defaultOptions = [
naming: 'basename'
]
def parameterStoreOptions = defaultOptions + options
return { closure ->
withAWSParameterStore(parameterStoreOptions) {
closure()
}
}
}
public static reset() {
globalPathPattern = null
}
}