Skip to content

Instantly share code, notes, and snippets.

@rbrto
Forked from gazoakley/Jenkinsfile
Created June 6, 2019 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbrto/ab05b54bcc3c3d9f78183962e271f367 to your computer and use it in GitHub Desktop.
Save rbrto/ab05b54bcc3c3d9f78183962e271f367 to your computer and use it in GitHub Desktop.
Jenkinsfile for running Terraform
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