Created
March 11, 2019 18:22
-
-
Save andymotta/4a2ae0dadb112805b1f005c30346b537 to your computer and use it in GitHub Desktop.
Event-driven Cloudbees Operations Center Pipeline to run Terraform code with Marker file
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
def call () { | |
def map = [:] | |
def causeClass = currentBuild?.getBuildCauses()[0]?._class | |
if(causeClass == "com.cloudbees.jenkins.plugins.pipeline.events.EventTriggerCause") { | |
// This run was triggered by an event and not by a person | |
map = [ | |
event: currentBuild?.getBuildCauses()[0]?.event?.event?.toString(), | |
action: currentBuild?.getBuildCauses()[0]?.event?.action?.toString(), | |
awsprofile: currentBuild?.getBuildCauses()[0]?.event?.awsprofile?.toString(), | |
tfver: currentBuild?.getBuildCauses()[0]?.event?.tfver?.toString(), | |
debug: currentBuild?.getBuildCauses()[0]?.event?.debug?.toString() | |
] | |
} else { | |
// a person filled out the form in Jenkins | |
map = [ | |
action: params.action, | |
awsprofile: params.AWS_PROFILE, | |
tfver: params.TF_VER, | |
debug: params.DEBUG | |
] | |
} | |
return map | |
} |
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
def call(String actualValue,String defaultValue) { | |
def trimmedValue = actualValue?.trim() | |
if("null".equals(trimmedValue)) { | |
trimmedValue = null | |
} | |
boolean containsData = (trimmedValue) as boolean | |
if (containsData) { | |
return trimmedValue | |
} | |
return defaultValue?.trim() | |
} |
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
// pod template at the top with terraform and aws | |
// pod would have two containers with terraform and | |
def call() { | |
def tbparams=getEventValues() | |
def eventmessage="event=='terraform-"+env.BRANCH_NAME+"'" | |
pipeline { | |
agent { | |
kubernetes { | |
label 'pod-terraform' | |
defaultContainer 'jnlp' | |
yaml """ | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: terraform | |
spec: | |
containers: | |
- name: aws-cli | |
image: aws/aws-cli | |
command: | |
- cat | |
tty: true | |
volumeMounts: | |
- name: boto-creds | |
mountPath: /root/.aws | |
- name: terraform | |
image: hashicorp/terraform:latest | |
command: | |
- cat | |
tty: true | |
volumeMounts: | |
- name: boto-creds | |
mountPath: /root/.aws | |
volumes: | |
- name: boto-creds | |
configMap: | |
name: aws-config | |
""" | |
} | |
} | |
triggers { | |
eventTrigger jmespathQuery(eventmessage) | |
} | |
parameters { | |
choice( | |
choices: ['preview' , 'apply' , 'show', 'preview-destroy' , 'destroy'], | |
description: 'Terraform action to apply', | |
name: 'action') | |
choice ( | |
choices: ['devops' , 'dev' , 'sit' , 'prod' , 'lab'], | |
description: 'Target AWS Account', | |
name: 'AWS_PROFILE') | |
string(defaultValue: "latest", description: 'Which version of Terraform would you like to target?', name: 'TF_VER') | |
booleanParam(name: 'DEBUG', defaultValue: false, description: 'Terraform debugging') | |
} | |
stages { | |
stage('buildenv') { | |
steps { | |
container("terraform") { | |
script { | |
NAMESPACE = sh(script: "echo $GIT_URL | cut -d: -f2 | cut -d/ -f1 | tr -d '[:space:]'", returnStdout: true) | |
REPO = sh(script: "echo $GIT_URL | cut -d/ -f2 | cut -d. -f1", returnStdout: true) | |
} | |
} | |
container("aws-cli") { | |
script { // try not to use, use a custom step instead (vars / custom step) | |
if (tbparams.debug.equals('true')) { | |
env.TF_LOG = 'DEBUG' | |
} | |
env.ACCOUNT = sh(script: "aws sts get-caller-identity --output text --query \"Account\" --profile=${tbparams.awsprofile} | tr -d \"\n\"", returnStdout: true) | |
if (env.BRANCH_NAME.equals('master')) { | |
env.TF_VAR_stack_name = "${NAMESPACE}-${REPO}" | |
} | |
else { | |
env.TF_VAR_stack_name = "${NAMESPACE}-${REPO}-${BRANCH_NAME}" | |
} | |
} | |
} | |
} | |
} | |
stage('init') { | |
steps { | |
container("terraform") { | |
sh "terraform init -backend-config=\"bucket=${ACCOUNT}-tfstate\" -backend-config=\"key=${TF_VAR_stack_name}/terraform.tfstate\" -backend-config=\"region=us-west-2\" -var aws_profile=${tbparams.awsprofile}" | |
} | |
} | |
} | |
stage('validate') { | |
when { | |
expression { tbparams.action == 'preview' || tbparams.action == 'apply' || tbparams.action == 'destroy' } | |
} | |
steps { | |
container("terraform") { | |
sh "terraform validate -var aws_profile=${tbparams.awsprofile}" | |
} | |
} | |
} | |
stage('preview') { | |
when { | |
expression { tbparams.action == 'preview' } | |
} | |
steps { | |
container("terraform") { | |
sh "terraform plan -var aws_profile=${tbparams.awsprofile}" | |
} | |
} | |
} | |
stage('apply') { | |
when { equals expected: 'apply', actual: tbparams.action } | |
steps { | |
container("terraform") { | |
sh "terraform plan -out=plan -var aws_profile=${tbparams.awsprofile}" | |
sh 'terraform apply -auto-approve plan' | |
} | |
} | |
} | |
stage('show') { | |
when { | |
expression { tbparams.action == 'show' } | |
} | |
steps { | |
container("terraform") { | |
sh 'terraform show' | |
} | |
} | |
} | |
// stage('test') { | |
// steps { | |
// sh 'sleep 120' | |
// } | |
// } | |
stage('preview-destroy') { | |
when { | |
expression { tbparams.action == 'preview-destroy' } | |
} | |
steps { | |
container("terraform") { | |
sh "terraform plan -destroy -var aws_profile=${tbparams.awsprofile}" | |
} | |
} | |
} | |
stage('destroy') { | |
when { | |
expression { tbparams.action == 'destroy' } | |
} | |
steps { | |
container("terraform") { | |
sh "terraform destroy -force -var aws_profile=${tbparams.awsprofile}" | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment