Creation of Pipe line Job & Perform CI-CD stages using Groovy code

What is Pipe line Job 

Jenkins’s pipeline is implemented as a code which allows multiple users to edit and execute the pipeline process, Pipelines are robust. … 

You can pause the pipeline process and make it wait to resume until there is an input from the user, Jenkins Pipelines support big projects. 

Advantages of Pipe line job 

It is also known as the Heart of the DevOps pipeline. There are a lot of benefits of using the Jenkins pipeline; a few of them are given below: Open Source and Free: Developers don’t need to take tension about the money; it is free of cost. 

Build Pipeline plugin 

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax. 

Generating the Groovy code 

To create Groovy-based project, add new free-style project and select “Execute Groovy script” in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory. 

Perform CI-CD stages using Groovy code 

The steps to build CI/CD pipeline with Jenkins are: 1. Download Jenkins Download Jenkins from the Jenkins downloads page ‘ https://www.jenkins.io/download/ ’. Download the file ‘Generic Java package (.war)’. 2. Execute Jenkins as a Java binary Open the terminal window and enter cd . 

++++++++++++++++ 

PIPELINE  

——————— 

Implementing CI-CD  from the level of code. 

This code is created using groovy script, and this file is also called as jenkins file. 

Advantges 

—————– 

As pipeline is implemented as code, it gives the developers the ability to upload into vesion controlling system from where they can edit and review the script. 

Pipelines can accept interactive human input before continuning with specific stage in CI-CD 

Ex: Before deployment into production environment, pipeline script can accept  approval 

from the delivery head and then continue. 

Pipeline script support complex realtime scenario where we can implement conditional statements, loops etc. 

Ex: If testing passes, we want to go to delivery. 

     If its fails, we want to send automated emails. 

Scripted pipeline syntax: 

———————————— 

node ( ‘master/slave’) 

         stage(‘ Stage in CI-CD’) 

           { 

                   Groovy  code for implementing the stage 

           } 

++++++++++++++++++ 

Install Build pipeline plugin 

+++++++++++++++++++++++ 

Ex: 

Create new item —  ScriptedPipeline 

select pipeline  –OK 

Pipeline tab, 

pipeline syntax 

Sample step – node: Allocate node 

label – master 

Generate piplescript  — copy the groovy code and paste in pipeline tab. 

————- 

In pipeline syntax  

Sample step – stage:Stage 

Stage name – Continuous Download 

Generate piplescript  — copy the groovy code and paste in pipeline tab. 

—– 

In pipeline syntax  

Sample step – git:Git 

Repository URL – https://github.com/sunildevops77/maven.git 

Generate piplescript  — copy the groovy code and paste in pipeline tab. 

————- 

Apply  — Save –> Run the job 

++++++++++++++++++++++++++++++++++++++++++ 

2nd stage 

—————– 

We need to run   ‘mvn package’ command. 

This command can be executed as a shell script 

In pipeline syntax: 

Sample step – sh: Shell Script 

Stage name – mvn package 

Generate piplescript  — copy the groovy code and paste in pipeline tab. 

Save and run. 

++++++++++++++++++++++++++++ 

Step 3: Deployment 

We need to establish password less SSH connection between  Dev server and QA Server 

Connect to QA server using gitbash 

Set the password for ubuntu 

$ sudo passwd  ubuntu 

Edit sshd_config  ( Password authentication — yes) 

$  cd /etc/ssh 

$ sudo vim sshd_config 

Go to insert mode 

) change password authentication to yes 

13) Save and quit :wq 

14) Restart the service 

$ sudo service ssh restart 

15) Connect to dev server using gitbash and generate  ssh keys 

$   ssh-keygen 

Overwrite ?  n 

18) copy the keys to QA server 

ssh-copy-id  ubuntu@private_ip_qa_server 

ssh-copy-id  ubuntu@172.31.47.36 

Test are you able to connect to qa? 

$ ssh ubuntu@172.31.47.36 

$ exit   ( To come back to dev server) 

Now, you can copy the files from dev server to QA server 

Create a file in dev server 

$ cat > file1 

fdsfgfdsgfdsgd 

Ctrl +d 

$  

To copy the file in QA server 

Syntax: 

$ scp(sourcecopy)  source   destination 

$ scp f1 ubuntu@172.31.22.80:/tmp/f2 

