In this guide we are working only with Scripted Pipelines, not Declarative Pipelines.
Nodes
node {
}
This is used to schedule the steps held within on the next available executor, it is possible to customise this to specify an executor (such as a windows one).
Properties
properties([
[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5']],
parameters([
choice(choices: ['ode-1','ode-2','uat', 'stg', 'prd-1', 'prd-2'], description: 'Environment', name: 'envName'),
string(defaultValue: "master", description: 'What costa-web branch? - Ignore origin/', name: 'webBranch')
])
])
Build Description
currentBuild.description = "Set some text here e.g. branch name etc."
Stage
stage('do some stuff') {
}
This wraps the steps within a named stage e.g.:
Pull SCM
stage('Pull') {
checkout scm
}
Clean up workspace
stage('Clear Workspace') {
step([$class: 'WsCleanup'])
}
Try/Catch & Build Status Notifications
node {
try {
stage ('build') {
sh './configure && make'
}
} catch (e) {
// If there was an exception thrown, the build failed
currentBuild.result = "Jenkins Job Failed"
//use slack plugin (https://github.com/jenkinsci/slack-plugin)
slackSend color: '#FF0000', message: 'This broke'
// Microsoft Teams
office365ConnectorSend message: msg, status: buildStatus, webhookUrl: "${web_hook_url}", notifyBackToNormal: true, notifyFailure: true, notifyRepeatedFailure: true, notifySuccess: false, notifyUnstable: true, startNotification: false
// Or CURL
sh 'curl https://webhook.slack.com ...'
throw e
} finally {
// stuff that should happen no matter what
}
}
Using Credentials
stage('do something with credentials') {
withCredentials([string(credentialsId: 'some-webook-url', variable: 'web_hook_url')]) {
sh "curl $web_hook_url"
}
}
Building containers
stage('Build Container') {
container = docker.build("${image_name}", ".")
}
Using a docker regsitry
stage('Push Image to AWS ECR') {
docker.withRegistry('https://012345abcdef.dkr.ecr.eu-west-1.amazonaws.com') {
sh "eval \$(aws ecr get-login --no-include-email)"
docker.image("${image_name}").push()
}
}
Here we assume that there is an IAM profile attached to the Jenkins instance and that it has the appropriate properties set or that there is a profile using keys available to the Jenkins user.
Building inside containers
stage('build the app') {
docker.images('node:8.10-alpine').inside('-u root') {
// Do the same steps but inside the container
sh 'whoami'
}
}
- Example of setting the user (don’t set to root)
- This mounts the current workspace inside the container
- If you were to modify files as root inside the container this may mean that jenkins outside the container doesn’t have the right permissions to modify the files in the workspace.
Bin/Bash
stage('some bash thing') {
sh "/bin/bash myBashScript.sh"
}
You can specify the shell to be used (this isn’t usually necessary but may help in certain scenarios).
Get bash output
GIT_SHA = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
Trigger another job
build job: 'deploy-application', parameters: [string(name: 'AWS_ENV', value: "dev")], wait: false
Disable concurrent builds
node {
properties([disableConcurrentBuilds()])
}
Schedule Build via Cron
node {
properties([pipelineTriggers([cron("*/10 * * * *")])])
}
Colour Terminal Output
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: "xterm"]) {
sh 'jest --config jest.integration.config.js --colors'
}