Jenkins Declarative Pipeline
First Declarative Pipeline
pipeline {
agent any
stages {
stage(' Welcome Step' ) {
steps {
echo ' Welcome to LambdaTest'
}
}
}
}
pipeline {
agent {
label ' linux-machine'
}
}
pipeline {
agent {
label ' linux-machine'
}
stages {
}
}
pipeline {
agent {
label ' linux-machine'
}
stages {
stage(' build step' ) {
}
}
}
pipeline {
agent any
stages {
stage(' build step' ) {
steps {
echo " Build stage is running"
}
}
}
}
Referring Env Variables from Jenkins configuration
pipeline {
agent any
stages {
stage(' Initialization' ) {
steps {
echo " ${ JAVA_INSTALLATION_PATH} "
}
}
}
}
Creating & Referring Environment variable in Jenkinsfile
pipeline {
agent any
environment {
DEPLOY_TO = ' production'
}
stages {
stage(' Initialization' ) {
steps {
echo " ${ DEPLOY_TO} "
}
}
}
}
Initialize Env variables using sh commands
pipeline {
agent any
stages {
stage(' Initialization' ) {
environment {
JOB_TIME = sh (returnStdout : true , script : " date '+%A %W %Y %X'" ). trim()
}
steps {
sh ' echo $JOB_TIME'
}
}
}
}
Loading credentials in declarative pipeline
pipeline {
agent any
environment {
MY_CRED = credentials(' MY_SECRET' )
}
stages {
stage(' Load Credentials' ) {
steps {
echo " Username is $MY_CRED_USR "
echo " Password is $MY_CRED_PSW "
}
}
}
}
pipeline {
agent any
stages {
stage(' build' ) {
when {
branch ' dev'
}
steps {
echo " Working on dev branch"
}
}
}
}
when block using Groovy expression
pipeline {
agent any
stages {
stage(' build' ) {
when {
expression {
return env. BRANCH_NAME == ' dev' ;
}
}
steps {
echo " Working on dev branch"
}
}
}
}
when block with environment variables
pipeline {
agent any
environment {
DEPLOY_TO = ' production'
}
stages {
stage(' Welcome Step' ) {
when {
environment name : ' DEPLOY_TO' , value : ' production'
}
steps {
echo ' Welcome to LambdaTest'
}
}
}
}
when block with multiple conditions & all conditions should be satisfied
pipeline {
agent any
environment {
DEPLOY_TO = ' production'
}
stages {
stage(' Welcome Step' ) {
when {
allOf {
branch ' master' ;
environment name : ' DEPLOY_TO' , value : ' production'
}
}
steps {
echo ' Welcome to LambdaTest'
}
}
}
}
when block with multiple conditions & any of the given conditions should be satisfied
pipeline {
agent any
environment {
DEPLOY_TO = ' production'
}
stages {
stage(' Welcome Step' ) {
when {
anyOf {
branch ' master' ;
branch ' staging'
}
}
steps {
echo ' Welcome to LambdaTest'
}
}
}
}
Loading maven using tools
pipeline {
agent any
tools {
maven ' MAVEN_PATH'
}
stages {
stage(' Load Tools' ) {
steps {
sh " mvn -version"
}
}
}
}
pipeline {
agent any
stages {
stage(' Parallel Stage' ) {
parallel {
stage(' windows script' ) {
agent {
label " windows"
}
steps {
echo " Running in windows agent"
bat ' echo %PATH%'
}
}
stage(' linux script' ) {
agent {
label " linux"
}
steps {
sh " Running in Linux agent"
}
}
}
}
}
}
post with multiple conditional blocks
pipeline {
agent any
stages {
stage(' build step' ) {
steps {
echo " Build stage is running"
}
}
}
post {
always {
echo " You can always see me"
}
success {
echo " I am running because the job ran successfully"
}
unstable {
echo " Gear up ! The build is unstable. Try fix it"
}
failure {
echo " OMG ! The build failed"
}
}
}
Marking build as UNSTABLE
Mark build as UNSTABLE based on Code Coverage
pipeline {
agent any
tools {
maven ' MAVEN_PATH'
jdk ' jdk8'
}
stages {
stage(" Tools initialization" ) {
steps {
sh " mvn --version"
sh " java -version"
}
}
stage(" Checkout Code" ) {
steps {
git branch : ' master' ,
url : " https://github.com/iamvickyav/spring-boot-data-H2-embedded.git"
}
}
stage(" Building Application" ) {
steps {
sh " mvn clean package"
}
}
stage(" Code coverage" ) {
steps {
jacoco(
execPattern : ' **/target/**.exec' ,
classPattern : ' **/target/classes' ,
sourcePattern : ' **/src' ,
inclusionPattern : ' com/iamvickyav/**' ,
changeBuildStatus : true ,
minimumInstructionCoverage : ' 30' ,
maximumInstructionCoverage : ' 80' )
}
}
}
}
pipeline {
agent any
triggers {
cron(' H/15 * * * *' )
}
stages {
stage(' Example' ) {
steps {
echo ' Hello World'
}
}
}
}