How to use Terraform Plan and Apply in different Jenkins pipeline stagesHow to choose between Hudson and Jenkins?How to restart Jenkins manually?Jenkins Pipeline Wipe Out WorkspaceJenkins Pipeline Conditional Step/Stagejenkins pipeline: agent vs node?Jenkins scripted pipeline or declarative pipelineHow to reuse built sources from previous pipieline stagesHow to assign the output from the shell command to the variable in the groovy scriptLarge terraform pipeline in JenkinsShared Jenkins pipeline for Terraform
How does Asimov's second law deal with contradictory orders from different people?
How to power down external drive safely
Move label of an angle in Tikz
UX writing: When to use "we"?
Can living where rare earth magnetic ore is abundant provide any protection from cosmic radiation?
Who's behind community AMIs on Amazon EC2?
Is Norway in the Single Market?
Why is “deal 6 damage” a legit phrase?
"Fewer errors means better products" or "Fewer errors mean better products"?
Applied Meditation
Is this popular optical illusion made of a grey-scale image with coloured lines?
Access Denied to CiviMail Opt-Out Form
Can the additional attack from a Samurai fighter's Rapid Strike feature be made at advantage?
How to compare files with diffrent extensions and delete extra files?
Can it be useful for a player block with a hanging piece in a back rank mate situation?
Basic transistor circuit
Is the EU really banning "toxic propellants" in 2020? How is that going to work?
Applying for mortgage when living together but only one will be on the mortgage
Being told my "network" isn't PCI Complaint. I don't even have a server! Do I have to comply?
Password management for kids - what's a good way to start?
Can I say "Gesundheit" if someone is coughing?
Ernie and the Superconducting Boxes
Can I shorten this filter, that finds disk sizes over 100G?
How long should I wait to plug in my refrigerator after unplugging it?
How to use Terraform Plan and Apply in different Jenkins pipeline stages
How to choose between Hudson and Jenkins?How to restart Jenkins manually?Jenkins Pipeline Wipe Out WorkspaceJenkins Pipeline Conditional Step/Stagejenkins pipeline: agent vs node?Jenkins scripted pipeline or declarative pipelineHow to reuse built sources from previous pipieline stagesHow to assign the output from the shell command to the variable in the groovy scriptLarge terraform pipeline in JenkinsShared Jenkins pipeline for Terraform
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am working on a declarative Jenkins pipeline for Terraform deployments. I want to have the terraform init / select workspace / plan in one stage, ask for approval in another stage, and then do the apply in another stage. I have the agent at the top set to none and then using a kubernetes agent for a docker image we created that has packages we need for the stages. I am declaring those images in each stage. When I execute the pipeline, I get an error that I need to reinitialize Terraform in the apply stage even though I initialized in the init/plan stage. I figure this is nature of the stages running in different nodes.
I have it working by doing init / plan and stashing the plan. In the apply stage, it unstashes the plan, calls init / select workspace again, and then finally applies the unstashed plan.
I realize I could set the agent at the top, but according to Jenkins documentation, that is bad practice, as waiting for user input will block the execution.
I feel like there has to be a way to do this more elegantly. Any suggestions?
Here's my code:
def repositoryURL = env.gitlabSourceRepoHttpUrl != null && env.gitlabSourceRepoHttpUrl != "" ? env.gitlabSourceRepoHttpUrl : env.RepoURL
def repositoryBranch = env.gitlabTargetBranch != null && env.gitlabTargetBranch != "" ? env.gitlabTargetBranch : env.RepoBranch
def notificationEmail = env.gitlabUserEmail != null && env.gitlabUserEmail != "" ? env.gitlabSourceRepoHttpUrl : env.Email
def projectName = env.ProjectName
def deployAccountId = env.AccountId
pipeline
agent none
stages
stage("Checkout")
agent any
steps
git branch: "$repositoryBranch", credentialsId: '...', url: "$repositoryURL"
stash name: 'tf', useDefaultExcludes: false
stage("Terraform Plan")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container('cis')
unstash 'tf'
script
sh "terraform init"
try
sh "terraform workspace select $deployAccountId_$projectName_$repositoryBranch"
catch (Exception e)
sh "terraform workspace new $deployAccountId_$projectName_$repositoryBranch"
sh "terraform plan -out=$deployAccountId_$projectName_$repositoryBranch_plan.tfplan -input=false"
stash includes: "*.tfplan" name: "tf-plan", useDefaultExcludes: false
post
success
echo "Terraform init complete"
failure
echo "Terraform init failed"
stage ("Terraform Plan Approval")
agent none
steps
script
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
stage ("Terraform Apply")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container("cis")
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'my-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]])
script jq .Credentials.AccessKeyId
jenkins continuous-integration jenkins-pipeline terraform
add a comment |
I am working on a declarative Jenkins pipeline for Terraform deployments. I want to have the terraform init / select workspace / plan in one stage, ask for approval in another stage, and then do the apply in another stage. I have the agent at the top set to none and then using a kubernetes agent for a docker image we created that has packages we need for the stages. I am declaring those images in each stage. When I execute the pipeline, I get an error that I need to reinitialize Terraform in the apply stage even though I initialized in the init/plan stage. I figure this is nature of the stages running in different nodes.
I have it working by doing init / plan and stashing the plan. In the apply stage, it unstashes the plan, calls init / select workspace again, and then finally applies the unstashed plan.
I realize I could set the agent at the top, but according to Jenkins documentation, that is bad practice, as waiting for user input will block the execution.
I feel like there has to be a way to do this more elegantly. Any suggestions?
Here's my code:
def repositoryURL = env.gitlabSourceRepoHttpUrl != null && env.gitlabSourceRepoHttpUrl != "" ? env.gitlabSourceRepoHttpUrl : env.RepoURL
def repositoryBranch = env.gitlabTargetBranch != null && env.gitlabTargetBranch != "" ? env.gitlabTargetBranch : env.RepoBranch
def notificationEmail = env.gitlabUserEmail != null && env.gitlabUserEmail != "" ? env.gitlabSourceRepoHttpUrl : env.Email
def projectName = env.ProjectName
def deployAccountId = env.AccountId
pipeline
agent none
stages
stage("Checkout")
agent any
steps
git branch: "$repositoryBranch", credentialsId: '...', url: "$repositoryURL"
stash name: 'tf', useDefaultExcludes: false
stage("Terraform Plan")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container('cis')
unstash 'tf'
script
sh "terraform init"
try
sh "terraform workspace select $deployAccountId_$projectName_$repositoryBranch"
catch (Exception e)
sh "terraform workspace new $deployAccountId_$projectName_$repositoryBranch"
sh "terraform plan -out=$deployAccountId_$projectName_$repositoryBranch_plan.tfplan -input=false"
stash includes: "*.tfplan" name: "tf-plan", useDefaultExcludes: false
post
success
echo "Terraform init complete"
failure
echo "Terraform init failed"
stage ("Terraform Plan Approval")
agent none
steps
script
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
stage ("Terraform Apply")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container("cis")
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'my-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]])
script jq .Credentials.AccessKeyId
jenkins continuous-integration jenkins-pipeline terraform
I think you need to runinitagain because your stash does not include the.terraform/folder.
– StephenKing
Mar 28 at 19:20
Other that that, the usage of Terraform looks fine (or don't you need an-auto-approvefor theapplystep?
– StephenKing
Mar 28 at 19:21
add a comment |
I am working on a declarative Jenkins pipeline for Terraform deployments. I want to have the terraform init / select workspace / plan in one stage, ask for approval in another stage, and then do the apply in another stage. I have the agent at the top set to none and then using a kubernetes agent for a docker image we created that has packages we need for the stages. I am declaring those images in each stage. When I execute the pipeline, I get an error that I need to reinitialize Terraform in the apply stage even though I initialized in the init/plan stage. I figure this is nature of the stages running in different nodes.
I have it working by doing init / plan and stashing the plan. In the apply stage, it unstashes the plan, calls init / select workspace again, and then finally applies the unstashed plan.
I realize I could set the agent at the top, but according to Jenkins documentation, that is bad practice, as waiting for user input will block the execution.
I feel like there has to be a way to do this more elegantly. Any suggestions?
Here's my code:
def repositoryURL = env.gitlabSourceRepoHttpUrl != null && env.gitlabSourceRepoHttpUrl != "" ? env.gitlabSourceRepoHttpUrl : env.RepoURL
def repositoryBranch = env.gitlabTargetBranch != null && env.gitlabTargetBranch != "" ? env.gitlabTargetBranch : env.RepoBranch
def notificationEmail = env.gitlabUserEmail != null && env.gitlabUserEmail != "" ? env.gitlabSourceRepoHttpUrl : env.Email
def projectName = env.ProjectName
def deployAccountId = env.AccountId
pipeline
agent none
stages
stage("Checkout")
agent any
steps
git branch: "$repositoryBranch", credentialsId: '...', url: "$repositoryURL"
stash name: 'tf', useDefaultExcludes: false
stage("Terraform Plan")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container('cis')
unstash 'tf'
script
sh "terraform init"
try
sh "terraform workspace select $deployAccountId_$projectName_$repositoryBranch"
catch (Exception e)
sh "terraform workspace new $deployAccountId_$projectName_$repositoryBranch"
sh "terraform plan -out=$deployAccountId_$projectName_$repositoryBranch_plan.tfplan -input=false"
stash includes: "*.tfplan" name: "tf-plan", useDefaultExcludes: false
post
success
echo "Terraform init complete"
failure
echo "Terraform init failed"
stage ("Terraform Plan Approval")
agent none
steps
script
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
stage ("Terraform Apply")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container("cis")
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'my-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]])
script jq .Credentials.AccessKeyId
jenkins continuous-integration jenkins-pipeline terraform
I am working on a declarative Jenkins pipeline for Terraform deployments. I want to have the terraform init / select workspace / plan in one stage, ask for approval in another stage, and then do the apply in another stage. I have the agent at the top set to none and then using a kubernetes agent for a docker image we created that has packages we need for the stages. I am declaring those images in each stage. When I execute the pipeline, I get an error that I need to reinitialize Terraform in the apply stage even though I initialized in the init/plan stage. I figure this is nature of the stages running in different nodes.
I have it working by doing init / plan and stashing the plan. In the apply stage, it unstashes the plan, calls init / select workspace again, and then finally applies the unstashed plan.
I realize I could set the agent at the top, but according to Jenkins documentation, that is bad practice, as waiting for user input will block the execution.
I feel like there has to be a way to do this more elegantly. Any suggestions?
Here's my code:
def repositoryURL = env.gitlabSourceRepoHttpUrl != null && env.gitlabSourceRepoHttpUrl != "" ? env.gitlabSourceRepoHttpUrl : env.RepoURL
def repositoryBranch = env.gitlabTargetBranch != null && env.gitlabTargetBranch != "" ? env.gitlabTargetBranch : env.RepoBranch
def notificationEmail = env.gitlabUserEmail != null && env.gitlabUserEmail != "" ? env.gitlabSourceRepoHttpUrl : env.Email
def projectName = env.ProjectName
def deployAccountId = env.AccountId
pipeline
agent none
stages
stage("Checkout")
agent any
steps
git branch: "$repositoryBranch", credentialsId: '...', url: "$repositoryURL"
stash name: 'tf', useDefaultExcludes: false
stage("Terraform Plan")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container('cis')
unstash 'tf'
script
sh "terraform init"
try
sh "terraform workspace select $deployAccountId_$projectName_$repositoryBranch"
catch (Exception e)
sh "terraform workspace new $deployAccountId_$projectName_$repositoryBranch"
sh "terraform plan -out=$deployAccountId_$projectName_$repositoryBranch_plan.tfplan -input=false"
stash includes: "*.tfplan" name: "tf-plan", useDefaultExcludes: false
post
success
echo "Terraform init complete"
failure
echo "Terraform init failed"
stage ("Terraform Plan Approval")
agent none
steps
script
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
stage ("Terraform Apply")
agent
kubernetes
label 'myagent'
containerTemplate
name 'cis'
image 'docker-local.myrepo.com/my-image:v2'
ttyEnabled true
command 'cat'
steps
container("cis")
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'my-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]])
script jq .Credentials.AccessKeyId
jenkins continuous-integration jenkins-pipeline terraform
jenkins continuous-integration jenkins-pipeline terraform
asked Mar 27 at 0:22
Aaron SandersAaron Sanders
4661 gold badge5 silver badges19 bronze badges
4661 gold badge5 silver badges19 bronze badges
I think you need to runinitagain because your stash does not include the.terraform/folder.
– StephenKing
Mar 28 at 19:20
Other that that, the usage of Terraform looks fine (or don't you need an-auto-approvefor theapplystep?
– StephenKing
Mar 28 at 19:21
add a comment |
I think you need to runinitagain because your stash does not include the.terraform/folder.
– StephenKing
Mar 28 at 19:20
Other that that, the usage of Terraform looks fine (or don't you need an-auto-approvefor theapplystep?
– StephenKing
Mar 28 at 19:21
I think you need to run
init again because your stash does not include the .terraform/ folder.– StephenKing
Mar 28 at 19:20
I think you need to run
init again because your stash does not include the .terraform/ folder.– StephenKing
Mar 28 at 19:20
Other that that, the usage of Terraform looks fine (or don't you need an
-auto-approve for the apply step?– StephenKing
Mar 28 at 19:21
Other that that, the usage of Terraform looks fine (or don't you need an
-auto-approve for the apply step?– StephenKing
Mar 28 at 19:21
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368041%2fhow-to-use-terraform-plan-and-apply-in-different-jenkins-pipeline-stages%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368041%2fhow-to-use-terraform-plan-and-apply-in-different-jenkins-pipeline-stages%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think you need to run
initagain because your stash does not include the.terraform/folder.– StephenKing
Mar 28 at 19:20
Other that that, the usage of Terraform looks fine (or don't you need an
-auto-approvefor theapplystep?– StephenKing
Mar 28 at 19:21