Some snippets:
steps:
------pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo "Hello World"'
sh '''
echo "Multiline shell steps"
ls –lah
'''
}
}
}
}
timeouts:
----------pipeline {
agent any
stages {
stage('Deploy') {
steps {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
powershell '.\flakey-deploy.ps1'
}
}
}
}
}
}
stages:
-------pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building'
}
}
stage('Test') {
steps {
echo 'Testing'
}
}
stage('Deploy') {
steps {
echo 'Deploying'
}
}
}
}
when
-----pipeline {
agent any
stages {
stage('Build') {
when {
expression {
"foo" == "bar"
}
}
steps {
echo 'Building'
}
}
stage('Test') {
when {
environment name: 'JOB_NAME', value: 'foo'
}
steps {
echo 'Testing'
}
}
stage('Deploy') {
when {
branch 'master'
}
steps {
echo 'Deploying'
}
}
}
}
when allOf:
----------pipeline {
agent any
stages {
stage('Build') {
when {
allOf {
not { branch 'master' }
environment name: 'JOB_NAME', value: 'Foo'
}
}
steps {
echo 'Building'
}
}
}
}
parallel stages:
------------------pipeline {
agent any
stages {
stage('Browser Tests') {
parallel {
stage('Chrome') {
steps {
echo "Chrome Tests"
}
}
stage('Firefox') {
steps {
echo "Firefox Tests"
}
}
}
}
}
}
Docker
--------pipeline {
agent {
docker {
label 'docker'
image 'maven:3.5.0-jdk-8'
}
}
}
One of the advantages of using containers is creating an immutable environment that defines only the tools required in a consistent manner.
Rather than creating one large image with every tool needed by the Pipeline, it is possible to use different containers in each stage and
reuse the workspace, keeping all of your files in one place.
pipeline {
agent {
node { label 'my-docker' }
}
stages {
stage("Build") {
agent {
docker {
reuseNode true
image 'maven:3.5.0-jdk-8'
}
}
steps {
sh 'mvn install'
}
}
}
}
Envs:
-----pipeline {
agent any
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
stages {
stage('Build') {
steps {
sh 'printenv’
}
}
}
}
Credentials envs:
------------------environment {
AWS_ACCESS = credentials('AWS_ KEY')
AWS_SECRET = credentials('AWS_SECRET')
}
post-actions:
------------pipeline {
agent any
stages {
stage('No-op') {
steps {
sh 'ls'
}
}
}
post {
always {
echo 'I have finished'
deleteDir() // clean up workspace
}
success {
echo 'I succeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things are different...'
}
}
}
Notify post actions:
-----------------post {
failure {
mail to: 'team@example.com',
subject: 'Failed Pipeline',
body: "Something is wrong"
}
}
slack:
------post {
success {
slackSend channel:'#ops-room',
color: 'good',
message: 'Completed successfully.'
}
}