Skip to content

Jenkins Pipelines

Use Jenkins continuous delivery pipelines to build and publish container.

You will be Jenkins to build a containerized Java Springboot app.

Create Jenkinsfile

The following is a skeleton of the Jenkinsfile

library 'jenkins-pipeline'

pipeline {
  agent {
  }
  options {
  }
  environment {
  }
  stages {
    stage(''){
      steps {
      }
    }
  }
  post {
  }
}

Agent

What would the agent section look like for this Maven application? Using an ephemeral Jenkins instance on a Kubernetes cluster, we need to specify an agent with the appropriate pod template.

Agent Solution
agent { 
  kubernetes {
    label 'pipeline-maven'
  }
}

Options

Open the Jenkins documentation to add the following two pieces to 'options'.

  • We would like to create a timeout for two hours.
  • We would like to keep a maximum of 20 logs and discard the oldest.
Options Solution
options {
  timeout( time: 2, unit: 'HOURS' )
  buildDiscarder( logRotator( numToKeepStr: '20' ) )
}

Environment

Environment Information

  • Which chat room do you want to send information to?
  • What project are you deploying to?
  • What is the name of your application? This is what your image should be called.
  • What group are you deploying to?
  • Hint: Same as Mnemonic
  • What is the name of the artifact?
    • Hint: Same as imageName
  • Which docker repository are we deploying to?
  • What is the docker tag?
    • Hint: Mixture of version + the current commit
  • What is the name of your module?
    • Hint: Same as imageName
  • What which repository was we doing an automated deployment?
    • Hint: Has not yet been created, but is the same name as this repo + 'deploy'
  • What is the target branch we're deploying to automatically?
  • What is the version of the docker image?
    • Hint: Same as version
Environment Solution
DEPLOY_PROJECT            = 'sandbox'
imageName                 = 'demo-app'
ROOM                      = 'Dojo-Sandbox'
ARTIFACT                  = '${env.imageName}'
DOCKER_REPO               = 'docker-stage.liatr.io'
DOCKER_TAG                = '1.0.1-${env.GIT_COMMIT}'
DEPLOY_REPO               = 'git.liatr.io/san/deploy.git'
TARGET_BRANCH             = 'stage-dev'
GROUP                     = '${env.LOB}'
VERSION                   = '${env.DOCKER_TAG}'

Stages

The following stages are pieces we will need to build out.

  • Initialization: Set up dependencies and variables to prepare the pipeline
  • Build: Build a maven application
  • Sonar Scan: Run sonar scan for code quality
  • Build Image: Build the container image
  • Update Deploy Repo Manifest
  • create a 'deploy-repo-update' container
    • It will include the following:
    • module
    • target branch
    • group
    • version
    • manifest repo
Stages Solutions
stages {
  // Required: setup dependencies and variables to prepare the pipeline
  stage('Initialization') {
    steps {
      initialization()
    }
  }

  stage('Build') {
    steps {
      // Default: `mvn -s path/to/settings.xml -B clean install`
      buildMaven()
    }
  }

  stage('Sonar Scan') {
    steps {
      // Execute `sonar-scanner` with default parameters
      sonar()
    }
  }
        
  stage('Build Image') {
    steps {
      //  Default: `docker build -t ${config.docker.repo}/${image}:${tag} ${dir} --build-arg version=${tag}`
        buildImage()
    }
  } 

  stage('Update Deploy Manifest') {
    when { expression { env.GIT_BRANCH == env.PRIMARY_BRANCH } }
    steps{
      container(name: 'deploy-repo-update' ){
        // Update artifact version in deploy repo manifest.yml 
        updateDeployRepoWithContainer(
          module: env.MODULE,
          targetBranch:env.TARGET_BRANCH,
          group: env.GROUP,
          version: env.VERSION,
          manifestRepo: env.MANIFEST_REPO
        )
      }
    }
  }
}

Post

Send the following build information to your mattermost channel

  • Success
  • Failure
  • Regression
  • Unstable
  • Fixed
  • Always
Post Solution
post {
  success {
    script{
      chatops.sendSuccess()
    }
  }
  failure {
    script{
      chatops.sendFailure()
    }
  }
  regression {
    script {
      event.sendUnhealthy()
    }
  }
  unstable {
    script{
      chatops.sendUnstable()
    }
  }
  fixed {
    script{
      chatops.sendFixed()
      event.sendHealthy()
    }
  }
  always {
    generalCleanup()
  }
}