Jenkins - Day - 3
Published: 2023-10-12 03:22:50

### Pipeline concept

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

> **Select Pipeline Project after giving the name.**

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

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

> **Pull the code " https://github.com/tkdhanasekar/jenkins-python.git "**

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

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

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

> **Save the project and build now.**

#### Jenkins File

```
pipeline {
  agent any
  stages {
    stage('version') {
      steps {
        sh 'python3 --version'
      }
    }
    stage('hello') {
      steps {
        sh 'python3 hello.py'
        sh 'python3 world.py'
      }
    }
  }
}

```

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

#### Python File 1

```
print('Hello DevOps')
```

#### Python File 2

```
print('Hello World')
```

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

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

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

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

> **Console Output**

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

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

> **Pipeline Logs**

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

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

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

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

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

### Let's Try with "https://github.com/Zulaikha12/git-test"

> **Create Project & Add the Git , START to EXPLORE**

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

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

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

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

> **Its trying to Check & Gets into ERROR , so this is where we need to provide the FILE NAME.**

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

> **Lets Change & Execute one by one**

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

#### Jenkinsfile.txt

```
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building.. This is the build phase'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing.. This is the testing phase'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....  This is the deployment phase'
            }
        }
	stage('Postdeploy') {
		steps {
			echo 'Postdeployment phase....'
		}
	}
    }
}
```

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

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

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

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

> **Markdown inside the script & Paste the script rather than calling the file name in GIT**

#### Jenkinsfile1.txt

```
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}
```

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

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

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

From where this "ls -lah" got executed ? [TBD]


> **Passing SH script**

#### Jenkinsfile2.txt

```
pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}
```

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

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

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

```
ERROR:
+ ./flakey-deploy.sh
/var/lib/jenkins/workspace/pipeline_Zulaikha12_Jenkinsfile2@tmp/durable-1c6bcb96/script.sh: 1: ./flakey-deploy.sh: not found
```

Whether we need to keep the script over this location ? [TBD]

> **Usage of POST in the Pipeline script**

#### Jenkinsfile3.txt

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

```
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                 echo 'Fail!'; exit 1
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}
```

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

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

> **Approve or Reject**

#### Jenkinsfile4.txt

```
pipeline {
    agent any

    stages {
        stage('Input') {
            steps {
                input('Do you want to proceed?')
            }
        }

        stage('If Proceed is clicked') {
            steps {
                print('hello')
            }
        }
    }
}
```

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

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

> **Writing scipt in Panel**

#### script.txt

```
pipeline{
    agent any
    stages{
        stage("dev"){
            echo"This is DEV environment"
        }
        stage("UAT"){
            echo"Testing in UAT"
        }
        stage("PROD"){
            echo"Go-live"
        }
    }
}
```

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

> **Apply and Save**

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

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

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

### Important Points to be Remembered

> **_What is SCM ?_**

In Jenkins, SCM stands for "**Source Code Management**". This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.

> **_What is Agent in Pipeline script ?_**
> **_What is Any in AGENT ?_**

Its like , Agent can run in any NODE. Node means OS.

> **Keep this always Enabled " Use Groovy Sandbox?**

### Reference :

https://github.com/Zulaikha12/git-test
https://github.com/tkdhanasekar/jenkins-python/
https://www.slideshare.net/EdurekaIN/jenkins-pipeline-tutorial-continuous-delivery-pipeline-using-jenkins-devops-training-edureka?from_search=8
https://www.liatrio.com/blog/building-with-docker-using-jenkins-pipelines
https://github.com/tkdhanasekar/jenkins-ruby/
https://github.com/tkdhanasekar/jenkins-php