Last active
September 24, 2022 17:18
-
-
Save devops-school/4f9df842702dba170b11ae2c8dedf632 to your computer and use it in GitHub Desktop.
Jenkins Pipeline Example Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to apply conditions aka Flow Control in jenkins scripted pipeline? | |
======================================================== | |
node { | |
stage('Example') { | |
if (env.BRANCH_NAME == 'master') { | |
echo 'I only execute on the master branch' | |
} else { | |
echo 'I execute elsewhere' | |
} | |
} | |
} | |
stage('UT') { | |
// Run the maven build | |
if (isUnix()) { | |
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean test" | |
} else { | |
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean test/) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decalartive Pipeline | |
=================================== | |
pipeline { | |
agent { docker { image 'maven:3.3.3' } } | |
stages { | |
stage('build') { | |
steps { | |
sh 'mvn --version' | |
} | |
} | |
} | |
} | |
Scripted Pipeline | |
node { | |
stage('Build') | |
{ | |
sh 'echo rajesh' | |
} | |
} | |
node { | |
stage('Build') | |
{ | |
sh "echo Build" | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploy" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
} | |
} | |
node { | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploy" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
} | |
} | |
# This code would run in a node called 'linux' | |
node('linux') { | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploy" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
} | |
} | |
node { | |
def mvnHome | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
mvnHome = tool 'rajesh-maven' | |
sh "'${mvnHome}/bin/mvn' clean compile" | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploy" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
sh "'${mvnHome}/bin/mvn' clean test" | |
} | |
} | |
node { | |
def mvnHome | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
mvnHome = tool 'rajesh-maven' | |
sh "'${mvnHome}/bin/mvn' clean compile" | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploy" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
sh "'${mvnHome}/bin/mvn' clean test" | |
junit '**/target/surefire-reports/TEST-*.xml' | |
} | |
} | |
node { | |
def mvnHome | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
mvnHome = tool 'rajesh-maven' | |
sh "'${mvnHome}/bin/mvn' clean compile" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
sh "'${mvnHome}/bin/mvn' clean test" | |
junit '**/target/surefire-reports/TEST-*.xml' | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploying..." | |
sh 'ansible-playbook -i inventory ansible-playbook.yaml' | |
} | |
} | |
node { | |
def mvnHome | |
stage('Build') | |
{ | |
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven' | |
sh "echo Build" | |
mvnHome = tool 'rajesh-maven' | |
sh "'${mvnHome}/bin/mvn' clean compile" | |
} | |
stage('Test') | |
{ | |
sh "echo Test" | |
sh "'${mvnHome}/bin/mvn' clean test" | |
junit '**/target/surefire-reports/TEST-*.xml' | |
} | |
stage('Package') | |
{ | |
sh "echo Package" | |
sh "'${mvnHome}/bin/mvn' clean package" | |
archive 'target/*.jar' | |
} | |
stage('Deploy') | |
{ | |
sh "echo Deploying..." | |
sh 'ansible localhost -m setup' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abcs = ['a', 'b', 'c'] | |
node('master') { | |
stage('Test 1: loop of echo statements') { | |
echo_all(abcs) | |
} | |
stage('Test 2: loop of sh commands') { | |
loop_of_sh(abcs) | |
} | |
stage('Test 3: loop with preceding SH') { | |
loop_with_preceding_sh(abcs) | |
} | |
stage('Test 4: traditional for loop') { | |
traditional_int_for_loop(abcs) | |
} | |
} | |
@NonCPS // has to be NonCPS or the build breaks on the call to .each | |
def echo_all(list) { | |
list.each { item -> | |
echo "Hello ${item}" | |
} | |
} | |
// outputs all items as expected | |
@NonCPS | |
def loop_of_sh(list) { | |
list.each { item -> | |
sh "echo Hello ${item}" | |
} | |
} | |
// outputs only the first item | |
@NonCPS | |
def loop_with_preceding_sh(list) { | |
sh "echo Going to echo a list" | |
list.each { item -> | |
sh "echo Hello ${item}" | |
} | |
} | |
// outputs only the "Going to echo a list" bit | |
//No NonCPS required | |
def traditional_int_for_loop(list) { | |
sh "echo Going to echo a list" | |
for (int i = 0; i < list.size(); i++) { | |
sh "echo Hello ${list[i]}" | |
} | |
} | |
// echoes everything as expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to apply try catch in jenkins scripted pipeline? | |
------------------------------- | |
node { | |
stage('Example') { | |
try { | |
sh 'exit 1' | |
} | |
catch (exc) { | |
echo 'Something failed, I should sound the klaxons!' | |
throw | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to define var in jenkins scripted pipeline? | |
# init a variable | |
def mvnHome | |
# set a value of it | |
mvnHome = tool 'maven' | |
# How to use | |
- ${mvnHome} | |
- sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean compile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment