Jenkins Pipeline

Jenkinsfiles

  • Jenkinsfiles are where you can script your deploy.  It's just called "Jenkinsfile" and it's in your project's root directory.
  • Language: Groovy (runs on JVM but is extremely dynamic)
  • Config-as-code
  • Cannot be locally tested, iterative development is hard and generates a lot of side-effects

Environment

  • The Kraken
  • Make sure to specify node("kraken") in Jenkinsfile
  • Kraken is spun up and down as necessary, no need to manually start it (just be patient).

Lifecycle of my deploy

  • Checkout step
  • Build docker image
  • Test the image
  • Push to Pierone
  • Register new version in KIO
  • Deploy using Lizzy

Checkout

stage('Checkout from scm') {
    checkout scm

    gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    shortCommit = gitCommit.take(6)
    packageJSON = readFile "package.json"
    version = new groovy.json.JsonSlurper().parseText(packageJSON).version

    if(env.BRANCH_NAME == "develop") {
        tag = "${version}-${env.BUILD_NUMBER}"
    } else if(env.BRANCH_NAME == "master") {
        tag = "${version}"
    } else {
        tag = "${version}-${shortCommit}"
    }

    fullImageName = "pierone.stups.zalan.do/metrigo/awesome-frontend:${tag}"
}

Build

stage('Build Image') {
    sh "docker build -t $fullImageName ."
}

Test

stage('Test Image') {
    // if we fail to stop the container because the name doesn't exist, we don't care - keep going.
    sh "docker stop ${containerName} || true"
    sh "docker rm ${containerName} || true"
    sh "docker run  -dt --name ${containerName} ${fullImageName}"
    sh "docker exec -t ${containerName} bash -c \"cd src && env CI=true npm test\""
    def dockerIp = sh (
        script: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${containerName}",
        returnStdout: true
    ).trim()
    sh "docker logs $containerName"
    sh "curl http://$dockerIp:8080/api/health"
}

<Some magic missing>

The next steps only happen if we are on develop or master.

This is achieved by comparing env.BRANCH_NAME to "master" or "develop".

Push

stage('Push image to pierone') {
    sh "/tools/run :stups -- pierone login --url pierone.stups.zalan.do"
    sh "docker push ${fullImageName}"
}

Register

stage('Deploy image to KIO (aka yourturn)') {
    sh "/tools/run :stups -e KIO_URL=$KIO_URL -- kio versions \
        create $KIO_APP_NAME $version $fullImageName"
}

Deploy

stage ('Create new stack in senza using lizzy, \
        and push traffic over immediately, \
        and clean up old stacks') {

    sh "/tools/run :lizzy -e LIZZY_URL=$LIZZY_URL -- lizzy create \
        -v --traffic 100 --keep-stacks 0 --region $AWS_REGION \
        $SENZA_CONFIG_PATH $stack_version $tag"

}

Done

Questions?

Copy of Jenkins CD

By Tarun Sharma

Copy of Jenkins CD

  • 925