-
Notifications
You must be signed in to change notification settings - Fork 53
Issue 175: Pass terraform plan file to apply #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 35 commits
622724c
c9c9112
389e975
60b50bd
368bb09
a657e42
460e8a8
41a5966
edfba49
2d58569
3f326cc
088f269
f17714b
bae17af
203bdc4
aa84e0e
4c78ff1
72581b3
bf31317
bc95cc7
8e44fce
6b6a3b8
0b1bf1b
6999cf9
0de7b3a
c5e91f9
afdc7a0
b3faf85
064b63f
4bb35c2
ae9dc44
07ec943
4b3575d
4ad3227
f32e800
06501bf
4e80af6
712ab07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| ## [PassPlanFilePlugin](../src/PassPlanFilePlugin.groovy) | ||
|
|
||
| Enable this plugin to pass the plan file output to `terraform apply`. | ||
|
|
||
| This plugin stashes the plan file during the `plan` step. | ||
| When `apply` is called, the plan file is unstashed and passed as an argument. | ||
|
|
||
|
|
||
| ``` | ||
| // Jenkinsfile | ||
| @Library(['terraform-pipeline@v3.10']) _ | ||
|
|
||
| Jenkinsfile.init(this, env) | ||
|
|
||
| // Pass the plan file to 'terraform apply' | ||
| PassPlanFilePlugin.init() | ||
|
|
||
| def validate = new TerraformValidateStage() | ||
|
|
||
| def destroyQa = new TerraformEnvironmentStage('qa') | ||
| def destroyUat = new TerraformEnvironmentStage('uat') | ||
| def destroyProd = new TerraformEnvironmentStage('prod') | ||
|
|
||
| validate.then(destroyQa) | ||
| .then(destroyUat) | ||
| .then(destroyProd) | ||
| .build() | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import static TerraformEnvironmentStage.PLAN | ||
| import static TerraformEnvironmentStage.APPLY | ||
|
|
||
| class PassPlanFilePlugin implements TerraformPlanCommandPlugin, TerraformApplyCommandPlugin, TerraformEnvironmentStagePlugin { | ||
|
|
||
| public static void init() { | ||
| PassPlanFilePlugin plugin = new PassPlanFilePlugin() | ||
|
|
||
| TerraformEnvironmentStage.addPlugin(plugin) | ||
| TerraformPlanCommand.addPlugin(plugin) | ||
| TerraformApplyCommand.addPlugin(plugin) | ||
| } | ||
|
|
||
| @Override | ||
| public void apply(TerraformEnvironmentStage stage) { | ||
| stage.decorate(PLAN, stashPlan(stage.getEnvironment())) | ||
| stage.decorate(APPLY, unstashPlan(stage.getEnvironment())) | ||
| } | ||
|
|
||
| @Override | ||
| public void apply(TerraformPlanCommand command) { | ||
| String env = command.getEnvironment() | ||
| command.withArgument("-out=tfplan-" + env) | ||
| } | ||
|
|
||
| @Override | ||
| public void apply(TerraformApplyCommand command) { | ||
| String env = command.getEnvironment() | ||
| command.withDirectory("tfplan-" + env) | ||
| } | ||
|
|
||
| public Closure stashPlan(String env) { | ||
| return { closure -> | ||
| closure() | ||
| String planFile = "tfplan-" + env | ||
| echo "Stashing ${planFile} file" | ||
| stash name: planFile, includes: planFile | ||
| } | ||
| } | ||
|
|
||
| public Closure unstashPlan(String env) { | ||
| return { closure -> | ||
| String planFile = "tfplan-" + env | ||
| echo "Unstashing ${planFile} file" | ||
| unstash planFile | ||
| closure() | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||||||||||||||||
| import static org.hamcrest.Matchers.containsString | ||||||||||||||||||||||||
| import static org.hamcrest.Matchers.hasItem | ||||||||||||||||||||||||
| import static org.hamcrest.Matchers.instanceOf | ||||||||||||||||||||||||
| import static org.junit.Assert.assertThat | ||||||||||||||||||||||||
| import static org.mockito.Mockito.spy; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.doReturn; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.verify; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.any; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.anyString; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.eq; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.times; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.when; | ||||||||||||||||||||||||
| import static org.mockito.Mockito.mock; | ||||||||||||||||||||||||
| import org.junit.Test | ||||||||||||||||||||||||
| import org.junit.Before | ||||||||||||||||||||||||
| import org.junit.After | ||||||||||||||||||||||||
| import org.junit.runner.RunWith | ||||||||||||||||||||||||
| import de.bechte.junit.runners.context.HierarchicalContextRunner | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @RunWith(HierarchicalContextRunner.class) | ||||||||||||||||||||||||
| class PassPlanFilePluginTest { | ||||||||||||||||||||||||
| @Before | ||||||||||||||||||||||||
| void resetJenkinsEnv() { | ||||||||||||||||||||||||
| Jenkinsfile.instance = mock(Jenkinsfile.class) | ||||||||||||||||||||||||
| when(Jenkinsfile.instance.getEnv()).thenReturn([:]) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private configureJenkins(Map config = [:]) { | ||||||||||||||||||||||||
| Jenkinsfile.instance = mock(Jenkinsfile.class) | ||||||||||||||||||||||||
| when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class Init { | ||||||||||||||||||||||||
| @After | ||||||||||||||||||||||||
| void resetPlugins() { | ||||||||||||||||||||||||
| TerraformPlanCommand.resetPlugins() | ||||||||||||||||||||||||
| TerraformApplyCommand.resetPlugins() | ||||||||||||||||||||||||
| TerraformEnvironmentStage.reset() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void modifiesTerraformEnvironmentStageCommand() { | ||||||||||||||||||||||||
| PassPlanFilePlugin.init() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Collection actualPlugins = TerraformEnvironmentStage.getPlugins() | ||||||||||||||||||||||||
| assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void modifiesTerraformPlanCommand() { | ||||||||||||||||||||||||
| PassPlanFilePlugin.init() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Collection actualPlugins = TerraformPlanCommand.getPlugins() | ||||||||||||||||||||||||
| assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void modifiesTerraformApplyCommand() { | ||||||||||||||||||||||||
| PassPlanFilePlugin.init() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Collection actualPlugins = TerraformApplyCommand.getPlugins() | ||||||||||||||||||||||||
| assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class Apply { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void decoratesTheTerraformEnvironmentStage() { | ||||||||||||||||||||||||
| PassPlanFilePlugin plugin = new PassPlanFilePlugin() | ||||||||||||||||||||||||
| def environment = spy(new TerraformEnvironmentStage()) | ||||||||||||||||||||||||
| plugin.apply(environment) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.PLAN), any(Closure.class)) | ||||||||||||||||||||||||
| verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.APPLY), any(Closure.class)) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void runsStashPlan() { | ||||||||||||||||||||||||
| def expectedClosure = { -> } | ||||||||||||||||||||||||
| def plugin = spy(new PassPlanFilePlugin()) | ||||||||||||||||||||||||
| doReturn(expectedClosure).when(plugin).stashPlan() | ||||||||||||||||||||||||
| def stage = mock(TerraformEnvironmentStage.class) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| plugin.apply(stage) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| verify(stage).decorate(anyString(), eq(expectedClosure)) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||
| void runsUnstashPlan() { | ||||||||||||||||||||||||
| def expectedClosure = { -> } | ||||||||||||||||||||||||
| def plugin = spy(new PassPlanFilePlugin()) | ||||||||||||||||||||||||
| doReturn(expectedClosure).when(plugin).unstashPlan() | ||||||||||||||||||||||||
| def stage = mock(TerraformEnvironmentStage.class) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| plugin.apply(stage) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| verify(stage).decorate(anyString(), eq(expectedClosure)) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
| @Test | |
| void doesNotBlowUpWhenRunningClosure() { | |
| Jenkinsfile.instance = spy(new Jenkinsfile()) | |
| doReturn([:]).when(Jenkinsfile.instance).getEnv() | |
| Jenkinsfile.defaultNodeName = 'foo' | |
| def stage = new TerraformEnvironmentStage('foo') | |
| def closure = stage.pipelineConfiguration() | |
| closure.delegate = new DummyJenkinsfile() | |
| closure() | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. The change to use stash/unstash looks great =).