-
Notifications
You must be signed in to change notification settings - Fork 1
09_Pipeline_Section
bhupender rawat edited this page Jun 20, 2021
·
6 revisions
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.
-
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"
}
}
}
}
- 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}"
}
}
}
}
- 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}"
}
}
}
}
}
- 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'
}
}
}
}
}
}
- 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"
}
}
}
- 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"
}
}
}