Basic DevOps CI/CD Pipeline #2

Before reading this blog, check out the first part of the series;

We are going to pull the private GitHub repository to Jenkins by adding credentials.

You need to login to Jenkins GUI for it. Then continue with Manage Jenkins > Credentials > Domain > (Global) > Add Credentials

Select the “Username with password” kind and fill the username and password sections. Write something that defines the credentials to the “ID” section. In our example, it will be “github-creds”. After clicking the “OK” button you need to see the credentials as;

Now we can move forward to creating pipeline;

First step will be checking out the source code;

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git(branch: 'master', credentialsId: 'github-creds', url: 'https://github.com/github_user/private-repository.git')
            }
        }
    }
}

I used a basic Maven Spring boot web application project. If you want you can also write yourself or install from https://start.spring.io/
You can select 2.4.1 version and Java 8 version.

Also, you need to add a Dockerfile. You can use below Dockerfile for starting point;

FROM openjdk:8-jdk-alpine
COPY . /data
WORKDIR /data
RUN ["mvn", "clean", "install"]
COPY target/*.jar ./app.jar
ENTRYPOINT ["java", "-Xmx750m", "-jar", "app.jar"]

Then, you’ll need a DockerHub account to push the artifact. https://hub.docker.com/

After creating the account, you need to create the Jenkins credentials for it.

Docker Operations on Jenkinsfile

You can directly interact with Docker Daemon with docker commands using sh in Jenkinsfile, but we will use a plugin for it;

I assume you downloaded Pipeline plugin on Jenkins installation > install suggested plugins.
Docker plugin
Docker Pipeline plugin

For installing these plugins you need to navigate Manage Jenkins > Manage Plugins > Available tab.

After installing the required plugins, we can move forward with Docker build & push stages;

stage('Build') {
    steps {
        script {
            newImage = docker.build 'yourdockerhubuser/javaapp'
        }
    }
}

This is basically executes

docker build -t yourdockerhubuser/javaapp .

Then continue with pushing this image to DockerHub;

On this stage we will need to login to DockerHub > tagging the image that we built last stage > pushing the image to DockerHub;

stage('Push') {
    steps {
        script{
            docker.withRegistry('', 'dockerhub-creds') {
                newImage.push('$BUILD_NUMBER')
            }
        }
    }
}
The reason we left the first part blank on docker.withRegistry is that the default registry of the Docker daemon is DockerHub. And, at the second part we need to fill with credentials id for DockerHub account added on Jenkins. Lastly, we’re tagging this image with Jenkins job build number. This stage basically executes;
docker login -u yourdockerhubuser -p ******** https://index.docker.io/v1/
docker tag yourdockerhubuser/javaapp yourdockerhubuser/javaapp:1
docker push yourdockerhubuser/javaapp:1

In the next episode, we will continue by preparing a deployment ansible-playbook.

Triggering the ansible-playbook within Jenkins.

Setting up the monitoring baseline.

Leave a Comment

Your email address will not be published. Required fields are marked *