Created
August 17, 2016 21:42
-
-
Save jfryman/bd5f47357b7ace8ece8ad504d7f78a83 to your computer and use it in GitHub Desktop.
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
node { | |
// Mark the code checkout 'Checkout'.... | |
stage 'Checkout' | |
// // Get some code from a GitHub repository | |
git url: '[email protected]:terraform/auth0-cloud.git' | |
// Get the Terraform tool. | |
def tfHome = tool name: 'terraform', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool' | |
env.PATH = "${tfHome}:${env.PATH}" | |
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) { | |
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'aws-playground-terraform', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID']]) { | |
// Mark the code build 'plan'.... | |
stage name: 'Plan', concurrency: 1 | |
// Output Terraform version | |
sh "terraform --version" | |
//Remove the terraform state file so we always start from a clean state | |
if (fileExists(".terraform/terraform.tfstate")) { | |
sh "rm -rf .terraform/terraform.tfstate" | |
} | |
if (fileExists("status")) { | |
sh "rm status" | |
} | |
sh "./init" | |
sh "terraform get" | |
sh "set +e; terraform plan -out=plan.out -detailed-exitcode; echo \$? > status" | |
def exitCode = readFile('status').trim() | |
def apply = false | |
echo "Terraform Plan Exit Code: ${exitCode}" | |
if (exitCode == "0") { | |
currentBuild.result = 'SUCCESS' | |
} | |
if (exitCode == "1") { | |
slackSend channel: '#ops-chatops', color: '#0080ff', message: "Plan Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} (https://jenkins.it.auth0.com/view/terraform/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/console)" | |
currentBuild.result = 'FAILURE' | |
} | |
if (exitCode == "2") { | |
stash name: "plan", includes: "plan.out" | |
slackSend channel: '#ops-chatops', color: 'good', message: "Plan Awaiting Approval: ${env.JOB_NAME} - ${env.BUILD_NUMBER} (https://jenkins.it.auth0.com/view/terraform/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/console)" | |
try { | |
input message: 'Apply Plan?', ok: 'Apply' | |
apply = true | |
} catch (err) { | |
slackSend channel: '#ops-chatops', color: 'warning', message: "Plan Discarded: ${env.JOB_NAME} - ${env.BUILD_NUMBER} (https://jenkins.it.auth0.com/view/terraform/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/console)" | |
apply = false | |
currentBuild.result = 'UNSTABLE' | |
} | |
} | |
// At this point in the build, we want to override the AWS credentials provided as part of CI | |
// and load up the credentials for our target environment. | |
if (apply) { | |
stage name: 'Apply', concurrency: 1 | |
unstash 'plan' | |
if (fileExists("status.apply")) { | |
sh "rm status.apply" | |
} | |
sh 'set +e; terraform apply plan.out; echo \$? > status.apply' | |
def applyExitCode = readFile('status.apply').trim() | |
if (applyExitCode == "0") { | |
slackSend channel: '#ops-chatops', color: 'good', message: "Changes Applied ${env.JOB_NAME} - ${env.BUILD_NUMBER} (https://jenkins.it.auth0.com/view/terraform/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/console)" | |
} else { | |
slackSend channel: '#ops-chatops', color: 'danger', message: "Apply Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} (https://jenkins.it.auth0.com/view/terraform/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/console)" | |
currentBuild.result = 'FAILURE' | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment