Skip to content

09_Pipeline_Section

bhupender rawat edited this page Jun 20, 2021 · 6 revisions

Pipeline Sections

In this lab we will create a Jenkins Declarative Pipeline exploring basic elements of the pipeline :

  • Create a Jenkins pipeline that will print Hello World
  • Add a parameter in Jenkins Pipeline that will take a name and print Hello
  • Add a stage to print the code of the name provided if the code is present in a secret file available in the credential
  • Add an option to take user input while running the pipeline if we want to fail the pipeline
  • Add a stage to print success/failure message
  • Update the pipeline to execute every 5 minutes
  • Ensure that if the pipeline is running for more than a minute it should fail.

Hello World Job

  • Install these plugins first:-

    • Pipeline Utility Steps
  • Create a new Pipeline Porject named "hello-world-pipeline"

  • Configure the job and go to the "Pipeline" section and paste this snippet


pipeline {
    agent any
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello World"
            }
        }
    }
}

Parameter in Jenkins Pipeline

  • Configure the Job and Add paramater in Jenkins with "NAME"


  • Go to the "Pipeline" section and paste this snippet

pipeline {
    agent any
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello ${NAME}"
            }
        }
    }
}

Printing Secret in Jenkins Pipeline

  • Create a credential in Jenkins with secret text.


  • Add another stage to print credentials

pipeline {
    agent any
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello ${NAME}"
            }
        }
        stage("Printing credential") {
            steps {
                withCredentials([string(credentialsId: 'tony', variable: 'secrettext')]) {
                    echo "${secrettext}"
                }
            }
        }
    }
}

Job Success and Failure Input

  • Go to the "Pipeline" section and paste this snippet

pipeline {
    agent any
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello ${NAME}"
            }
        }
        stage("Printing credential") {
            steps {
                withCredentials([string(credentialsId: 'tony', variable: 'secrettext')]) {
                    echo "${secrettext}"
                }
            }
        }
        stage("Pipeline status Input") {
            steps{
                script {
                    jobStatus = input message: '', parameters: [choice(choices: ['Success', 'Failure'], description: '', name: 'Status')]
                    if("${jobStatus}" == "Success") {
                        echo "I am continuing"
                    } else {
                        error 'I am killing'
                    }
                }
            }
        }
    }
}

Pipeline Post Analysis

  • Go to the "Pipeline" section and paste this snippet

pipeline {
    agent any
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello ${NAME}"
            }
        }
        stage("Printing credential") {
            steps {
                withCredentials([string(credentialsId: 'tony', variable: 'secrettext')]) {
                    echo "${secrettext}"
                }
            }
        }
        stage("Pipeline status Input") {
            steps{
                script {
                    jobStatus = input message: '', parameters: [choice(choices: ['Success', 'Failure'], description: '', name: 'Status')]
                    if("${jobStatus}" == "Success") {
                        echo "I am continuing"
                    } else {
                        error 'I am killing'
                    }
                }
            }
        }
    }
    post {
        success {
            echo "Job is successful"
        }
        failure {
            echo "Job is unsuccessful"
        }
    }
}

Pipeline Execution at every 5 minutes

  • Configure the job and go to build triggers and these setting


  • Go to the "Pipeline" section and paste this snippet

pipeline {
    agent any
    options {
        timeout(time: 1, unit: 'MINUTES')
    }
    stages {
        stage("Printing hello world") {
            steps {
                echo "Hello ${NAME}"
            }
        }
        stage("Printing credential") {
            steps {
                withCredentials([string(credentialsId: 'tony', variable: 'secrettext')]) {
                    echo "${secrettext}"
                }
            }
        }
        stage("Pipeline status Input") {
            steps{
                script {
                    jobStatus = input message: '', parameters: [choice(choices: ['Success', 'Failure'], description: '', name: 'Status')]
                    if("${jobStatus}" == "Success") {
                        echo "I am continuing"
                    } else {
                        error 'I am killing'
                    }
                }
            }
        }
    }
    post {
        success {
            echo "Job is successful"
        }
        failure {
            echo "Job is unsuccessful"
        }
    }
}



Previous: Continous Integration                    Next: Declaritive Pipeline