Last active
July 13, 2023 16:11
-
-
Save keith-miller/c1f448029855a032f18b6bc0ec9b7c57 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
pipeline { | |
agent none | |
options { | |
disableConcurrentBuilds() | |
} | |
stages { | |
stage('checkout_repo') { | |
agent { | |
label "${params.AGENT}" | |
} | |
steps { | |
checkout perforce( | |
credential: 'perforce', | |
populate: autoClean( | |
delete: true, | |
modtime: false, | |
parallel: [enable: false, minbytes: '1024', minfiles: '1', threads: '4'], | |
pin: "${params.CHANGELIST}", | |
quiet: true, | |
replace: true, | |
tidy: false), | |
workspace: streamSpec( | |
charset: 'none', | |
format: 'jenkins-${NODE_NAME}-${JOB_NAME}', | |
pinHost: false, | |
streamName: "//Repo/${params.STREAM}" | |
) | |
) | |
} | |
} | |
stage("deploy_stage") { | |
steps { | |
run_terraform(params.MODULES.split(), "${params.AGENT}", "${params.ENV}", "${env.JOB_NAME}", "${env.BUILD_NUMBER}", "${env.BUILD_URL}") | |
} | |
} | |
} | |
} | |
def run_terraform(list, agent, env, jobName, jobNumber, jobUrl) { | |
for (int i = 0; i < list.size(); i++) { | |
node(agent) { | |
// build path | |
String workspace = sh( | |
script: 'pwd', | |
returnStdout: true | |
).trim() | |
String fullPath = "${workspace}/Terraform/modules/${list[i]}" as String | |
// build key | |
String key = "${env}/tfstate/${list[i]}/terraform.tfstate" | |
// clean up files | |
if (fileExists("${fullPath}/.terraform/terraform.tfstate")) { | |
sh "rm -rf ${fullPath}/.terraform/terraform.tfstate" | |
} | |
if (fileExists("${fullPath}/terraform.tfstate.backup")) { | |
sh "rm -rf ${fullPath}/terraform.tfstate.backup" | |
} | |
dir(fullPath) { | |
ansiColor('xterm') { | |
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'Terraform']]) { | |
// terraform init and get | |
sh "terraform init -backend=true -get=true -backend-config=\"../../s3_backend.tfvars\" -backend-config=\"key=${key}\"" | |
int exitCode = sh( | |
script: "terraform plan -detailed-exitcode -var-file=\"../../global.tfvars\" -var-file=\"../../${env}.tfvars\"", | |
returnStatus: true | |
) | |
def apply = false | |
switch (exitCode) { | |
case 1: | |
// failed, let's hard stop | |
throw new Exception('Terraform run failed') | |
case 2: | |
slackSend color: 'good', message: "Terraform plan Awaiting Approval: ${jobName} - <${jobUrl}|${jobNumber}>" | |
try { | |
input message: 'Apply Plan?', ok: 'Apply' | |
apply = true | |
} catch (err) { | |
slackSend color: 'warning', message: "Terraform plan ignored: ${jobName} - <${jobUrl}|${jobNumber}>" | |
apply = false | |
} | |
break | |
case 0: | |
default: | |
break | |
} | |
if(apply) { | |
sh "terraform apply -auto-approve -var-file=\"../../global.tfvars\" -var-file=\"../../${env}.tfvars\"" | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment