Skip to content

Commit 43f3365

Browse files
committed
Issue 21: remove outputMapping, break apart environment variables
1 parent 47c8b9c commit 43f3365

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

docs/FlywayMigrationPlugin.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ validate.then(deployQa)
2121
.build()
2222
```
2323

24-
If needed, flyway configuration can be set through the Plugin.
24+
If needed, flyway configuration can be set through the Plugin, or FlywayCommand.
2525

2626
```
2727
@Library(['terraform-pipeline']) _
2828
Jenkinsfile.init(this, Customizations)
29-
// Runs `terraform output jdbc_url_with_database` and sets the output to the environment variable FLYWAY_URL
30-
// Sets FLYWAY_USER to the value of $TF_VAR_MIGRATION_USER
31-
// Sets FLYWAY_PASSWORD to the value of $TF_VAR_MIGRATION_PASSWORD
32-
FlywayMigrationPlugin.convertOutputToEnvironmentVariable('jdbc_url_with_database', 'FLYWAY_URL')
33-
.withUserVariable('TF_VAR_MIGRATION_USER')
29+
// Use the FlywayCommand to modify specific options like `locations` and `url`
30+
FlywayCommand.withLocations("filesystem:`pwd`/migrations")
31+
.withUrl("`terraform output jdbc_url`")
32+
// Flyway automatically picks up specific enviornment variables (FLYWAY_USER, FLYWAY_PASSWORD)
33+
// Sets FLYWAY_USER to the value of $TF_VAR_MIGRATION_USER
34+
// Sets FLYWAY_PASSWORD to the value of $TF_VAR_MIGRATION_PASSWORD
35+
FlywayMigrationPlugin.withUserVariable('TF_VAR_MIGRATION_USER')
3436
.withPasswordVariable('TF_VAR_MIGRATION_PASSWORD')
3537
.init()
3638

src/FlywayMigrationPlugin.groovy

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettable {
2-
public static Map<String,String> outputMappings = [:]
32
public static String passwordVariable
43
public static String userVariable
54

@@ -14,32 +13,27 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
1413
public Closure flywayInfoClosure() {
1514
return { innerClosure ->
1615
innerClosure()
17-
def environmentVariables = outputMappings.collect { variable, outputId ->
18-
def outputValue = sh(
19-
script: "terraform output ${outputId}",
20-
returnStdout: true
21-
).trim()
22-
"${variable}=${outputValue}"
23-
}
24-
25-
if (passwordVariable) {
26-
environmentVariables << "FLYWAY_PASSWORD=${env[passwordVariable]}"
27-
}
28-
29-
if (userVariable) {
30-
environmentVariables << "FLYWAY_USER=${env[userVariable]}"
31-
}
3216

17+
def environmentVariables = buildEnvironmentVariableList(env)
3318
withEnv(environmentVariables) {
3419
def command = new FlywayCommand('info')
3520
sh command.toString()
3621
}
3722
}
3823
}
3924

40-
public static convertOutputToEnvironmentVariable(String output, String variableName) {
41-
outputMappings[variableName] = output
42-
return this
25+
public Collection buildEnvironmentVariableList(env) {
26+
def list = []
27+
if (passwordVariable) {
28+
println "env: ${env.toString()}, passwordVariable: ${passwordVariable}"
29+
list << "FLYWAY_PASSWORD=${env[passwordVariable]}"
30+
}
31+
32+
if (userVariable) {
33+
list << "FLYWAY_USER=${env[userVariable]}"
34+
}
35+
36+
return list
4337
}
4438

4539
public static withPasswordFromEnvironmentVariable(String passwordVariable) {

test/FlywayMigrationPluginTest.groovy

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import static org.hamcrest.Matchers.equalTo
22
import static org.hamcrest.Matchers.hasItem
33
import static org.hamcrest.Matchers.instanceOf
44
import static org.hamcrest.MatcherAssert.assertThat
5+
import static org.mockito.Mockito.any
56
import static org.mockito.Mockito.doReturn;
7+
import static org.mockito.Mockito.eq
68
import static org.mockito.Mockito.mock;
79
import static org.mockito.Mockito.spy;
810
import static org.mockito.Mockito.verify;
@@ -39,16 +41,6 @@ class FlywayMigrationPluginTest {
3941
}
4042
}
4143

42-
@Nested
43-
public class ConvertOutputToEnvironmentVariable {
44-
@Test
45-
void isFluent() {
46-
def result = FlywayMigrationPlugin.convertOutputToEnvironmentVariable('output', 'VARIABLE')
47-
48-
assertThat(result, equalTo(FlywayMigrationPlugin.class))
49-
}
50-
}
51-
5244
@Nested
5345
public class WithPasswordFromEnvironmentVariable {
5446
@Test
@@ -83,6 +75,58 @@ class FlywayMigrationPluginTest {
8375

8476
assertThat(iWasCalled, equalTo(true))
8577
}
78+
79+
@Test
80+
void setsTheListOfOptionalEnvironmentVariables() {
81+
def plugin = spy(new FlywayMigrationPlugin())
82+
def expectedList = ['KEY=value']
83+
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
84+
def flywayClosure = plugin.flywayInfoClosure()
85+
def mockWorkflowScript = spy(new MockWorkflowScript())
86+
flywayClosure.delegate = mockWorkflowScript
87+
88+
flywayClosure { -> }
89+
90+
verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
91+
}
92+
}
93+
94+
@Nested
95+
public class BuildEnvironmentVariableList {
96+
@Test
97+
void returnsEmptyListByDefault() {
98+
def plugin = new FlywayMigrationPlugin()
99+
100+
def result = plugin.buildEnvironmentVariableList(null)
101+
102+
assertThat(result, equalTo([]))
103+
}
104+
105+
@Test
106+
void setsPasswordWhenVariableProvided() {
107+
def expectedVariable = 'MY_PASSWORD_VARIABLE'
108+
def expectedValue = 'somePass'
109+
FlywayMigrationPlugin.withPasswordFromEnvironmentVariable(expectedVariable)
110+
def plugin = new FlywayMigrationPlugin()
111+
def env = [(expectedVariable): expectedValue]
112+
113+
def result = plugin.buildEnvironmentVariableList(env)
114+
115+
assertThat(result, equalTo(["FLYWAY_PASSWORD=${expectedValue}"]))
116+
}
117+
118+
@Test
119+
void setsUserWhenVariableProvided() {
120+
def expectedVariable = 'MY_USER_VARIABLE'
121+
def expectedValue = 'someUser'
122+
FlywayMigrationPlugin.withUserFromEnvironmentVariable(expectedVariable)
123+
def plugin = new FlywayMigrationPlugin()
124+
def env = [(expectedVariable): expectedValue]
125+
126+
def result = plugin.buildEnvironmentVariableList(env)
127+
128+
assertThat(result, equalTo(["FLYWAY_USER=${expectedValue}"]))
129+
}
86130
}
87131
}
88132

0 commit comments

Comments
 (0)