-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
622724c
initial commit
jleopold28 c9c9112
change branch for apply
jleopold28 389e975
testing workspace
jleopold28 60b50bd
test pwd
jleopold28 368bb09
testing echo
jleopold28 a657e42
Testing pwd with appending file name
jleopold28 460e8a8
fix syntax
jleopold28 41a5966
adding envi=ronment name
jleopold28 edfba49
testing archive artifacts
jleopold28 2d58569
Testing archive
jleopold28 3f326cc
testing download archive
jleopold28 088f269
fix import
jleopold28 f17714b
testing with jobName
jleopold28 bae17af
eccho vars
jleopold28 203bdc4
testing splitting job name
jleopold28 aa84e0e
testing new split method
jleopold28 4c78ff1
fix loop typo
jleopold28 72581b3
adding new getArtifactUrl method
jleopold28 bf31317
cleanup
jleopold28 bc95cc7
adding tests
jleopold28 8e44fce
fix style
jleopold28 6b6a3b8
remove plan and apply plugin additions
jleopold28 0b1bf1b
update tests
jleopold28 6999cf9
Adding tests for url
jleopold28 0de7b3a
specify directoy instead of argument
jleopold28 c5e91f9
revert changes to conditional apply
jleopold28 afdc7a0
Test PR 7
jleopold28 b3faf85
switch branch to master
jleopold28 064b63f
Testing stash
jleopold28 4bb35c2
simplify
jleopold28 ae9dc44
stash based on filename
jleopold28 07ec943
remove tests for url generation
jleopold28 4b3575d
update tests
jleopold28 4ad3227
fix merge conflicts
jleopold28 f32e800
update docs
jleopold28 06501bf
update tests to verify closure
jleopold28 4e80af6
fix naming
jleopold28 712ab07
adding delegate
jleopold28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| 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.junit.Assert.assertTrue | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.any; | ||
| 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 addsArgumentToTerraformPlan() { | ||
| PassPlanFilePlugin plugin = new PassPlanFilePlugin() | ||
| TerraformPlanCommand command = new TerraformPlanCommand("dev") | ||
| plugin.apply(command) | ||
|
|
||
| String result = command.toString() | ||
| assertThat(result, containsString("-out=tfplan-dev")) | ||
| } | ||
|
|
||
| @Test | ||
| void addsArgumentToTerraformApply() { | ||
| PassPlanFilePlugin plugin = new PassPlanFilePlugin() | ||
| TerraformApplyCommand command = new TerraformApplyCommand("dev") | ||
| plugin.apply(command) | ||
|
|
||
| String result = command.toString() | ||
| assertThat(result, containsString("tfplan-dev")) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public class StashPlan { | ||
|
|
||
| @Test | ||
| void runsStashPlan() { | ||
| def wasCalled = false | ||
| def passedClosure = { -> wasCalled = true } | ||
| def plugin = new PassPlanFilePlugin() | ||
|
|
||
| def stashClosure = plugin.stashPlan('dev') | ||
| stashClosure.delegate = new DummyJenkinsfile() | ||
| stashClosure.call(passedClosure) | ||
|
|
||
| assertTrue(wasCalled) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public class UnstashPlan { | ||
|
|
||
| @Test | ||
| void runsUnstashPlan() { | ||
| def wasCalled = false | ||
| def passedClosure = { -> wasCalled = true } | ||
| def plugin = new PassPlanFilePlugin() | ||
|
|
||
| def unstashClosure = plugin.unstashPlan('dev') | ||
| unstashClosure.delegate = new DummyJenkinsfile() | ||
| unstashClosure.call(passedClosure) | ||
|
|
||
| assertTrue(wasCalled) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 =).