Skip to main content

Command Palette

Search for a command to run...

Jenkins on Server Pull Based Approach GitOps

Jenkins Pipeline for Java based application using Maven, SonarQube, Argo CD and Kubernetes

Published
β€’7 min readβ€’View as Markdown
Jenkins on Server Pull Based Approach GitOps
D

, I'm a seasoned DevOps engineer πŸ› οΈ with a knack for optimizing software development lifecycles and infrastructure operations. πŸ’‘ Specializing in cutting-edge DevOps practices and proficient in tools like Docker, Kubernetes, Ansible, and more, I'm committed to driving digital transformation and empowering teams to deliver high-quality software with speed and confidence. πŸ’»

Tools Used

  • Developers: The process starts with developers writing code.

  • SCM (Source Control Management): The code is stored in a version control system, likely GitHub, as indicated by the logo. This is where the code is managed and tracked.

  • Jenkins: Jenkins is used for automation, such as building the code. It triggers the next steps in the CI/CD pipeline based on commits to the SCM.

  • Maven: Maven is used for building the project. It compiles the code, manages dependencies, and packages the code for deployment.

  • SonarQube: After building the project, SonarQube performs static code analysis to ensure code quality and security standards.

  • Test: Automated testing is performed to validate the functionality of the code.

  • Docker: Docker is used to containerize the application. The built Docker images are stored in a Docker registry.

  • Manifests Repo: This refers to a repository where Kubernetes manifest files (like deployment YAMLs) are stored.

  • Argo (likely Argo CD): Argo is used for continuous delivery. It pulls code and manifests from the Git repository and applies the manifests to the Kubernetes cluster.

  • Kubernetes: Finally, the application is deployed to a Kubernetes cluster.

Prerequisite

In this POC we need kubaedm cluster 1 master and 2 worker machine and Jenkins machine separately

Controlplane And Workernode

once done take joined command from Controlplane and paste on worker node

Jenkins Install using shell script

Install Jenkins but don't install maven because we have plan to use maven as docker container

For install Jenkins we need to follow below shell script i am using ubuntu 20.04 this script also work for ubuntu 22.04

vi jenkins.sh
#! /bin/bash

sudo apt update
sudo apt install openjdk-11-jre -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt-get update
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

After that we need to check using Jenkins using ec2 instance id for example:- 34.203.34.186:8080 for checking purpose but our end goal its should not be run on this after configuring nginx reverse proxy will be remove port 8080 from ec2 instance security group and check , Here you can see our Jenkins up and running

Access your Jenkins Dashboard on browser now

Note: Make sure you enable 8080 port in Security Group Inbound Rules.

Get the initial administration password

$sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Now your Jenkins started : Create Admin user

πŸŽ‰Now , appreciate yourself you successfully deploy Jenkins on your machine πŸŽ‰

Install SonarQube on VM

sudo apt install unzip
sudo adduser sonarqube
sudo su - sonarqube
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
unzip *
chmod -R 755 /home/sonarqube/sonarqube-9.4.0.54424
chown -R sonarqube:sonarqube /home/sonarqube/sonarqube-9.4.0.54424
cd sonarqube-9.4.0.54424/bin/linux-x86-64/
./sonar.sh start

Hurray !! Now you can access the SonarQube Server on http://<ip-address>:9000

it is look like this : The default username and password for SonarQube is Admin later on you can also change

πŸŽ‰Now , appreciate yourself you successfully deploy SonarQube on your machine πŸŽ‰

Install Argocd in the kubernetes cluster

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

once this is done kindly change argocd-server service from ClusterIP to NodePort to expose the argocd UI

to change service type either use edit command use this command

kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

also for fetching password for argocd use this command

 kubectl -n argocd get secret argocd-initial-admin-secret \
          -o jsonpath="{.data.password}" | base64 -d; echo

Run below command and check argocd run on which port number and access your argocd on browser with same port number

kubectl get all -n argocd

https://<master_node_public_IP>:31185

Docker Install on Jenkins VM

sudo apt install docker.io -y

-need to provide owenership from root to jenkins user as the jenkins user has permission

sudo chmod 777 /var/run/docker.sock
sudo chown jenkins:docker /var/run/docker.sock

Plugins Installation on Jenkins Dashboard

Manage Jenkins --> plugins ---> available plugins ---->

for Docker

  1. docker

  2. docker Pipeline

  3. docker commons

  4. docker -build-steps

  5. docker slave

install it

for kubernetes

  1. Kubernetes Client API

  2. Kubernetes Credentials

  3. Kubernetes

  4. Kubernetes CLI

  5. Kubernetes Credentials Provider

  • install it

For SonarQube

  1. SonarQube Scanner

Hurray πŸŽ‰ we will done with all plugins πŸŽ‰

Now Time to add Credentials in Jenkins

Credentials

for Docker

Generate a token on Dockerhub

Now on your Jenkins machine click on manage Jenkins --> click on Credentials --->

system ---> global Credentials ---> Select Username and Password

ADD your docker token as a password

ADD (ID) which is you given in your pipeline

for SonarQube

Generate a token on SonarQube Dashboard ---> My Account---> Security ---> Generate Tokens

Now on your Jenkins machine click on manage Jenkins --> click on Credentials --->

system ---> global Credentials--->Select Secrete Text

ADD your SonarQube token as a Secrete

ADD (ID) which is you given in your pipeline

For GitHub

Generate a token on Github Dashboard ---> Settings ---> developer settings ---> Generate Tokens

Now on your Jenkins machine click on manage Jenkins --> click on Credentials --->

system ---> global Credentials--->Select Secrete Text

ADD your Github token as a Secrete

ADD (ID) which is you given in your pipeline

Hurray πŸŽ‰ we will done with all CredentialsπŸŽ‰

Job creation on Jenkins

  1. New Item:

    • Click on New Item in the Jenkins dashboard.
  2. Name Your Job:

    • Enter a name for your job and select Pipeline.
  3. click on pipeline from SCM

  4. Select Git and Configure Git Repo-URL https://github.com/divyasatpute/Jen-Argocd.git

  5. Select Script Path = where your Jenkins file is present on gitrepo

    in my case here is present my jenkinsfile

    java-maven-sonar-argocd-helm-k8s/spring-boot-app/JenkinsFile

  6. save and apply

  7. Now Click on Build Now

For your preference here i give pipeline Replace your Id

pipeline {
    agent {
        docker {
            image 'iamsakib/maven:latest'
            args '--user root -v /var/run/docker.sock:/var/run/docker.sock'
        }
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    // Checkout the branch
                    sh '''
                        git checkout main || git checkout -b main
                        git pull origin main
                    '''
                }
            }
        }

        stage('Build and Test') {
            steps {
                sh 'ls -ltr'
                sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && mvn clean package'
            }
        }

        stage('Static Code Analysis') {
            environment {
                SONAR_URL = "http://13.233.127.161:9000"
            }
            steps {
                withCredentials([string(credentialsId: 'sonarqube', variable: 'SONAR_AUTH_TOKEN')]) {
                    sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && mvn sonar:sonar -Dsonar.login=$SONAR_AUTH_TOKEN -Dsonar.host.url=${SONAR_URL}'
                }
            }
        }

        stage('Build and Push Docker Image') {
            environment {
                DOCKER_IMAGE = "divyasatpute/argocd-app:${BUILD_NUMBER}"
                REGISTRY_CREDENTIALS = credentials('docker-cred')
            }
            steps {
                script {
                    sh 'cd java-maven-sonar-argocd-helm-k8s/spring-boot-app && docker build -t ${DOCKER_IMAGE} .'
                    def dockerImage = docker.image("${DOCKER_IMAGE}")
                    docker.withRegistry('https://index.docker.io/v1/', "docker-cred") {
                        dockerImage.push()
                    }
                }
            }
        }

        stage('Update Deployment File') {
            environment {
                GIT_REPO_NAME = "Jen-Argocd"
                GIT_USER_NAME = "divyasatpute"
            }
            steps {
                withCredentials([string(credentialsId: 'github', variable: 'GITHUB_TOKEN')]) {
                    script {
                        // Configure Git and apply changes
                        sh '''
                            git config user.email "divyasatpute822@gmail.com"
                            git config user.name "divyasatpute"
                            sed -i "s/replaceImageTag/${BUILD_NUMBER}/g" java-maven-sonar-argocd-helm-k8s/spring-boot-app-manifests/deployment.yml
                        '''

                        // Switch to the correct branch
                        sh '''
                            git checkout main || git checkout -b main
                            git add java-maven-sonar-argocd-helm-k8s/spring-boot-app-manifests/deployment.yml
                        '''

                        // Commit and push changes
                        def hasChanges = sh(script: 'git diff --cached --quiet || echo "changes"', returnStdout: true).trim()

                        if (hasChanges == "changes") {
                            sh '''
                                git commit -m "Update deployment image to version ${BUILD_NUMBER}"
                                git push https://${GITHUB_TOKEN}@github.com/${GIT_USER_NAME}/${GIT_REPO_NAME} HEAD:main
                            '''
                        } else {
                            echo 'No changes to commit. Skipping the commit step.'
                        }
                    }
                }
            }
        }
    }
}

Now you are in the last stage keep going

Creating app on ArgoCD

  • Go to Argocd dashboard

  • click on New App

  • configure detail

  • and click on create app

its look like this

Test Result

πŸŽ‰ Congratulations on your achievement! πŸ₯³ Your hard work and dedication have truly paid off, and you deserve every bit of this success. Keep reaching for the stars 🌟, and may this milestone be just the beginning of even greater things ahead. Well done! πŸ‘πŸŽŠ

B
baki2y ago

thanks

More from this blog

D

Divya_satpute's_blog

23 posts

I'm a seasoned DevOps engineer πŸ› οΈ with a knack for optimizing software development lifecycles and infrastructure operations. πŸ’‘ Specializing in cutting-edge DevOps practices and proficient in tools