Jenkins - Day - 3

technonotes-hacker - Oct 12 '23 - - Dev Community

Pipeline concept

Image description

Select Pipeline Project after giving the name.

Image description

Image description

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

Image description

Image description

Image description

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'
      }
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

Image description

Python File 1

print('Hello DevOps')
Enter fullscreen mode Exit fullscreen mode

Python File 2

print('Hello World')
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Console Output

Image description

Image description

Pipeline Logs

Image description

Image description

Image description

Image description

Image description

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

Create Project & Add the Git , START to EXPLORE

Image description

Image description

Image description

Image description

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

Image description

Lets Change & Execute one by one

Image description

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....'
        }
    }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

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
                '''
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

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'
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

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

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

Usage of POST in the Pipeline script

Jenkinsfile3.txt

Image description

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'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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')
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Apply and Save

Image description

Image description

Image description

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player