Building a declarative Jenkinsfile for Continuous Delivery

BLOG
X
min read

Continuous Integration(CI) methodologies help developers automate code development so that code can be pushed more frequently which leads to better collaboration and faster software delivery, Continuous Delivery (CD) methodologies automate the delivery of application on infrastructures such as servers, this reduces the load on developers as they only have to worry about developing the code and pushing it onto a repository while the deployment of said code is all automated.


At the heart of continuous integration and delivery lies a powerhouse called Jenkins, Jenkins is an automation tool that lets you build and test deployments of your developed code while maintaining a build log to trace all changes made.


Jenkins can integrate with a lot of plugins such as Git to pull all changes from a repository or maven to build the code automatically.

Jenkins can also run your tests and provide code coverage and code reports by integrating with plugins like Sonarqube so that all commits made to a repository are covered and tested and all decreases/ increases in code coverage can be traced to each commit made to the repository.

So how do I integrate with such plugins in my Jenkins deployments?

In comes the Jenkinsfile, Jenkinsfile is a text file that is written in Groovy syntax that lets you identify the plugins used and the commands that should run during your deployment.

I will be showing you a run-through of a Jenkinsfile we use in our manual deployments while showcasing the basics of a Jenkins file.

1) The first thing we do is to identify an agent which instructs Jenkins to allocate an executor which will run our deployment commands.

 pipeline {
        agent any


2) The next step is to identify all the tools we will be using in our deployment, for this example, we only need to define maven as we can then use maven to call other tools such as fabric8 without needing to implicitly identify which tools we will be using which makes our Jenkinsfile very versatile.

tools {
        maven 'maven'
            }


3) We can also list a few parameters in the case of manual deployment so that the user can choose the host/namespace they will be deploying on and choose whether they would like to run unit and integration tests and send them over to sonarqube to report. To do so we can either keep them as text inputs with default values such as the HOST option or we can give the user options to choose from such as the NAMESPACE option or just a checkbox like the SKIP_TESTS option.

We can then use these parameters during our builds for dynamic deployment.  

  parameters {
      string(name: 'HOST', description: 'Ingress host', defaultValue: 'api.sumerge.gov.local')
      choice(name: 'NAMESPACE', choices: [sumerge-inspections'], description: 'K8s Namespace')
      choice(name: 'DOCKER_REGISTRY', choices: ['registry.sumerge.local'], description: 'Docker registry')
      booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Do you want to skip tests and sonarqube?')
      choice(name: 'SONAR', choices: ['http://sonarqube.sumerge.local'], description: 'SonarQube host url')
  }


4) The most important part of a Jenkins file is the stages section where the deployment can be split into various checkpoints all responsible for performing a command, this helps us visualize and easily identify where a deployment fails. It can also visualize which steps of the deployment take the longest so that we can identify where we would need to optimize our deployment if needed.

The first we do in our deployment is run mvn clean to delete any old build directories.

stages {
stage('Clean') {
steps {
sh 'mvn clean post-clean -Dbuild.number=${BUILD_NUMBER}'
}
}


5) Then we can run our unit and integration tests using maven, keep in mind that these steps can now be skipped as we are using the parameters we created in step 2 which allowed the user to pick the skip tests option.

stage('Unit tests') {
    steps {
        sh 'mvn test -Dmaven.test.skip=${SKIP_TESTS} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
           }
                    }

stage('Integration tests') {
     steps {
        sh 'mvn integration-test verify -Dskip.surefire.tests -Dmaven.test.skip=${SKIP_TESTS} -Dbuild.number=${BUILD_NUMBER}
        -Dspring.profiles.active=kubernetes'
            }
                           }


6) We can now take the output of these tests that we ran and integrate them with sonarqube to generate our reports. This is all dynamic using our parameters as the user can direct these test results to their sonarqube host that they input in the parameters or they can completely skip the tests thus skipping the sonarqube stage.

    stage ('SonarQube') {
      steps {
        script {
            if (params.SKIP_TESTS) {
                echo "Test skip $SKIP_TESTS, so no SonarQube"
            } else {
                sh 'mvn sonar:sonar -Dsonar.host.url=${SONAR} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
            }
        }
      }
    }


7) After we are done with building our code and running our tests we can create a docker image which we can then deploy onto Kubernetes dynamically using the docker registry and Kubernetes namespaces declared in the parameters.

stage ('Docker') {
      steps {
        sh 'mvn spring-boot:build-image k8s:push -Ddocker.registry=${DOCKER_REGISTRY} -Dk8s.namespace=${NAMESPACE}
        -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
      }
    }


8)  And the final step is to deploy our Docker image on Kubernetes by listing out our commands that will run on our image once it is deployed.

    stage ('K8s deploy') {
      steps {
        sh """
        sed -i "s|@DOCKER_REGISTRY@|${DOCKER_REGISTRY}|g" $WORKSPACE/kubernetes-db.yml
        sed -i "s|@NAMESPACE@|${NAMESPACE}|g" $WORKSPACE/kubernetes-db.yml
        mvn k8s:resource -Ddocker.registry=${DOCKER_REGISTRY} -Dhost=${HOST} -Dk8s.namespace=${NAMESPACE} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes
        kubectl apply -f $WORKSPACE/target/classes/META-INF/jkube/kubernetes.yml -n ${NAMESPACE} --kubeconfig /var/jenkins_home/k8s/${NAMESPACE}
        kubectl apply -f $WORKSPACE/kubernetes-db.yml -n ${NAMESPACE} --kubeconfig /var/jenkins_home/k8s/${NAMESPACE}
        """
      }
    }
  }


Below is a copy of the Jenkinsfile referenced in our steps.

pipeline {
  agent any

  tools {
    maven 'maven'
  }

  parameters {
      string(name: 'HOST', description: 'Ingress host', defaultValue: 'api.sumerge.gov.local')
      choice(name: 'NAMESPACE', choices: [sumerge-inspections'], description: 'K8s Namespace')
      choice(name: 'DOCKER_REGISTRY', choices: ['registry.sumerge.local'], description: 'Docker registry')
      booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Do you want to skip tests and sonarqube?')
      choice(name: 'SONAR', choices: ['http://sonarqube.sumerge.local'], description: 'SonarQube host url')
   }

  options {
    skipStagesAfterUnstable()
    disableConcurrentBuilds()
  }

  stages {
   stage('Clean') {
    steps {
    sh 'mvn clean post-clean -Dbuild.number=${BUILD_NUMBER}'
   }
 }

   stage('Unit tests') {
    steps {
    sh 'mvn test -Dmaven.test.skip=${SKIP_TESTS} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
   }
 }

   stage('Integration tests') {
    steps {
    sh 'mvn integration-test verify -Dskip.surefire.tests -Dmaven.test.skip=${SKIP_TESTS} -Dbuild.number=${BUILD_NUMBER}
    -Dspring.profiles.active=kubernetes'
   }
 }

  stage ('SonarQube') {
   steps {
   script {
       if (params.SKIP_TESTS) {
          echo "Test skip $SKIP_TESTS, so no SonarQube"
       } else {
          sh 'mvn sonar:sonar -Dsonar.host.url=${SONAR} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
       }
    }
  }
 }

  stage ('Docker') {
    steps {
       sh 'mvn spring-boot:build-image k8s:push -Ddocker.registry=${DOCKER_REGISTRY} -Dk8s.namespace=${NAMESPACE}
       -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes'
     }
   }

  stage ('K8s deploy') {
   steps {
     sh """
     sed -i "s|@DOCKER_REGISTRY@|${DOCKER_REGISTRY}|g" $WORKSPACE/kubernetes-db.yml
     sed -i "s|@NAMESPACE@|${NAMESPACE}|g" $WORKSPACE/kubernetes-db.yml
     mvn k8s:resource -Ddocker.registry=${DOCKER_REGISTRY} -Dhost=${HOST} -Dk8s.namespace=${NAMESPACE} -Dbuild.number=${BUILD_NUMBER} -Dspring.profiles.active=kubernetes
     kubectl apply -f $WORKSPACE/target/classes/META-INF/jkube/kubernetes.yml -n ${NAMESPACE} --kubeconfig /var/jenkins_home/k8s/${NAMESPACE}
     kubectl apply -f $WORKSPACE/kubernetes-db.yml -n ${NAMESPACE} --kubeconfig /var/jenkins_home/k8s/${NAMESPACE}
     """
     }
   }
  }
 }


Conclusion

In our Jenkins file, we can instruct our agent to use tools such as maven which can build our module and run its tests for we can then use with other CI/CD tools such as sonarqube, and we can also instruct it to dockerize that build and deploy it on Kubernetes for easy automatic deployment.

What we have just demonstrated is only a glimpse of the possibilities that can be done using a Jenkins file for automatic deployment, as Jenkins is free and can integrate with over 1000 plugins I urge you to experiment with your own Jenkinsfile and fully utilize its array of tools to easily optimize your project pipelines.

Modernizing Legacy Apps​

Maecenas mollis sagittis ante, eleifend ultricies sapien. Nam ultricies risus et augue auctor vulputate gravida eget sem. Quisque mollis gravida magna, eu semper eros pharetra in. Sed et elit sit amet odio rutrum consectetur vel vel ante. Praesent vitae elementum lacus. Vivamus efficitur nunc tortor, cursus lobortis purus placerat ut. Maecenas ut aliquet ante, vel finibus lorem. Nulla facilisi. Donec maximus elementum pulvinar.

test heading

h1 text

h3

Impact

Sample article featured image
Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam. Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.
“Quisque mollis purus nec pulvinar rutrum. Duis faucibus sed orci vel pellentesque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec non volutpat eros, nec placerat mi. Praesent porta felis ut urna sagittis, sit amet placerat nisl porttitor.”

Nunc tempor molestie velit id dictum. Aenean ac venenatis ipsum, sit amet sodales tortor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam.

Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.

data-acc-source-start

Ensure that Modernizing your Legacy Application is the Right Decision

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Depend on a Tailored, Phased Application Modernization Strategy

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Streamline the Transition from Old to New

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

data-acc-source-end

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum, elit a pellentesque placerat, nisl quam blandit orci, at maximus eros nunc nec lacus. Nullam euismod consequat libero, eget suscipit ligula lacinia nec. Nunc finibus dapibus quam, eu convallis magna. Nulla finibus ut risus in sodales. Cras tristique nisi non mattis volutpat. Nullam venenatis varius nisl, dictum ornare lorem dictum rhoncus. Nulla sem nunc, lobortis et massa sed, ultrices convallis justo. Quisque laoreet nibh sit amet arcu rhoncus accumsan. Proin at elementum lacus, at maximus mi. Curabitur vulputate urna mollis lacinia auctor. Donec venenatis finibus magna id tempor. Duis at mattis odio. Aenean eu tempus justo. Donec est arcu, vulputate quis risus et, pharetra imperdiet velit.

Vivamus ut dignissim quam.

No items found.
Article carousel image 1
Article carousel image 2
Article carousel image 3
Author
Yomna Anwar
Posted on
17 May 2021
Topics
No items found.
We’re your partner in addressing

real human needs.

Align IT Initiatives with Strategic Business Goals
Plus sign iconMinus sign icon
10X
Increase in transactions
per second

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Modernizing Legacy Apps​

Maecenas mollis sagittis ante, eleifend ultricies sapien. Nam ultricies risus et augue auctor vulputate gravida eget sem. Quisque mollis gravida magna, eu semper eros pharetra in. Sed et elit sit amet odio rutrum consectetur vel vel ante. Praesent vitae elementum lacus. Vivamus efficitur nunc tortor, cursus lobortis purus placerat ut. Maecenas ut aliquet ante, vel finibus lorem. Nulla facilisi. Donec maximus elementum pulvinar.

Impact

Sample article featured image
Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam. Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.
“Quisque mollis purus nec pulvinar rutrum. Duis faucibus sed orci vel pellentesque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec non volutpat eros, nec placerat mi. Praesent porta felis ut urna sagittis, sit amet placerat nisl porttitor.”

Nunc tempor molestie velit id dictum. Aenean ac venenatis ipsum, sit amet sodales tortor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque posuere sem in ipsum venenatis, at bibendum lorem aliquam.

Nullam condimentum tempus orci nec commodo. Maecenas malesuada elementum metus, non aliquam est elementum sed. Integer ac finibus ligula, id venenatis lectus. Mauris non eleifend enim. Pellentesque eu congue justo. In ornare dapibus nisi, sit amet feugiat neque. Vivamus mollis, lectus quis gravida viverra, risus ligula congue felis, ut laoreet sem nisi in tortor. Sed vel ligula nulla.

data-acc-source-start

Ensure that Modernizing your Legacy Application is the Right Decision

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Depend on a Tailored, Phased Application Modernization Strategy

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

Streamline the Transition from Old to New

Our expert consultants work closely with you to understand you organization's business drivers, then conduct an in-depth business goals and that every dollar invested is directed towards the right solution

data-acc-source-end

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum, elit a pellentesque placerat, nisl quam blandit orci, at maximus eros nunc nec lacus. Nullam euismod consequat libero, eget suscipit ligula lacinia nec. Nunc finibus dapibus quam, eu convallis magna. Nulla finibus ut risus in sodales. Cras tristique nisi non mattis volutpat. Nullam venenatis varius nisl, dictum ornare lorem dictum rhoncus. Nulla sem nunc, lobortis et massa sed, ultrices convallis justo. Quisque laoreet nibh sit amet arcu rhoncus accumsan. Proin at elementum lacus, at maximus mi. Curabitur vulputate urna mollis lacinia auctor. Donec venenatis finibus magna id tempor. Duis at mattis odio. Aenean eu tempus justo. Donec est arcu, vulputate quis risus et, pharetra imperdiet velit.

Vivamus ut dignissim quam.

No items found.
Article carousel image 1
Article carousel image 2
Article carousel image 3
Author
This is some text inside of a div block.
Posted on
This is some text inside of a div block.
Topics