-
Notifications
You must be signed in to change notification settings - Fork 53
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
Adding support for Terraform Destroy. Adding ConfirmApply customization #251
Merged
Merged
Changes from all commits
Commits
Show all changes
106 commits
Select commit
Hold shift + click to select a range
4bad4b6
Merge pull request #1 from manheim/master
jleopold28 b956a3a
adding destroy command
jleopold28 dacd71c
typo fix
jleopold28 e95c8a5
remove constructor
jleopold28 3dcdc03
super constructor
jleopold28 7316ae1
remove comments
jleopold28 b161c37
super constructor
jleopold28 9115fc9
change private to protected
jleopold28 dbde8cd
adding localPlugins var
jleopold28 52d63c2
testing protected
jleopold28 192189b
add destroyCommand back
jleopold28 1929c58
remove extra init
jleopold28 95e07ad
add conditional around destroy
jleopold28 3c9f4fd
revert overloading method
jleopold28 07c866c
fix typo
jleopold28 c0f9509
change branch to destroy_test
jleopold28 398359c
ConfirmApply updates
jleopold28 c2635f5
fix typo
jleopold28 0948d99
define command_name
jleopold28 9731413
ading destroy support for target plugin
jleopold28 b339e68
typo
jleopold28 332bb40
adding initial docs
jleopold28 2b5cfaf
refactor
jleopold28 2ebadf3
adding newlines, refactoring
jleopold28 9c70aca
refactoring of confirm plugin
jleopold28 6edf162
clean up
jleopold28 8e5139d
add parens
jleopold28 0bba5b9
add void
jleopold28 6c7e323
Add constructor
jleopold28 d95e32a
adding constructors
jleopold28 968df59
adding constructor for tf destroy command
jleopold28 799d347
refactor
jleopold28 216f212
remove extends
jleopold28 5e9541a
Revert docs
jleopold28 15f581a
remove extension for destroy command
jleopold28 973c96c
add init() method
jleopold28 f7cf74e
Add jenkinsfile ver
jleopold28 5c6962f
define vars
jleopold28 1c95134
Add decorations
jleopold28 6e722d1
import hooks
jleopold28 78f184f
change init method
jleopold28 4cf2653
refactor getStrategyName
jleopold28 eb224ce
static strat
jleopold28 e3206f7
change destroycommand to extend from apply command
jleopold28 ee06537
testing destroy with extends
jleopold28 29d1fc9
changing constructor to use super
jleopold28 3bef392
change to protected
jleopold28 130b295
change to protected for method
jleopold28 9a94da0
change all to protected
jleopold28 709000e
remove unneeded file
jleopold28 a132306
testing changes to destroyPlugin
jleopold28 fc69c72
revert last changes
jleopold28 5c86117
testing set_command
jleopold28 ad679a9
fix typo
jleopold28 31dab58
Testing withCommand
jleopold28 7e4041e
remove static
jleopold28 39c1c90
make it all static=
jleopold28 afbe57f
cleanup
jleopold28 1c20dba
cleanup unused method
jleopold28 3af1e69
remove whitespace:
jleopold28 1528452
refactoring confirmapply logic
jleopold28 57e2130
change name of method
jleopold28 f278ed2
double quotes
jleopold28 89d7702
testing
jleopold28 4eaf1c9
fix typo
jleopold28 20ff00e
adding back addPlugin
jleopold28 0f9d949
change back to private
jleopold28 6d42814
remove static
jleopold28 ab41d93
return object
jleopold28 d97f28f
change wording
jleopold28 9cecca4
fix style
jleopold28 c4026a4
adding docs
jleopold28 13fac41
refactor, remove hook for destroy
jleopold28 9a83f37
revert prev changes
jleopold28 0cad059
import
jleopold28 457c10e
test import again
jleopold28 1a79575
testing decorate destroy stage
jleopold28 adb779b
remove unused import
jleopold28 a837cdf
set destroy_test
jleopold28 54ef87f
adding images
jleopold28 b08a234
fix ref
jleopold28 58224d7
adding support for deploy and destroy
jleopold28 c56c534
add import
jleopold28 a2dd1d4
remove dploy and destroy
jleopold28 b70e697
update readme
jleopold28 ca392ba
update docs
jleopold28 bc7e5cd
update docs, add images
jleopold28 955d198
fix filename
jleopold28 d37bf3d
change back to master
jleopold28 d3a537e
initial testing
jleopold28 ce6e4dc
update tests
jleopold28 018442e
update tests
jleopold28 4f66a7f
remove old import
jleopold28 013c886
change branch for tests
jleopold28 d176864
set to public
jleopold28 61360f7
switch branch
jleopold28 25e6e0e
testing remove get methods
jleopold28 9870f1a
fix tests, remove getMethods
jleopold28 c21259a
remove docs about destroy after deploy
jleopold28 7dfed77
change branch for testing
jleopold28 c7784ad
change back to masterg
jleopold28 59b6322
adding withArg support
jleopold28 a40b543
change apply branch
jleopold28 d41e27d
add arg to destroy command, not plan
jleopold28 c6a8f38
change branch to master
jleopold28 2866a8e
Adding DESTROY stage decorations for existing plugins
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 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 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 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 @@ | ||
## [DestroyPlugin](../src/DestroyPlugin.groovy) | ||
|
||
Enable this plugin to use `terraform destroy` to destroy your environment(s). | ||
|
||
When this plugin is enabled, the pipeline will follow these steps: | ||
1. Run a `terraform plan -destroy` to display which resources will get destroyed. | ||
2. Ask for human confirmation to proceed with the destroy. | ||
3. Run the `terraform destroy` command. | ||
|
||
|
||
``` | ||
// Jenkinsfile | ||
@Library(['terraform-pipeline@v3.10']) _ | ||
|
||
Jenkinsfile.init(this, env) | ||
|
||
// This enables the destroy functionality | ||
DestroyPlugin.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() | ||
``` | ||
|
||
When using this plugin, your pipeline will look something like this: | ||
|
||
![DestroyPluginPipeline](../images/destroy-pipeline.png) | ||
|
||
## Adding arguments to the destroy command | ||
|
||
You can use `withArgument("-some-arg")` to add arguments to the `terraform destroy` command. | ||
``` | ||
// Jenkinsfile | ||
@Library(['terraform-pipeline@v3.10']) _ | ||
|
||
Jenkinsfile.init(this, env) | ||
|
||
// This enables the destroy functionality | ||
// Set refresh to false for destroy command | ||
DestroyPlugin.withArgument("-refresh=false").init() | ||
|
||
... | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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 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 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 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,53 @@ | ||
import static TerraformEnvironmentStage.ALL | ||
import static TerraformEnvironmentStage.PLAN | ||
import static TerraformEnvironmentStage.CONFIRM | ||
import static TerraformEnvironmentStage.APPLY | ||
|
||
class DefaultStrategy { | ||
|
||
private TerraformInitCommand initCommand | ||
private TerraformPlanCommand planCommand | ||
private TerraformApplyCommand applyCommand | ||
private Jenkinsfile jenkinsfile | ||
|
||
public Closure createPipelineClosure(String environment, StageDecorations decorations) { | ||
initCommand = TerraformInitCommand.instanceFor(environment) | ||
planCommand = TerraformPlanCommand.instanceFor(environment) | ||
applyCommand = TerraformApplyCommand.instanceFor(environment) | ||
|
||
jenkinsfile = Jenkinsfile.instance | ||
|
||
return { -> | ||
node(jenkinsfile.getNodeName()) { | ||
deleteDir() | ||
checkout(scm) | ||
|
||
decorations.apply(ALL) { | ||
stage("${PLAN}-${environment}") { | ||
decorations.apply(PLAN) { | ||
sh initCommand.toString() | ||
sh planCommand.toString() | ||
} | ||
} | ||
|
||
decorations.apply("Around-${CONFIRM}") { | ||
stage("${CONFIRM}-${environment}") { | ||
decorations.apply(CONFIRM) { | ||
echo "Approved" | ||
} | ||
} | ||
} | ||
|
||
decorations.apply("Around-${APPLY}") { | ||
stage("${APPLY}-${environment}") { | ||
decorations.apply(APPLY) { | ||
sh initCommand.toString() | ||
sh applyCommand.toString() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
kmanning marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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,24 @@ | ||
class DestroyPlugin implements TerraformEnvironmentStagePlugin { | ||
|
||
private static arguments = [] | ||
|
||
public static void init() { | ||
DestroyPlugin plugin = new DestroyPlugin() | ||
|
||
ConfirmApplyPlugin.withConfirmMessage('WARNING! Are you absolutely sure the plan above is correct? Your environment will be IMMEDIATELY DESTROYED via "terraform destroy"') | ||
ConfirmApplyPlugin.withOkMessage("Run terraform DESTROY now") | ||
|
||
TerraformEnvironmentStage.addPlugin(plugin) | ||
} | ||
|
||
public static withArgument(String arg) { | ||
arguments << arg | ||
return this | ||
} | ||
|
||
@Override | ||
public void apply(TerraformEnvironmentStage stage) { | ||
stage.withStrategy(new DestroyStrategy(arguments)) | ||
} | ||
|
||
} | ||
kmanning marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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,64 @@ | ||
import static TerraformEnvironmentStage.ALL | ||
import static TerraformEnvironmentStage.PLAN | ||
import static TerraformEnvironmentStage.CONFIRM | ||
import static TerraformEnvironmentStage.DESTROY | ||
|
||
class DestroyStrategy { | ||
|
||
private TerraformInitCommand initCommand | ||
private TerraformPlanCommand planCommand | ||
private TerraformApplyCommand destroyCommand | ||
private Jenkinsfile jenkinsfile | ||
private List extraArguments | ||
|
||
DestroyStrategy(List args) { | ||
this.extraArguments = args | ||
} | ||
|
||
public Closure createPipelineClosure(String environment, StageDecorations decorations) { | ||
initCommand = TerraformInitCommand.instanceFor(environment) | ||
|
||
planCommand = TerraformPlanCommand.instanceFor(environment) | ||
planCommand = planCommand.withArgument("-destroy") | ||
|
||
destroyCommand = TerraformApplyCommand.instanceFor(environment) | ||
destroyCommand = destroyCommand.withCommand("destroy") | ||
for (arg in extraArguments) { | ||
destroyCommand = destroyCommand.withArgument(arg) | ||
} | ||
|
||
jenkinsfile = Jenkinsfile.instance | ||
|
||
return { -> | ||
node(jenkinsfile.getNodeName()) { | ||
deleteDir() | ||
checkout(scm) | ||
|
||
decorations.apply(ALL) { | ||
stage("${PLAN}-${DESTROY}-${environment}") { | ||
decorations.apply(PLAN) { | ||
sh initCommand.toString() | ||
sh planCommand.toString() | ||
} | ||
} | ||
|
||
decorations.apply("Around-${CONFIRM}") { | ||
stage("${CONFIRM}-${DESTROY}-${environment}") { | ||
decorations.apply(CONFIRM) { | ||
echo "Approved" | ||
} | ||
} | ||
} | ||
|
||
decorations.apply("Around-${DESTROY}") { | ||
stage("${DESTROY}-${environment}") { | ||
decorations.apply(DESTROY) { | ||
sh destroyCommand.toString() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
kmanning marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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 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 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
Oops, something went wrong.
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 touch on the screenshot - thank you for that.