-
Notifications
You must be signed in to change notification settings - Fork 53
/
FileParametersPlugin.groovy
47 lines (36 loc) · 1.63 KB
/
FileParametersPlugin.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
import static TerraformEnvironmentStage.ALL
import groovy.text.StreamingTemplateEngine
class FileParametersPlugin implements TerraformEnvironmentStagePlugin {
public static void init() {
FileParametersPlugin plugin = new FileParametersPlugin()
TerraformEnvironmentStage.addPlugin(plugin)
}
@Override
public void apply(TerraformEnvironmentStage stage) {
String environment = stage.getEnvironment()
stage.decorate(ALL, addEnvironmentSpecificVariables(environment))
}
public Closure addEnvironmentSpecificVariables(String environment) {
String environmentFilename = "${environment}.properties"
return { closure ->
if (fileExists(environmentFilename)) {
echo "Found file: ${environmentFilename} - loading the contents as environment variables."
String fileContent = readFile(environmentFilename)
List variables = getVariables(fileContent)
withEnv(variables) { closure() }
} else {
echo "No environment properties file found. Create a ${environmentFilename} file to add environment-specific variables to this stage."
closure()
}
}
}
public List getVariables(String fileContent) {
return fileContent.split('\\r?\\n').collect { String value -> interpolate(value) }
}
public String interpolate(String value) {
return new StreamingTemplateEngine().createTemplate(value).make([env: getEnv()]).toString()
}
public getEnv() {
return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:]
}
}