Skip to content

Instantly share code, notes, and snippets.

Last active Aug 17, 2018
What would you like to do?
Jenkinsfile for component banjo
def label = UUID.randomUUID().toString()
podTemplate(label: label, cloud: 'local cluster',
containers: [
name: 'test',
image: 'jenkins-slave-k8s',
ttyEnabled: true,
command: 'cat')
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
hostPathVolume(hostPath: '/usr/bin/docker', mountPath: '/usr/bin/docker'),
hostPathVolume(hostPath: '/tmp/workspace', mountPath: '/root/workspace')
) {
node(label) {
container('test') {
def project = ''
def appName = ''
def imageTag = "${project}/${appName}:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
def prodChartRepo = ""
def stageChartRepo = ""
// Holds the test result status
def result
stage("Fetch dependent images") {
notifyBuild('Build started', 'GREEN')
sh("gcloud docker -- pull python:3.6.3-1")
sh("gcloud docker -- pull postgres:test")
sh("gcloud docker -- pull redis:test")
stage("Build image") {
checkout scm
sh("docker build -f Dockerfile -t ${imageTag} .")
env.GIT_TAG_NAME = gitTagName()
env.GIT_TAG_MESSAGE = gitTagMessage()
stage("Test image") {
sh("docker run -d --name postgres-${appName}-${env.BRANCH_NAME}-${env.BUILD_ID} -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password postgres:test")
sh("docker run -d --name redis-${appName}-${env.BRANCH_NAME}-${env.BUILD_ID} redis:test")
sh ("sleep 10")
def dbId = sh (
script: "docker ps -aqf name=postgres-${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}",
returnStdout: true
def redisId = sh (
script: "docker ps -aqf name=redis-${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}",
returnStdout: true
sh("docker network create ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}-network")
sh("docker network connect --alias postgres ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}-network ${dbId}")
sh("docker network connect --alias redis ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}-network ${redisId}")
result = sh (
script: "docker run --network ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}-network --name ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID} ${imageTag} /bin/bash -c \"mv banjo/ banjo/ && python test\"",
returnStatus: true
sh("docker stop ${dbId}")
sh("docker stop ${redisId}")
sh("docker rm ${dbId}")
sh("docker rm ${redisId}")
sh("docker network rm ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}-network")
if (result == 0) {
notifyBuild('Tests passed', 'GREEN')
else {
notifyBuild('Tests failing', 'RED')
stage("Pushing image") {
if ("${env.BRANCH_NAME}" == "master") {
if (result == 0) {
sh("gcloud docker -- push ${imageTag}")
notifyBuild('Pushing image to registry', 'GREEN')
if (forProduction() == true) {
def productionTag = "${project}/${appName}:${env.GIT_TAG_NAME}"
echo "Image for production ${productionTag}"
if (result == 0) {
sh("docker tag ${imageTag} ${productionTag}")
sh("gcloud docker -- push ${productionTag}")
notifyBuild("Image ${productionTag} pushed for release", 'GREEN')
// Create chart for stage and prod
dir("charts") {
sh "helm init --client-only"
// Replace container tag
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo/values/prod_values.yaml"""
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo-celery/values/prod_values.yaml"""
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo-cronjob/values/prod_values.yaml"""
// For production
// Move prod_values file to values
sh("mv banjo/values/prod_values.yaml banjo/values.yaml")
sh("""helm package banjo --version ${env.GIT_TAG_NAME} banjo""")
sh("mv banjo-celery/values/prod_values.yaml banjo-celery/values.yaml")
sh("""helm package banjo-celery --version ${env.GIT_TAG_NAME} banjo-celery""")
sh("mv banjo-cronjob/values/prod_values.yaml banjo-cronjob/values.yaml")
sh("""helm package banjo-cronjob --version ${env.GIT_TAG_NAME} banjo-cronjob""")
// Push to chart repository
sh("""curl -vvv --data-binary "@banjo-${env.GIT_TAG_NAME}.tgz" ${prodChartRepo}""")
sh("""curl -vvv --data-binary "@banjo-celery-${env.GIT_TAG_NAME}.tgz" ${prodChartRepo}""")
sh("""curl -vvv --data-binary "@banjo-cronjob-${env.GIT_TAG_NAME}.tgz" ${prodChartRepo}""")
// For staging
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo/values/stage_values.yaml"""
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo-celery/values/stage_values.yaml"""
sh """sed -i \"s/<container_tag_replace>/${env.GIT_TAG_NAME}/g\" banjo-cronjob/values/stage_values.yaml"""
// Move stage_values file to values
sh("mv banjo/values/stage_values.yaml banjo/values.yaml")
sh("""helm package banjo --version ${env.GIT_TAG_NAME}-stage banjo""")
sh("mv banjo-celery/values/stage_values.yaml banjo-celery/values.yaml")
sh("""helm package banjo-celery --version ${env.GIT_TAG_NAME}-stage banjo-celery""")
sh("mv banjo-cronjob/values/stage_values.yaml banjo-cronjob/values.yaml")
sh("""helm package banjo-cronjob --version ${env.GIT_TAG_NAME}-stage banjo-cronjob""")
sh("""curl -vvv --data-binary "@banjo-${env.GIT_TAG_NAME}-stage.tgz" ${stageChartRepo}""")
sh("""curl -vvv --data-binary "@banjo-celery-${env.GIT_TAG_NAME}-stage.tgz" ${stageChartRepo}""")
sh("""curl -vvv --data-binary "@banjo-cronjob-${env.GIT_TAG_NAME}-stage.tgz" ${stageChartRepo}""")
notifyBuild("Charts ready to deploy", 'GREEN')
stage("Cleanup") {
// Delete images
sh ("docker stop ${appName}-${env.BRANCH_NAME}-${env.BUILD_ID}")
sh ("docker rmi -f ${imageTag}")
if (forProduction() == true) {
def productionTag = "${project}/${appName}:${env.GIT_TAG_NAME}"
sh ("docker rmi -f ${productionTag}")
if (result != 0) {
currentBuild.result = 'UNSTABLE'
// ********** Helper functions *********** //
def forProduction() {
def matcher = "${env.GIT_TAG_NAME}" =~ /^[0-9]+\.[0-9]+\.[0-9]+/
if( matcher.matches() ) {
return true
return false
/** @return The tag name, or `null` if the current commit isn't a tag. */
String gitTagName() {
commit = getCommit()
if (commit) {
try {
desc = sh(script: "git describe --tags ${commit}", returnStdout: true)?.trim()
if (isTag(desc)) {
return desc
} catch (all) {
return null
return null
/** @return The tag message, or `null` if the current commit isn't a tag. */
String gitTagMessage() {
name = gitTagName()
msg = sh(script: "git tag -n10000 -l ${name}", returnStdout: true)?.trim()
if (msg) {
return msg.substring(name.size()+1, msg.size())
return null
String getCommit() {
return sh(script: 'git rev-parse HEAD', returnStdout: true)?.trim()
boolean isTag(String desc) {
match = desc =~ /.+-[0-9]+-g[0-9A-Fa-f]{6,}$/
result = !match
match = null // prevent serialisation
return result
def notifyBuild(String buildStatus, String color) {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def subject = "${buildStatus}: Job '${env.JOB_NAME} [ Branch: ${env.BRANCH_NAME} - ${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
// Override default values based on build status
if (color == 'YELLOW') {
colorCode = '#FFFF00'
} else if (color == 'GREEN') {
colorCode = '#00FF00'
} else {
colorCode = '#FF0000'
// Send notifications
slackSend (color: colorCode, message: summary)

This comment has been minimized.

Copy link

@shashankmehra shashankmehra commented Aug 17, 2018
The command needs to have pull as a parameter. Also cant you express the full path of the docker image using groovy variables like project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment