Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Jenkins Declarative Pipeline creating ..

You need to create a Jenkins declarative pipeline for Java Application


that
Pulls the code from github, test , build it , save artifact to Nexus , QA
with sonarqube , deploy to tomcat and finally send notifaction on slack
workspace about the job status.

Goals:

Stages:

Stage 1: Pull the code for git hub


Stage 2: build and Test using maven
Stage 3: QA test using sonarqube
Stage 4: Save Atifacts to Nexus
Stage 5: Deploy to Tomcat server
Stage 6: Notify on Slack Status of Pipeline.

Assigned by Vijay :

To : Mohammed Abdul Mubeen & Mohammad Muqeeth.

Solution:

Page | 1
Architecture Diagram

Installations Required
First we have taken two Centos Servers in AWS with t3.medim computer power and 8gb of
storage.. Allowed Jenkins port 8080 and in sonar&nexus instance allowed port for sonar and
nexus that is 8081 and 8080 ,9000 for sonar & in another instance.

First update instance

$sudo yum install update -y

$Java, wget, unzip, Jenkins, sonarqube, nexus, apache tomcat.

Page | 2
Installed Jenkins in Jenkins server

sudo wget -O /etc/yum.repos.d/jenkins.repo \


https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum upgrade
sudo yum install epel-release java-11-openjdk-devel
sudo yum install jenkins

Page | 3
Now Jenkins server is up and running .. Lets move to another server that is sonar&Nexus server

wget https://download.sonatype.com/nexus/oss/nexus-2.14.18-01-bundle.tar.gz

➢ sudo yum update -y


➢ wget https://download.sonatype.com/nexus/oss/nexus-2.14.18-01-bundle.tar.gz
➢ tar -xvzf nexus-2.14.18-01-bundle.tar.gz
➢ mv nexus-2.14.18-01 nexus
➢ rm nexus-2.14.18-01-bundle.tar.gz
➢ cd nexus/
➢ cd bin/
➢ sh nexus start
➢ netstat -ntpl

Nexus is up and running now

Page | 4
Goto jekins host

Create a job of type pipeline

Page | 5
With help of Jenkins snippet generator generate the declarative jekins code

Page | 6
Stage 1 pulling the code from Github

Now create Generate a declerative pipeline for our first stages

stages {

stage('PullCodeFromGitHub') {

steps {

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],


userRemoteConfigs: [[url: 'https://github.com/mubeen507/JavaWebCalculator.git']]])

We are using checkout from version control in snippet generator to script as shown in fig below

Page | 7
Copy the generated code to stage

Stage 2 Build and test using maven

stage('test') {

steps {

sh 'mvn test'

stage('package') {

steps {

sh 'mvn clean package'

As maven will test and build no need of snippet generator here..

Page | 8
Stage 3 QA test using SonarQube

Install sonarqube..

https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.7.zip

mvn sonar:sonar -Dsonar.host.url=http://44.200.203.134:9000 -


Dsonar.login=aeb2f818a6e28cd70987043ad81aa5046953c53a'

Stage 4 Uploading Artifact to Nexus Repo

stage('nexusupload') {

Page | 9
steps {

nexusArtifactUploader artifacts: [[artifactId: 'webapp', classifier: '', file: 'target/webapp-


0.2.war', type: 'war']], credentialsId: 'nexus', groupId: 'com.web.cal', nexusUrl:
'3.208.13.92:8081/nexus', nexusVersion: 'nexus2', protocol: 'http', repository: 'releases', version:
'0.2'

Using nexus artifact upload uloader in snipper generator generated the above script as shown
below

Page | 10
Artifacts stored in releases folder as shown in pic below

Page | 11
Page | 12
Just copy the generated script and add to job in stage

….

Stage 5 Deploy to Tomcat Server


Generated from snippet

deploy adapters: [tomcat8(credentialsId: 'tom', path: '', url: 'http://172.31.17.130:8080')],


contextPath: null, war: '**/*.war'

Page | 13
Start your tomcat server

Page | 14
Login to tomcat

Page | 15
Stage 6 Notify to slack Group
Generated using snippted

slackSend channel: 'jenkinspipeline-group', message: 'slackSend "started ${env.JOB_NAME}


${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"', teamDomain: 'jenkinspipeline-group',
tokenCredentialId: 'slackuser'

Page | 16
Click advance

Page | 17
Finally received notification on slack about job status….

Steps to config Jenkins app on slack


workspace

Page | 18
Jenkins CI

Added by mubeen507 on February 2nd, 2022


Disable • Remove
Jenkins CI is a customizable continuous integration server with over 600 plugins, allowing you
to configure it to meet your needs.This integration will post build notifications to a channel in
Slack.
close
Setup Instructions
Here are the steps necessary to add the Jenkins CI integration.
Note: These instructions are for v2.8. To install an older version, go down to Previous
Setup Instructions.

Step 1
In your Jenkins dashboard, click on Manage Jenkins from the left navigation.

Page | 19
Step 2
Click on Manage Plugins and search for Slack Notification in the Available tab. Click the
checkbox and install the plugin.

Step 3
After it's installed, click on Manage Jenkins again in the left navigation, and then go
to Configure System. Find the Global Slack Notifier Settings section and add the following
values:

• Team Subdomain: jenkinspipeline-group


• Integration Token Credential ID: Create a secret text credential
using 6Ooe2L1jGEAXmIdFhRrNTexq as the value

The other fields are optional. You can click on the question mark icons next to them for
more information. Press Save when you're done.

Page | 20
Note: Please remember to replace the Integration Token in the screenshot below with your
own.

Step 4
For each Project that you would like receive notifications for, choose Configure from the
project's menu.

Page | 21
Step 5
Then you'll need to add Slack Notifications to the Post-build Actions for this project.

Step 6
In the Slack Notifications section, choose the events you'd like to be notified about.

expand
Previous Setup Instructions (v2.1 and lower)
Here are the instructions for installing older versions of the Slack Notifier.

Integration Settings
Post to Channel
Notifications that come in from Jenkins CI will be posted here.

team-updates
or create a new channel

Token
This token is used as the key to your Jenkins CI integration.

Regenerate

Page | 22
Jenkins Pipeline script

pipeline {

agent any

stages {

stage('PullCodeFromGitHub') {

steps {

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],


userRemoteConfigs: [[url: 'https://github.com/mubeen507/JavaWebCalculator.git']]])

stage('test') {

steps {

sh 'mvn test'

stage('package') {

steps {

sh 'mvn clean package'

stage('sonartest') {

steps {

sh 'mvn sonar:sonar -Dsonar.host.url=http://44.200.203.134:9000 -


Dsonar.login=aeb2f818a6e28cd70987043ad81aa5046953c53a'

Page | 23
}

stage('nexusupload') {

steps {

nexusArtifactUploader artifacts: [[artifactId: 'webapp', classifier: '', file: 'target/webapp-


0.2.war', type: 'war']], credentialsId: 'nexus', groupId: 'com.web.cal', nexusUrl:
'172.31.17.130:8081/nexus', nexusVersion: 'nexus2', protocol: 'http', repository: 'releases', version:
'0.2'

stage('Tomcat') {

steps {

deploy adapters: [tomcat8(credentialsId: 'tom', path: '', url:


'http://172.31.17.130:8080')], contextPath: null, onFailure: false, war: '**/*.war'

stage('notifyonslack') {

steps {

slackSend channel: 'team-updates', message: "slackSend 'started ${env.JOB_NAME}


${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)'", teamDomain: 'jenkinspipeline-group',
tokenCredentialId: 'slackuser'

Page | 24
Stage view

Page | 25

You might also like