Skip to content

Commit

Permalink
Issue 21: remove outputMapping, break apart environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanning committed Apr 14, 2021
1 parent e246440 commit f99788a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 35 deletions.
14 changes: 8 additions & 6 deletions docs/FlywayMigrationPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ validate.then(deployQa)
.build()
```

If needed, flyway configuration can be set through the Plugin.
If needed, flyway configuration can be set through the Plugin, or FlywayCommand.

```
@Library(['terraform-pipeline']) _
Jenkinsfile.init(this, Customizations)
// Runs `terraform output jdbc_url_with_database` and sets the output to the environment variable FLYWAY_URL
// Sets FLYWAY_USER to the value of $TF_VAR_MIGRATION_USER
// Sets FLYWAY_PASSWORD to the value of $TF_VAR_MIGRATION_PASSWORD
FlywayMigrationPlugin.convertOutputToEnvironmentVariable('jdbc_url_with_database', 'FLYWAY_URL')
.withUserVariable('TF_VAR_MIGRATION_USER')
// Use the FlywayCommand to modify specific options like `locations` and `url`
FlywayCommand.withLocations("filesystem:`pwd`/migrations")
.withUrl("`terraform output jdbc_url`")
// Flyway automatically picks up specific enviornment variables (FLYWAY_USER, FLYWAY_PASSWORD)
// Sets FLYWAY_USER to the value of $TF_VAR_MIGRATION_USER
// Sets FLYWAY_PASSWORD to the value of $TF_VAR_MIGRATION_PASSWORD
FlywayMigrationPlugin.withUserVariable('TF_VAR_MIGRATION_USER')
.withPasswordVariable('TF_VAR_MIGRATION_PASSWORD')
.init()
Expand Down
31 changes: 12 additions & 19 deletions src/FlywayMigrationPlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettable {
public static Map<String,String> outputMappings = [:]
public static String passwordVariable
public static String userVariable

Expand All @@ -14,32 +13,26 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
public Closure flywayInfoClosure() {
return { innerClosure ->
innerClosure()
def environmentVariables = outputMappings.collect { variable, outputId ->
def outputValue = sh(
script: "terraform output ${outputId}",
returnStdout: true
).trim()
"${variable}=${outputValue}"
}

if (passwordVariable) {
environmentVariables << "FLYWAY_PASSWORD=${env[passwordVariable]}"
}

if (userVariable) {
environmentVariables << "FLYWAY_USER=${env[userVariable]}"
}

def environmentVariables = buildEnvironmentVariableList(env)
withEnv(environmentVariables) {
def command = new FlywayCommand('info')
sh command.toString()
}
}
}

public static convertOutputToEnvironmentVariable(String output, String variableName) {
outputMappings[variableName] = output
return this
public Collection buildEnvironmentVariableList(env) {
def list = []
if (passwordVariable) {
list << "FLYWAY_PASSWORD=${env[passwordVariable]}"
}

if (userVariable) {
list << "FLYWAY_USER=${env[userVariable]}"
}

return list
}

public static withPasswordFromEnvironmentVariable(String passwordVariable) {
Expand Down
64 changes: 54 additions & 10 deletions test/FlywayMigrationPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.instanceOf
import static org.hamcrest.MatcherAssert.assertThat
import static org.mockito.Mockito.any
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -39,16 +41,6 @@ class FlywayMigrationPluginTest {
}
}

@Nested
public class ConvertOutputToEnvironmentVariable {
@Test
void isFluent() {
def result = FlywayMigrationPlugin.convertOutputToEnvironmentVariable('output', 'VARIABLE')

assertThat(result, equalTo(FlywayMigrationPlugin.class))
}
}

@Nested
public class WithPasswordFromEnvironmentVariable {
@Test
Expand Down Expand Up @@ -83,6 +75,58 @@ class FlywayMigrationPluginTest {

assertThat(iWasCalled, equalTo(true))
}

@Test
void setsTheListOfOptionalEnvironmentVariables() {
def plugin = spy(new FlywayMigrationPlugin())
def expectedList = ['KEY=value']
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
def flywayClosure = plugin.flywayInfoClosure()
def mockWorkflowScript = spy(new MockWorkflowScript())
flywayClosure.delegate = mockWorkflowScript

flywayClosure { -> }

verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
}
}

@Nested
public class BuildEnvironmentVariableList {
@Test
void returnsEmptyListByDefault() {
def plugin = new FlywayMigrationPlugin()

def result = plugin.buildEnvironmentVariableList(null)

assertThat(result, equalTo([]))
}

@Test
void setsPasswordWhenVariableProvided() {
def expectedVariable = 'MY_PASSWORD_VARIABLE'
def expectedValue = 'somePass'
FlywayMigrationPlugin.withPasswordFromEnvironmentVariable(expectedVariable)
def plugin = new FlywayMigrationPlugin()
def env = [(expectedVariable): expectedValue]

def result = plugin.buildEnvironmentVariableList(env)

assertThat(result, equalTo(["FLYWAY_PASSWORD=${expectedValue}"]))
}

@Test
void setsUserWhenVariableProvided() {
def expectedVariable = 'MY_USER_VARIABLE'
def expectedValue = 'someUser'
FlywayMigrationPlugin.withUserFromEnvironmentVariable(expectedVariable)
def plugin = new FlywayMigrationPlugin()
def env = [(expectedVariable): expectedValue]

def result = plugin.buildEnvironmentVariableList(env)

assertThat(result, equalTo(["FLYWAY_USER=${expectedValue}"]))
}
}
}

0 comments on commit f99788a

Please sign in to comment.