Pipelines are made up of multiple steps that allow you to build, test and
deploy applications. Jenkins Pipeline allows you to compose multiple steps in
an easy way that can help you model any sort of automation process.
Think of a "step" like a single command which performs a single action. When a
step succeeds it moves onto the next step. When a step fails to execute
correctly the Pipeline will fail.
When all the steps in the Pipeline have successfully completed, the Pipeline is
considered to have successfully executed.
Linux, BSD, and Mac OS
On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute
a shell command in a Pipeline.
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo "Hello World"'
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
}
}
There are some powerful steps that "wrap" other steps which can easily solve
problems like retrying (retry) steps until successful or exiting if a
step takes too long (timeout).
When a step cannot be completed, timeouts help the controller avoid wasting resources.
This video reviews the process of setting up both stage and global timeouts.
How do I set a timeout in a Jenkins Pipeline
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
}
}
node {
stage('Deploy') {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
The "Deploy" stage retries the flakey-deploy.sh script 3 times, and then
waits for up to 3 minutes for the health-check.sh script to execute. If the
health check script does not complete in 3 minutes, the Pipeline will be marked
as having failed in the "Deploy" stage.
"Wrapper" steps such as timeout and retry may contain other steps,
including timeout or retry.
How do I retry a Jenkins Job?
We can compose these steps together. For example, if we wanted to retry our
deployment 5 times, but never want to spend more than 3 minutes in total before
failing the stage:
When the Pipeline has finished executing, you may need to run clean-up steps or
perform some actions based on the outcome of the Pipeline. These actions can be
performed in the post section.
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
echo 'This will run only if failed'
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
node {
try {
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
echo 'This will run only if successful'
} catch (e) {
echo 'This will run only if failed'// Since we're catching the exception in order to report on it,// we need to re-throw it, to ensure that the build is marked as failedthrow e
} finally {
def currentResult = currentBuild.result ?: 'SUCCESS'if (currentResult == 'UNSTABLE') {
echo 'This will run only if the run was marked as unstable'
}
def previousResult = currentBuild.previousBuild?.result
if (previousResult != null && previousResult != currentResult) {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
echo 'This will always run'
}
}