file1  will be copied into qa server with the name file2 

Lets check for the file, by connecting to qa server 

$ ssh ubuntu@172.31.47.36 

$ cd /tmp 

$ ls 

$ cat  file2 

$ exit 

++++++++++++++++++++++++++ 

Deployment is nothing but , copying the war file from dev server to qa server 

Get the location of war file from log  

$ scp  /home/ubuntu/.jenkins/workspace/ScripPipeline/webapp/target/webapp.war   ubuntu@172.31.24.75:/var/lib/tomcat8/webapps/prodenv.war 

Get the groovy code of scp command 

Sample Step – sh: Shell Script 

Shell script —   copy the scp command which we have created 

Generate the code and paste in pipeline script 

Apply —  save — run 

Deployment fails 

Observe  the log file  ( permissions denied ) 

To give the permissions 

Connect to qa server  using git bash 

$ cd  /var/lib 

$ ls -ld tomcat8 

( Observation:   tomcat8  directory  — others is not having write permissions ) 

$ sudo  chmod  -R  o+w   tomcat8/ 

Now run the job 

+++++++++++++++++++= 

Connect qa server and check 

+++++++++++++++++++++++++++++++++ 

4th Stage: Continuous testing 

In pipeline —  add a new stage 

Shell script —   echo “Tesing Passed” 

Generate the groovy code  and copy paste 

Apply — save– run 

+++++++++++++++++++++++++++++++++++ 

5th Stage : continuous delivery 

In pipeline —  add  a new stage 

Copy the code in the – continuousdeployment and change the qa_ipaddress to prod_Ip_address 

Also change the context path  –  prodenv 

( We need to establish password less ssh between devserver and prodserver) 

( we should change tomcat8 permissions ) 

Connect to prod server using gitbash 

Set the password for ubuntu 

$ sudo passwd  ubuntu 

Edit sshd_config  ( Password authentication — yes) 

$  cd /etc/ssh 

$ sudo vim sshd_config 

Go to insert mode 

) change password authentication to yes 

13) Save and quit :wq 

14) Restart the service 

$ sudo service ssh restart 

15) Connect to dev server using gitbash and generate  ssh keys 

$   ssh-keygen 

Overwrite ?  n 

18) copy the keys to Prod server 

ssh-copy-id  ubuntu@private_ip_prod_server 

ssh-copy-id  ubuntu@172.31.40.134 

Test are you able to connect to prod? 

$ ssh ubuntu@172.31.40.134 

$ exit   ( To come back to dev server) 

_______________ 

To give the permissions 

Connect to prod server  using git bash 

$ cd  /var/lib 

$ ls -ld tomcat8 

( Observation:   tomcat8  directory  — others is not having write permissions ) 

$ sudo  chmod  -R  o+w   tomcat8/ 

Now run the job 

Connect prod server and check 

http://13.126.45.247:8080/prodenv/ 

+++++++++++++++++++++++++++++++++++++++++++++ 

Script 

——— 

node(‘master’) 

stage(‘Continuous Download’)  

 { 

    git ‘https://github.com/sunildevops77/maven.git‘ 

stage(‘Continuous build’)  

 { 

   sh label: ”, script: ‘mvn package’ 

stage(‘Continuous Deployment’)  

 { 

sh label: ”, script: ‘scp  /home/ubuntu/.jenkins/workspace/ScriptedPipeline/webapp/target/webapp.war  ubuntu@172.31.21.16:/var/lib/tomcat8/webapps/qaenv.war‘ 

stage(‘Continuous Testing’)  

 { 

sh label: ”, script: ‘echo “Testing Passed”‘ 

stage(‘Continuous Delivery’)  

 { 

sh label: ”, script: ‘scp  /home/ubuntu/.jenkins/workspace/ScriptedPipeline/webapp/target/webapp.war  ubuntu@172.31.28.16:/var/lib/tomcat8/webapps/prodenv.war‘ 

+++++++++++++++ 

13.126.48.87:8080/qaenv 

13.127.24.219:8080/prodenv 

Leave a Comment

MFH IT Solutions (Regd No -LIN : AP-03-46-003-03147775)

Consultation & project support organization.

Contact

MFH IT Solutions (Regd)
NAD Kotha Road, Opp Bashyam School, Butchurajupalem, Jaya Prakash Nagar Visakhapatnam, Andhra Pradesh – 530027