Tuesday, August 29, 2023

Jenkins Pipeline

Jenkins


Login to Jenkins instance. Create a pipeline script test-pipeline
Click on New Item -> Enter the Item Name -> Click on Pipeline -> Click ok
Scroll down to Pipeline section, write the pipeline script. For now we will create a script that consists of
1. Pipeline structure
2. Stage/Stages
3. Steps

Within the Pipeline structure, we will have Stage or Stages and within Stage or Stages we will have steps
A simple script would like this

Click on save and click on Build Now
Once the Build is complete, the console output will look like this

Lets add more steps, click on Configure and enter the following code
Which has three stages and each stage containing one step each

Click on Build Now, you see the following screenshot displaying each stage, with success or failure and log messages. Click on each green area and click on Logs
Check the console output, it will look like
Lets Add sh, build, checkout, git, input into the pipeline script
Using sh - we are executing commands
Using build - we are running 'echo-test' script
Using checkout and git to pull repository
Input is used to review, Proceed or Abort the pipeline build

pipeline {
    agent any
    stages {
        stage('check Docker version') {
            steps {
                sh '/usr/bin/docker --version'
            }
        }
        stage('check Linux version') {
            steps {
                sh 'uname -a'
            }
        }
        stage('running echo-test') {
           steps {
               build 'echo-test'
           }
        } 
        stage('pulling git repository') {
           steps {
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'd4cd4712-affc-4933-a18f-dfbb90338097', url: 'https://github.com/yashwanthsn2020/automation.git'], [credentialsId: 'd4cd4712-affc-4933-a18f-dfbb90338097', url: 'https://github.com/yashwanthsn2020/yashwanthsn.git']]])
           }
        }
        stage('git:automation') {
           steps {
               git credentialsId: 'd4cd4712-affc-4933-a18f-dfbb90338097', url: 'https://github.com/yashwanthsn2020/automation.git'
            }
        }
        stage('approve') {
           steps {
               input 'Approve'
            }
        }
        
        
    }
}
Click Save -> Click on Build now
The Console output looks like this
Success Console Output

Started by user yashwanthsn
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/version-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (check Docker version)
[Pipeline] sh
+ /usr/bin/docker --version
Docker version 19.03.11, build 42e35e61f3
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check Linux version)
[Pipeline] sh
+ uname -a
Linux yashwanthsn-VirtualBox 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (running echo-test)
[Pipeline] build (Building echo-test)
Scheduling project: echo-test
Starting building: echo-test #18
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (pulling git repository)
[Pipeline] checkout
using credential d4cd4712-affc-4933-a18f-dfbb90338097
using credential d4cd4712-affc-4933-a18f-dfbb90338097
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from 2 remote Git repositories
 > git config remote.origin.url https://github.com/yashwanthsn2020/automation.git # timeout=10
Fetching upstream changes from https://github.com/yashwanthsn2020/automation.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://github.com/yashwanthsn2020/automation.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin1.url https://github.com/yashwanthsn2020/yashwanthsn.git # timeout=10
Fetching upstream changes from https://github.com/yashwanthsn2020/yashwanthsn.git
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://github.com/yashwanthsn2020/yashwanthsn.git +refs/heads/*:refs/remotes/origin1/* # timeout=10
Seen branch in repository origin/20Jun20
Seen branch in repository origin/master
Seen branch in repository origin/mytest
Seen branch in repository origin/revert-5-test
Seen branch in repository origin/test
Seen branch in repository origin/yashwanthsn2020-workflow
Seen branch in repository origin1/20Jun20
Seen branch in repository origin1/master
Seen branch in repository origin1/mytest
Seen 9 remote branches
 > git show-ref --tags -d # timeout=10
Checking out Revision cd91b37fa672d01052a1ece9b475ed88ef0f55a0 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f cd91b37fa672d01052a1ece9b475ed88ef0f55a0 # timeout=10
Commit message: "Merge pull request #13 from yashwanthsn2020/test"
 > git rev-list --no-walk cd91b37fa672d01052a1ece9b475ed88ef0f55a0 # timeout=10

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (git:automation)
[Pipeline] git
using credential d4cd4712-affc-4933-a18f-dfbb90338097
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/yashwanthsn2020/automation.git # timeout=10
Fetching upstream changes from https://github.com/yashwanthsn2020/automation.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://github.com/yashwanthsn2020/automation.git +refs/heads/*:refs/remotes/origin/* # timeout=10

 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision cd91b37fa672d01052a1ece9b475ed88ef0f55a0 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f cd91b37fa672d01052a1ece9b475ed88ef0f55a0 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master cd91b37fa672d01052a1ece9b475ed88ef0f55a0 # timeout=10
Commit message: "Merge pull request #13 from yashwanthsn2020/test"
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (approve)
[Pipeline] input
Approve
Proceed or Abort

Approved by yashwanthsn
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Check http://<jenkinsurl>:8080/job/parameterized-pipeline/pipeline-syntax/ to learn more on different Steps available