Jenkins Pipeline Examples

Please visit my website and subscribe to my youtube channel for more articles

https://devops4solutions.com/

It is used to create stages/pipeline of all the steps which are required for building the project.

How to initialize software and set path

stage(‘Terraform Init’) {
steps {
script {
def tfHome = tool name: ‘Terraform’
def anHome = tool name: ‘Ansible’
env.PATH = “${anHome}:${env.PATH}”
env.PATH = “${tfHome}:${env.PATH}”
}
sh ‘terraform — version’
sh ‘ansible — version’

}
}

How to declare variable

def my_list

pipeline {
agent any
stages {
stage(‘stage1’) {
steps {
script {
my_list = “nidhi”
}
}
}

stage(‘stage2’) {
steps {
echo “$my_list”

}
}
}
}

Second Example
def test=”nik”

pipeline {
agent any
stages {



stage(‘stage2’) {
steps {
echo “$test”

}
}
}
}

Example of pipeline which will use variables/checkout/dockerpush

def source=”/var/jenkins_home/workspace/mvv-web-pipeline1/MVVWeb/build/libs/.jar”
def destination=”MVVWeb/docker/”
def DTR=”dtest”
def DOCKER_NAME=”mvv/mvv-web”
pipeline {
agent any
stages {
stage(“Checkout”) {
steps {
git url: ‘ssh://git@bitbucket.sandata.com:7999/ohio/automation_scripts.git’
}
}
stage(“Compile”) {
steps {
dir (‘MVVWeb’) {
sh “./gradlew build”
}
}
}
stage(“Copy”) {
steps {

sh “cp /var/jenkins_home/workspace/mvv-web-pipeline1/MVVWeb/build/libs/*.jar /var/jenkins_home/workspace/mvv-web-pipeline1/MVVWeb/docker”
}

}
stage(“Docker build”) {
steps {
dir(‘MVVWeb/docker’)
{
sh “docker build -t ${DTR}/${DOCKER_NAME}:v$BUILD_ID .”
}
}
}

stage(“Docker push”) {
steps {
sh “docker login ${DTR} — username=peter — password=mypass”
sh “docker push ${DTR}/${DOCKER_NAME}:v$BUILD_ID”

}

}
}
}

drop down in jenkinspipeline

def userInput = input(
id: ‘userInput’, message: ‘Let\’s promote?’, parameters: [
[$class: ‘TextParameterDefinition’, defaultValue: ‘uat’, description: ‘Environment’, name: ‘env’],
[$class: ‘TextParameterDefinition’, defaultValue: ‘uat1’, description: ‘Target’, name: ‘target’]
])
pipeline {
agent any
stages {

stage (‘promotion’)
{
steps{
echo (“Env: “+userInput[‘env’])
echo (“Target: “+userInput[‘target’])

}
}
}
}

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store