JENKINS DECLARATIVE PIPELINE EXAMPLE

Jenkins released the 2nd version of their Pipeline As a Code project appropriately called “Declarative Pipeline” with whole new syntax rules and not to be confused with the previous version “Scripted Pipeline”. Long story short, it makes things simpler and a bit more structured than its predecessor. Unlike the 1st version(Scripted Pipeline) it forces us to follow the structure of Pipeline -> Agent ->Stages -> Stage -> Steps -> Post as seen below.

pipeline {
agent {
node {
label 'master'
}
}
stages {
stage('build') {
steps {
sh 'mvn --version'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
echo 'This will run only if failed'
}
}

Parameters and environment variables are standardized to be defined in their respective blocks.

pipeline {
agent any
parameters {
string(name: 'RELEASE_VERSION', defaultValue: '1.0.0', description: 'Application git release tag version')
}
environment {
ENV_STACK = 'staging'
}
stages {
stage('Example') {
steps {
echo "Deploying ${params.RELEASE_VERSION} in ${env.ENV_STACK}"
}
}
}
}

Combining the two examples will quickly get you started in your Pipeline as a code journey. As any other tool out there it has its downside. There is still a limited amount of plugins supported(They now call it Steps) and definitely not as flexible and versatile as the “Scripted pipeline”. To be fair, it was just launched February 2017 thus still quite new and a lot more room to grow and improve.

Their documentation is well written and newbie friendly as long as you know what Jenkins is for. This is just a short introduction to share my personal thoughts on this new pipeline. Hope this is helpful to all people that are finding their way in their Pipeline as a Code journey.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade