Jenkins CICD
Tags: jenkins, docker, maven, sonarqube
Published: 2024-03-18 00:33:05

- Provision an EC2 instance on AWS, opting for the t2.large configuration that supports 2 CPUs and 8GB of RAM. This ensures ample computational power and memory resources to support our Jenkins project's requirements effectively.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knf2dcah1hi3ml7zszi4.png)

-  Clone the repository from its Git source located at https://github.com/bharatrajtj/jenkins.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5roykeelvu245nd3jtk.png)

- Install Java, a prerequisite for Jenkins' operation. This ensures the availability of the Java Runtime Environment (JRE), essential for executing Jenkins and its associated processes smoothly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqrznh504lemdrb4se3o.png)

- Execute the Jenkins installation script to commence the setup process. This script initiates the installation procedure, configuring Jenkins' core components and preparing the environment for subsequent configuration steps.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihc31lxgadgcgvrcie3t.png)

- By default, Jenkins operates its HTTP service on port 8080. This configuration allows for access to the Jenkins web interface.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw2tcx7d9pa65dj4f5y1.png)

- Establish inbound traffic rules within the AWS security group associated with your EC2 instance to facilitate communication on port 8080. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l62rzvq8yh4161igdnc0.png)

- Access the initial administrative password for Jenkins by executing the command cat /var/lib/jenkins/secrets/initialAdminPassword within your EC2 terminal. This step retrieves the required credential, enabling you to proceed with the initial setup and configuration of your Jenkins instance.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21jy0yy1qn1zpuujxgu2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxwm20vhv5bz25ef41p6.png)


- Upon completing the installation of the required plugins and finalizing the setup process, you will be directed to the Jenkins Dashboard. This central hub serves as the control panel for managing your Jenkins environment, offering access to various features and functionalities essential for orchestrating automation tasks and pipelines effectively.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzrtswe0ck3gt2gqdhyd.png)

- Navigate to the "New Item" option within the Jenkins interface to initiate the creation of a new Jenkins Pipeline. Opt for the Pipeline project type to configure and manage your pipeline's workflow, facilitating streamlined automation and continuous integration within your development environment.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76kql0bkmm5s2kstd5k7.png)

- Choose "Pipeline script from SCM" as the configuration option for your Jenkins Pipeline. Given that Git serves as our Source Code Manager (SCM) for this project, specify the repository URL as https://github.com/bharatrajtj/jenkins. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcuzpcsymws99ogoetjm.png)

- Specify the "Script Path" as the directory path within the repository where the Jenkinsfile is located.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyk9f318f7crfn00eh05.png)

- Navigate to "Manage Jenkins" > "Manage Plugins" > "Available" tab, and search for the "Docker Pipeline" plugin. Proceed to install this plugin to enable the utilization of Docker containers as agents within your Jenkins environment. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/912ntj181ytmyy7iau4k.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gogpmr13keqndbl1d5vw.png)

- Navigate to "Manage Jenkins" > "Manage Plugins" > "Available" tab, and search for the "SonarQube Scanner" plugin. Install this plugin to enable seamless integration of SonarQube for code quality inspection within your Jenkins environment.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bc5o2kru7xinu3c23on5.png)

- Add new user named SonarQube in the EC2 instance and switch to the new user


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fi4rh56iv9crj1wauq0e.png)

- Download the sonar zip file 
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouz7jcqqwqwhzy3eanbz.png)

- To unzip the downloaded SonarQube zip file, you'll need to have the unzip utility installed on your system. You can install it using the apt package manager. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccqjuhhwo7p4n3vl1p88.png)

- Switch to sonarqube user and run unzip * 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nenl5d5k9iqrzui7qsze.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkziv6fwrap5sfuq1rpt.png)


- Grant permission to the folders 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbjpo1j87x5qiwvyna0h.png)

- Go to the path depending upon your instance architecture 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kazznfshi1kpe621gtm5.png)

- Run  ./sonar.sh start   to execute your SonarQube server 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ip6beqr5o2m0ss2oag7k.png)

- Configure inbound rules within your EC2 instance's security group to allow traffic from port 9000, the default port for SonarQube. This step ensures that incoming traffic on port 9000 is permitted, enabling access to the SonarQube application.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvtr6091eb41dwo8qc5r.png)

- Access the SonarQube server page by entering your EC2 instance's IP address followed by ":9000" in your web browser's address bar.
Upon reaching the SonarQube server page, use the default credentials to log in:

Username: admin
Password: admin


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue7s0edw5okbq9c2yhum.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmmgl95q9hh9mjkm7vb9.png)

- In the SonarQube web interface, navigate to "My Account" and then to "Security" settings. Locate the option to generate an access token, found within the user profile or security settings.

This token will be used to configure the integration between SonarQube and Jenkins. Ensure you securely store the token as it grants access to SonarQube resources.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6j44row55qgpba631hi.png)

- In Jenkins Page go to ManageJenkins>Credentials>System>GlobalCredentials>AddCredentials  to enter sonarqube token


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8ltfx6jggbvmsk2t3yx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdpy9dszzbfdrxd41a2c.png)

- Install Docker in root user in your EC2 terminal

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ewl5c6gk5blv4zrc3aa.png)

- Grant Jenkins and Ubuntu user permission to access docker daemon and restart docker

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nbzozsetccnzxbohs4z.png)

- Restart Jenkins to make sure the plugins installed will function properly. To restart jenkins after port number enter /restart


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uswrj1oqxm3510e07gqf.png)

- Run Minikube on your local machine through docker as the driver


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncax6ttadp2i78tdde42.png)

- Install the ArgoCD operator to streamline the lifecycle management of the Kubernetes controller, ArgoCD, within your Minikube environment. Utilize the resources available at https://operatorhub.io/operator/argocd-operator to access and deploy the operator. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d3y8ogzge8mq9qsdjjf.png)

- Configure DockerHub and GitHub credentials within Jenkins to facilitate seamless integration with these platforms.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwzcwq2r4ks0oe7tuql8.png)

- For GitHub, navigate to "Settings," then "Personal access tokens," and proceed to "Generate New token (classic)" to create a new token. This token will serve as the authentication mechanism for Jenkins to interact with your GitHub repositories securely.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9j25gdpjbais5b8bvbh.png)

- Within Jenkins, employ the "Secret text" type to configure GitHub credentials securely. After creating the credentials, ensure to restart Jenkins to enact the changes and enable seamless authentication for GitHub interactions within your Jenkins environment.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxswcq8thkq3q7g141cs.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybbhkqy1udh1l1xnfls7.png)


- Build the Jenkins Pipeline


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71dt0arscmg1xdfqsln5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67vcddk7audae7e631mk.png)

- SonarQube report for code analysis 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dikaqdlqdth8vls7duov.png)

- Docker registry where the image generated has been pushed

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6qui6y0p00yjlmercue.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkkflt8il3a9clt97qyk.png)


- Update the docker image name in the deployment yml


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs5v3wk2ma60ium8aywx.png)

- Create a yml file in your local machine and apply this content to install ArgoCD controller

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpfav4g861ceaisggti6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ub2xth3n1rl1m2m47eb.png)

- Make sure the argocd operator pods are created in MiniKube


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilnb9y2bsoylziogi7ww.png)

- Edit the service type from Cluster IP to NodePort for example-argocd-server 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49cushosh85wj3dxnvjt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj3m3i6zwqleym5mdm8v.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omeq70inmzyfofgkhrzo.png)


- Get URL to access the service through web


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw4ug1sd71fgrk80b19l.png)


- Get the password to argocd account by getting into argocd-cluster secret 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnip8j75skub9krm3ahv.png)


- Decode your base 64 encrypted secret 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60vr4uawryd4pknj2ay1.png)


- Enter the credentials in ArgoCD web interface 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vbqby3fzlxa0wa60c8m.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn198psnlxl49lwu1xeg.png)



- Create application in argocd and configure required information 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p71qcehgxqkpag58lw8u.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjibpiqiple35fgy2r46.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02o9c3j4ajtec6nsrrkf.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyvcyk34k4f2qadjkf7a.png)


- The spring boot application has been deployed in our minikube cluster through argocd


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39uu3zd9utnoyvmjup8m.png)


- Edit the deployment image to nginx in the minikube


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fd62w7s087z36sf9aby1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr1ogeis5v532t0wdhb3.png)


- ArgoCD recognize that the image in manifest yml is different from the image executing on minikube cluster it starts to rollback to image mentioned in the manifest

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xa6d96e2t80881wvyt1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/za94e2bhzyff5w4tplsr.png)