-
Notifications
You must be signed in to change notification settings - Fork 248
Running multiple steps
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 as like a single command that does 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 successfully executed.
The sh
step is used to execute a shell command.
pipeline {
agent { label 'master' }
stages {
stage('build') {
steps {
sh 'echo step1'
sh 'echo step2'
sh '''
echo 'Multiline'
echo 'Example'
'''
echo 'not using shell'
}
}
}
}
The bat
step is used to execute a batch command.
pipeline {
agent { label 'master' }
stages {
stage('build') {
steps {
bat 'echo step1'
bat 'echo step2'
bat '''
echo 'Multiline'
echo 'Example'
'''
echo 'not using shell'
}
}
}
}
There are some powerful steps that "wrap" other steps that can easily solve problems like retrying (retry
) steps until successful or bailing out if a step takes too long (timeout
).
pipeline {
agent { label 'master' }
stages {
stage('deploy') {
steps {
retry(3) {
sh 'echo deploying...'
}
timeout(time: 3, unit: 'MINUTES') {
sh 'echo checking health...'
}
}
}
}
}
The deploy
stage retries a script 3 times, and then waits for up to 3 minutes for the "health check" script to run. If it did not complete in 3 minutes, the pipeline will have failed at the "deploy" stage.
Steps like timeout
and retry
contain other steps and you can have any steps run within them as you like - even timeout
and retry
.
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 before failing the pipeline stage:
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh 'echo deploying'
sh 'echo verifying...'
}
}
When the build has run, you may need to run steps to clean up, or take some action based on the outcome. This is where the post
section comes in handy:
pipeline {
agent { label 'master' }
stages {
stage('test') {
steps {
sh 'fail me please'
}
}
}
post {
always {
sh 'This will always run'
}
success {
sh 'This will run only if successful'
}
failure {
sh 'This will run only if failed'
}
unstable {
sh 'This will run only if the run was marked as unstable'
}
changed {
sh 'This will run only if the state of the Pipeline has changed')
sh 'For example, the Pipeline was previously failing but is now successful'
}
}
}
You can also run cleanup steps per stage as well if you like:
pipeline {
agent { label 'master' }
stages {
stage('test') {
post {
always {
sh 'This will always run'
}
failure {
sh 'This will run only if failed'
}
changed {
sh 'This will run only if the state of the Pipeline has changed')
}
}
steps {
sh 'fail me please'
}
}
}
}
Documentation
- Getting Started
- Running multiple steps
- Controlling your build environment
- Environment variables
- Reporting test results and storing artifacts
- Notifications
- Deployment and credentials
- Parallelism
- Triggering runs
- Parametrized pipelines
- Pipeline options and log rotation
- Jenkinsfile validation from the CLI
- Advanced pipeline authoring
- Syntax reference
- Version history and changes
Examples