-
-
Save rbrto/ab05b54bcc3c3d9f78183962e271f367 to your computer and use it in GitHub Desktop.
Jenkinsfile for running Terraform
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
pipeline { | |
agent any | |
environment { | |
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID') | |
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY') | |
TF_IN_AUTOMATION = '1' | |
} | |
stages { | |
stage('Plan') { | |
steps { | |
script { | |
currentBuild.displayName = "${version}" | |
} | |
sh 'terraform init -input=false' | |
sh 'terraform workspace select ${environment}' | |
sh "terraform plan -input=false -out tfplan -var 'version=${version}' --var-file=environments/${environment}.tfvars" | |
sh 'terraform show -no-color tfplan > tfplan.txt' | |
} | |
} | |
stage('Approval') { | |
when { | |
not { | |
equals expected: true, actual: params.autoApprove | |
} | |
} | |
steps { | |
script { | |
def plan = readFile 'tfplan.txt' | |
input message: "Do you want to apply the plan?", | |
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] | |
} | |
} | |
} | |
stage('Apply') { | |
steps { | |
sh "terraform apply -input=false tfplan" | |
} | |
} | |
} | |
post { | |
always { | |
archiveArtifacts artifacts: 'tfplan.txt' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment