<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adedimeji Sebiotimo</title>
    <description>The latest articles on DEV Community by Adedimeji Sebiotimo (@dims26).</description>
    <link>https://dev.to/dims26</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F662319%2Fed7f912d-5e84-41a0-9da4-401aeae48a7d.jpeg</url>
      <title>DEV Community: Adedimeji Sebiotimo</title>
      <link>https://dev.to/dims26</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dims26"/>
    <language>en</language>
    <item>
      <title>CI/CD with Spring Boot and Jenkins Pipelines</title>
      <dc:creator>Adedimeji Sebiotimo</dc:creator>
      <pubDate>Tue, 28 Mar 2023 20:18:35 +0000</pubDate>
      <link>https://dev.to/dims26/cicd-with-spring-boot-and-jenkins-pipelines-5fdp</link>
      <guid>https://dev.to/dims26/cicd-with-spring-boot-and-jenkins-pipelines-5fdp</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We shall explore CI/CD workflows in this tutorial, making use of &lt;a href="https://www.jenkins.io/doc/book/pipeline/" rel="noopener noreferrer"&gt;Jenkins Pipeline&lt;/a&gt; to take a Spring Boot application from version control through integration, delivery and deployment processes.&lt;br&gt;
The capabilities of Jenkins Pipeline will be used to orchestrate testing, code coverage analysis, containerisation, and deployment to a PaaS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your Spring Boot Application
&lt;/h2&gt;

&lt;p&gt;A simple Spring Boot application using the Gradle build tool can be created using &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;Spring Initializr&lt;/a&gt;. You will need the Web dependency to configure your app as a web application, and the Actuator dependency to provide a URL for checking the status of your application. Afterwards, generate, download and extract your application. Using an appropriate IDE, open your project files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Version Control
&lt;/h2&gt;

&lt;p&gt;You will need to init a git repo in the extracted folder and &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-push-an-existing-project-to-github" rel="noopener noreferrer"&gt;link&lt;/a&gt; the repo to GitHub. &lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Jenkins Pipeline
&lt;/h2&gt;

&lt;p&gt;We will use a local Jenkins server to run our CI/CD processes. &lt;a href="https://www.jenkins.io/doc/book/installing/" rel="noopener noreferrer"&gt;Install Jenkins&lt;/a&gt; if you do not have an existing installation. Next, install the Pipeline plugin as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On your Jenkins Dashboard Select &lt;strong&gt;Manage Jenkins&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Manage Plugins&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Available plugins&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the text box type in "Pipeline" and install the plugin.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, create a new item on your Jenkins dashboard and select &lt;strong&gt;Pipeline&lt;/strong&gt; as the type. In the configuration menu, set the pipeline definition as "Pipeline from SCM" to source the pipeline script from GitHub. Set SCM as "Git" and provide the repository URL formatted as follows (replace angled brackets with relevant values):&lt;br&gt;
&lt;code&gt;https://&amp;lt;github access token&amp;gt;@github.com/&amp;lt;userName&amp;gt;/&amp;lt;name of repository containing pipeline script&amp;gt;.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We will use the same GitHub repo used for the Spring application to house the pipeline script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set pipeline definition as "Pipeline" if you want to provide the pipeline script directly in Jenkins GUI instead of sourcing it from version control.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Defining a Pipeline Script
&lt;/h2&gt;

&lt;p&gt;Define a Jenkins Pipeline by including a text file - named &lt;strong&gt;Jenkinsfile&lt;/strong&gt; - containing the pipeline script, at the root of your application. The pipeline script specifies the CI/CD processes through which the application passes. You can treat your Jenkinsfile similarly to code and check it into version control.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will define a &lt;strong&gt;Scripted Pipeline&lt;/strong&gt; using Groovy-related syntax. Include the following to define a Pipeline that clones your source code from GitHub using the Git plugin and builds your application using the Gradle build tool:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

node {
  stage("Clone project") {
    git branch: 'main', url: 'https://github.com/&amp;lt;GitHub username&amp;gt;/&amp;lt;repo name&amp;gt;.git'
  }

  stage("Build project with test execution") {
    sh "./gradlew build"
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Commit and push your changes, then head to your Jenkins dashboard, select your pipeline and "&lt;strong&gt;Build Now&lt;/strong&gt;". Jenkins will retrieve your pipeline script and orchestrate the stages defined in your pipeline script. Your build should complete successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Code Coverage Analysis
&lt;/h2&gt;

&lt;p&gt;Code coverage analysis tools quantify the amount of tested code, serving as a valuable tool to inform on code structure and testing related decisions. We will make use of &lt;a href="https://github.com/jacoco/jacoco" rel="noopener noreferrer"&gt;JaCoCo&lt;/a&gt;, JaCoCo produces reports on multiple kinds of code coverage metrics including instructions, line and branch coverage.&lt;/p&gt;

&lt;p&gt;Implement the following steps to configure JaCoCo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the JaCoCo Jenkins plugin.&lt;/li&gt;
&lt;li&gt;Add the JaCoCo plugin within the plugins block in your build.gradle file.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

plugins {
  ...
  id 'jacoco'
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Configure JaCoCo to run after the Gradle test task.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

test {
  finalizedBy jacocoTestReport
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Update Jenkinsfile, instructing the JaCoCo agent on what files to analyse, the location of the coverage report and to exclude test files.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

node {
  stage("Clone project") {
    git branch: 'main', url: 'https://github.com/&amp;lt;GitHub username&amp;gt;/&amp;lt;repo name&amp;gt;.git'
  }

  stage("Build project with test execution") {
    sh "./gradlew build"
  }

  jacoco(
    execPattern: '**/*.exec',
    sourcePattern: 'src/main/java',
    exclusionPattern: 'src/test*'
  )
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Commit your changes and build the Jenkins pipeline again. You should have a new &lt;strong&gt;Coverage Trend&lt;/strong&gt; button on your Pipeline page linking to additional information about code coverage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e69ctuacs62r0x1dxnb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e69ctuacs62r0x1dxnb.png" alt="Code Coverage chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying a Container Image to DockerHub
&lt;/h2&gt;

&lt;p&gt;In this section, we will setup the automated generation and deployment of a Docker container image. You will need a Docker Hub &lt;a href="https://hub.docker.com/signup" rel="noopener noreferrer"&gt;account&lt;/a&gt; and the &lt;a href="https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin" rel="noopener noreferrer"&gt;Jib Gradle Plugin&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Access Docker command from Pipeline Script
&lt;/h3&gt;

&lt;p&gt;Using your Docker Hub account, create a repository to hold the container image. Afterwards, allow access to the docker installation on your computer. Implement the following steps to achieve this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In Jenkins GUI, navigate to &lt;strong&gt;Manage Jenkins &amp;gt; Configure System &amp;gt; Global properties&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set an environment variable for &lt;strong&gt;PATH+EXTRA&lt;/strong&gt; with it's value as the directory containing your docker executable.&lt;br&gt;
You can find your docker installation by running &lt;code&gt;where docker&lt;/code&gt; in a terminal window. For example, given a location &lt;code&gt;/usr/local/bin/docker&lt;/code&gt;, the relevant value is &lt;code&gt;/usr/local/bin/&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Set Docker Hub Credentials in Jenkins
&lt;/h3&gt;

&lt;p&gt;You also need to &lt;a href="https://www.jenkins.io/doc/book/using/using-credentials/#:~:text=Adding%20new%20global%20credentials" rel="noopener noreferrer"&gt;set&lt;/a&gt; your Docker Hub login credentials within Jenkins. Set a &lt;strong&gt;Username with password&lt;/strong&gt; credential with ID "DOCKER_CRED" and the appropriate values for username and password.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Jib
&lt;/h3&gt;

&lt;p&gt;The Jib Gradle Plugin enables the generation of a container image without needing a dockerfile. Implement the following steps to configure Jib:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the plugin to the plugins block in your &lt;strong&gt;build.gradle&lt;/strong&gt; file and configure the values for the image's name and tags.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

plugins {
  ...
  id 'com.google.cloud.tools.jib' version '3.3.1'
}

jib.to.image = '&amp;lt;docker hub username&amp;gt;/&amp;lt;repository name&amp;gt;'
jib.to.tags = [version, 'latest']


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Update Jenkinsfile adding steps to trigger the jib task.
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;node {&lt;br&gt;
  stage("Clone project") {&lt;br&gt;
    git branch: 'main', url: '&lt;a href="https://github.com/" rel="noopener noreferrer"&gt;https://github.com/&lt;/a&gt;/.git'&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;stage("Build project with test execution") {&lt;br&gt;
    sh "./gradlew build"&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;stage("Deploy to DockerHub with Jib") {&lt;br&gt;
    withCredentials([string(credentialsId: 'DOCKER_PASSWORD', variable: 'DOCKER_PASSWORD'), string(credentialsId: 'DOCKER_USERNAME', variable: 'DOCKER_USERNAME')]) {&lt;br&gt;
        sh '''&lt;br&gt;
        echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin&lt;br&gt;
        ./gradlew jib -Djib.to.auth.username="${DOCKER_USERNAME}" -Djib.to.auth.password="${DOCKER_PASSWORD}"&lt;br&gt;
        '''&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;jacoco(&lt;br&gt;
    execPattern: '*&lt;em&gt;/&lt;/em&gt;.exec',&lt;br&gt;
    sourcePattern: 'src/main/java',&lt;br&gt;
    exclusionPattern: 'src/test*'&lt;br&gt;
  )&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Commit your changes and build the Jenkins pipeline. After a successful build, the container image should be available from your Docker Hub repository.


![Container Image Tags in Docker Hub Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r1di91ftpbnq0ecwa6z.png)

## Deploying application to Heroku

We will tackle deployment as a final step, using Heroku platform as a service to host the spring boot application. You will need a [Heroku account](https://signup.heroku.com/) and the [Heroku Gradle plugin](https://github.com/heroku/heroku-gradle-plugin). 

### Prepare App for Heroku

Create a new app from your Heroku [dashboard](https://dashboard.heroku.com/apps) and give it a name, then add a **Procfile** to the root of your Spring Boot application containing the following command to start up your application `web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/&amp;lt;Name of your jar file&amp;gt;.jar`. You can find your jar file in the **build/libs** directory after running the Gradle build task.

**NB:** 
- Do not use the "-plain" jar file as it does not contain the dependencies required by your app to run.

- Also create a **system.properties** file at the root of your Spring Boot application and specify the  java version used by your application within it. For example, `java.runtime.version=17` informs Heroku to launch the app using the Java 17 runtime.

### Set Heroku API Key as Jenkins Credential

Retrieve your Heroku API key from your [account settings](https://dashboard.heroku.com/account). Next, set a **Secret text** credential with ID "HEROKU_API_KEY" and paste your Heroku API key in the secret field.

### Configure Heroku Gradle Plugin

- Add the plugin to the plugins block in your **build.gradle** file and configure the values for the Heroku app name.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;plugins {&lt;br&gt;
  ...&lt;br&gt;
  id 'com.heroku.sdk.heroku-gradle' version '3.0.0'&lt;br&gt;
}&lt;br&gt;
heroku.appName = ''&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- Update Jenkinsfile adding steps to run the Heroku deploy step in parallel with the Jib deploy step.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;node {&lt;br&gt;
  stage("Clone project") {&lt;br&gt;
    git branch: 'main', url: '&lt;a href="https://github.com/" rel="noopener noreferrer"&gt;https://github.com/&lt;/a&gt;/.git'&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;stage("Build project with test execution") {&lt;br&gt;
    sh "./gradlew build"&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;stage("Deploy to DockerHub with Jib and to Heroku") {&lt;br&gt;
    parallel([&lt;br&gt;
        docker: {&lt;br&gt;
            withCredentials([usernamePassword(credentialsId: 'DOCKER_CRED', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {&lt;br&gt;
                sh '''&lt;br&gt;
                echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin&lt;br&gt;
                ./gradlew jib -Djib.to.auth.username="${DOCKER_USERNAME}" -Djib.to.auth.password="${DOCKER_PASSWORD}"&lt;br&gt;
                '''&lt;br&gt;
            }&lt;br&gt;
        },&lt;br&gt;
        heroku: {&lt;br&gt;
            withCredentials([string(credentialsId: 'HEROKU_KEY', variable: 'HEROKU_API_KEY')]) {&lt;br&gt;
                sh './gradlew deployHeroku'&lt;br&gt;
            }&lt;br&gt;
        }&lt;br&gt;
    ])&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;jacoco(&lt;br&gt;
    execPattern: '*&lt;em&gt;/&lt;/em&gt;.exec',&lt;br&gt;
    sourcePattern: 'src/main/java',&lt;br&gt;
    exclusionPattern: 'src/test*'&lt;br&gt;
  )&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Commit your changes and build the Jenkins pipeline. After a successful build, your Heroku app should be acessible via `https://&amp;lt;Heroku app name&amp;gt;.herokuapp.com/`


![Heroku application logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15bwm10n9177erdyqgmv.png)

## Conclusion

We covered a number of essential steps in this tutorial. First, we pushed our application to a GitHub repository, next we used Jenkins Pipeline to retrieve and build our application. After that, we introduced code coverage analysis and then pushed a container image to Docker Hub. Lastly, we deployed our application to Heroku PaaS, ready for consumption.

We have been able to harness the capabilities of Jenkins Pipelines to automate core aspects of CI/CD for a Spring Boot application. We have covered integration, delivery and deployment of the application and simplified the aforementioned steps to execute with the push of a button. We can take it further by taking advantage of Jenkins Pipeline build triggers to run the pipeline script periodically or based on some event, for example, pushing code to GitHub.

With the work done, new features can be delivered directly to various environments and to customers while maintaining quality by implementing test-driven development and hinging build success on the successful completion of tests.

You can find the complete code on [GitHub](https://github.com/dims26/TDDSampleJenkins).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>jenkins</category>
      <category>cicd</category>
      <category>tutorial</category>
      <category>gradle</category>
    </item>
  </channel>
</rss>
