Last active
June 18, 2019 16:10
-
-
Save mrballcb/211b61bcd1a7a61c62f4faef5c760f38 to your computer and use it in GitHub Desktop.
Sample Jenkinsfile - There are external library functions used here but not shown
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 label = "buildpod.${env.JOB_NAME.replace('%2F','_').reverse().take(38).reverse()}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_') | |
def buildCloudName = "foo.cloud" | |
def flatBranchName = BRANCH_NAME.replace("/", "-") | |
def CHECKOUTPREFIX = "CHART" | |
def CHARTPATH = "${CHECKOUTPREFIX}/chart" | |
podTemplate( | |
label: label, | |
cloud: buildCloudName, | |
containers: [ | |
// This is an image we have built with jdk and maven3 | |
containerTemplate(name: 'jdk-maven3', image: 'java-maven3:latest', | |
alwaysPullImage:true, ttyEnabled: true, command: 'cat', | |
envVars: [containerEnvVar(key: 'BRANCH_NAME', value: flatBranchName)]), | |
// This is an image we have built with the versions of kubectl and helm we need | |
containerTemplate(name: 'kubectl-helm', image: 'kubectl-helm:latest', | |
alwaysPullImage:true, ttyEnabled: true, command: 'cat') | |
], | |
volumes: [ | |
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock') | |
], | |
imagePullSecrets: [ 'docker-secret' ], | |
) { | |
node(label) { | |
// Stores several git related vars in scmInfo object | |
scmInfo = checkout scm | |
... | |
println "CONTAINER TAG IS: ${CONTAINER_TAG}" | |
container('jdk-maven3') { | |
try{ | |
stage('Clean') { | |
mavenClean("${ARGS}") | |
} | |
} catch (e) { | |
currentBuild.rawBuild.result = Result.FAILURE | |
throw new hudson.AbortException('MVN CLEAN FAILURE') | |
} | |
try{ | |
stage('Package') { | |
mavenPackage("${ARGS}") | |
} | |
} catch (e) { | |
currentBuild.rawBuild.result = Result.FAILURE | |
throw new hudson.AbortException('MVN PACKAGE FAILURE') | |
} | |
} | |
container('kubectl-helm') { | |
try{ | |
checkout([$class: 'GitSCM', | |
submoduleCfg: [], | |
doGenerateSubmoduleConfigurations: false, | |
extensions: [[$class: 'CleanBeforeCheckout'], [$class: 'RelativeTargetDirectory', relativeTargetDir: CHECKOUTPREFIX]], | |
branches: [[name: scmInfo.GIT_COMMIT ]], | |
userRemoteConfigs: [[credentialsId: 'jenkins-pgitools', url: scmInfo.GIT_URL]] ]) | |
configureHelm() | |
stage('Helm Package') { | |
def files = [ "${CHARTPATH}/values.yaml" ] | |
chartfile = "${CHARTPATH}/Chart.yaml" | |
rewriteImageTag(files, CONTAINER_TAG) | |
rewriteChartVersion(chartfile, CONTAINER_TAG) | |
packageAndPublishChartWithPrefix(CHECKOUTPREFIX,CHARTPATH,true) | |
} | |
} catch (e) { | |
currentBuild.rawBuild.result = Result.FAILURE | |
throw e | |
} | |
try { | |
stage('Helm Deployment') { | |
switch (BRANCH_NAME) | |
{ | |
case "develop": | |
build job: 'app-deploy', | |
parameters: [ string(name: 'CLUSTER', value: 'bar'), | |
string(name: 'ENVIRONMENT', value: 'dev'), | |
string(name: 'DEPLOY_TAG', value: CONTAINER_TAG), | |
string(name: 'HELMBRANCH', value: BRANCH_NAME) ] | |
break | |
default: | |
println "Not deploying anywhere for branch ${BRANCH_NAME}" | |
break | |
} | |
} | |
} catch (e) { | |
currentBuild.rawBuild.result = Result.FAILURE | |
# throw new hudson.AbortException('HELM DEPLOYMENT FAILURE') | |
throw e | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment