Prashant singh
3 min readMay 24, 2023

Install and configure SONARQUBE on the Openshift container platform and CI/CD automation with Jenkins

🙂 Hello Connections 🙂

This article is related to SonarQube configuration and is also used in automation with Jenkins to scan the Source code.

CI/CD Process using Jenkins

First, we create an account on an Openshift container platform where we install and configure SONARQUBE and also set up and install Jenkins.

By using the following approach, we will be able to easily integrate Sonarqube with Jenkins.

Now, we create a YAML file for Sonarqube and Jenkins.

SONARQUBE CONFIGURATION

For Sonarqube, we create multiple services like deployment, route, and service.

Deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube
template:
metadata:
labels:
app: sonarqube
spec:
containers:
- name: sonarqube
image: sonarqube
ports:
- containerPort: 9000
env:
- name: SONARQUBE_JDBC_URL
value: "jdbc:postgresql://<db-host>:5432/sonarqube"
- name: SONARQUBE_JDBC_USERNAME
value: "<db-username>"
- name: SONARQUBE_JDBC_PASSWORD
value: "<db-password>"

Service.yml

apiVersion: v1
kind: Service
metadata:
name: sonarqube
spec:
ports:
- port: 9000
targetPort: 9000
selector:
app: sonarqube
type: ClusterIP

Route.yml

apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: sonarqube-route
spec:
host: sonarqube.example.com
to:
kind: Service
name: sonarqube
weight: 100
port:
targetPort: 9000

Access the SonarQube in a web browser and default credentials. For example:

http://example.route.openshift.com

username: admin password: admin

Now create a unique token and copy that token into Notepad for future use.

click on my account and enter the name and click on generate button and the token start with “sqa_xxxxxxxxxxxxxxxxxxxx”.

JENKINS CONFIGURATION

Similarly, for Jenkins, we create a Yaml file for Jenkins.

Deployment.yaml

apiVersion: v1
kind: Template
metadata:
name: jenkins-template
objects:
- kind: "BuildConfig"
apiVersion: "v1"
metadata:
name: "jenkins-master"
spec:
source:
contextDir: ""
type: "None"
strategy:
type: "JenkinsPipeline"
jenkinsPipelineStrategy:
jenkinsfilePath: Jenkinsfile
env:
- name: "MAVEN_VERSION"
value: "3.6.3"
- kind: "DeploymentConfig"
apiVersion: "v1"
metadata:
name: "jenkins-master"
spec:
replicas: 1
selector:
name: "jenkins-master"
template:
metadata:
labels:
name: "jenkins-master"
spec:
containers:
- name: "jenkins-master"
image: "jenkins/jenkins:lts"
ports:
- containerPort: 8080
protocol: "TCP"
- containerPort: 50000
protocol: "TCP"
env:
- name: "JAVA_OPTS"
value: "-Djenkins.install.runSetupWizard=false"
- name: "MAVEN_HOME"
value: "/opt/maven"
- name: "MAVEN_VERSION"
value: "3.6.3"
volumeMounts:
- mountPath: "/var/jenkins_home"
name: "jenkins-home"
volumes:
- name: "jenkins-home"
emptyDir: {}

Service.yaml

apiVersion: v1
kind: Service
metadata:
name: jenkins-service
spec:
selector:
name: jenkins-master
ports:
- protocol: TCP
port: 8080
targetPort: 8080
- protocol: TCP
port: 50000
targetPort: 50000

Route.yaml

apiVersion: v1
kind: Route
metadata:
name: jenkins-route
spec:
to:
kind: Service
name: jenkins-service
port:
targetPort: 8080
tls:
termination: edge

Note:- please modify this Yaml according to your needs.

Now, after starting Jenkins install the plugins for SonarQube.

plugin:- SonarQubeScanner

https://plugins.jenkins.io/sonar/

  1. Open Jenkins and click on manage Jenkins -> Global tool.
  2. Enter required details for example:- SonarQube Scanner path and name.
  3. Click on Apply and save.
  4. Open Jenkins and click on again manage Jenkins -> Configure.
  5. Enter the name and server_ip of the SonarQube Server.
  6. click on Apply and save.

After completing the above process. Now we create a Jenkins pipeline.

  1. Open Jenkins and click on new-item.
  2. enter the name and select the pipeline.
  3. Click Ok.
  4. Now, enter the pipeline syntax.
pipeline {
agent any
environment {
PATH = "/var/jenkins_home/sonar-scanner-4.8.0.2856-linux:${env.PATH}"
SONAR_HOST_URL = "https://sonarqube-example.com"
SONAR_TOKEN = "sqa_xxxxxxxxxxxxxxxxxxxxxx"
scannerHome = tool name: 'SonarScanner'
}

stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'http://gitlab-example.com/test/demo.git']]])
}
}

stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('sonar') {
sh """
${scannerHome}/bin/sonar-scanner \
-Dsonar.host.url=${SONAR_HOST_URL} \
-Dsonar.login=${SONAR_TOKEN} \
-Dsonar.projectKey=test \
-Dsonar.sources=. \
"""
}
}
}
}
}

Conclusion

This is a fully configured Jenkins pipeline that integrates Jenkins, SonarQube, and SonarScanner. Also Configured and install SonaQube, and Jenkins on the OpenShift Container Platform.

In case of any questions, comment in the comments section.

Thank You