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”

}

}
}
}

// build with parameterspipeline {
agent any
parameters {
choice(
// choices are a string of newline separated values
// https://issues.jenkins-ci.org/browse/JENKINS-41180
choices: 'create\nupdate',
description: '',
name: 'REQUESTED_ACTION')

choice(
// choices are a string of newline separated values
// https://issues.jenkins-ci.org/browse/JENKINS-41180
choices: '100\n50',
description: '',
name: 'percentage')
}
stages {
stage ('Create') {
when {
// Only say hello if a "greeting" is requested
expression { params.REQUESTED_ACTION == 'create' }
}
steps {
echo "Hello, create!"
}
}

stage ('Percentage') {
when {
// Only say hello if a "greeting" is requested
expression { params.percentage == '100' }
}
steps {
echo "Hello, 100!"
}
}
}
}

drop down in jenkinspipeline

pipeline {
agent none
stages {
stage ('First') {
agent any
steps {
echo "First dummy stage"
}
}
stage ('Input') {

steps {
script {
myStage = input message: 'What service type do you want to run now?', parameters: [choice(choices: 'Create\nUpdate', description: '', name: 'Stage')]
}
echo myStage
}
}
stage('Stage1') {
when {
expression { myStage == 'Create' }
}
steps {
echo "Running Service: Create"
}
}
stage('Stage2') {
when {
expression { myStage == 'Update' }
}
steps {
echo "Running Service:Update"
}
}
}
}

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’])

}
}
}
}

Written by

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