Boost Your Continuous Integration with Jenkins Pipelines: A Comprehensive Guide to Different Types with Practical Examples
- Simple pipeline:
This is the most basic type of pipeline, which consists of a single stage.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
}
}
- Parallel pipeline:
This type of pipeline executes multiple stages in parallel.
pipeline {
agent any
stages {
stage('Build and Test') {
parallel {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Unit Test') {
steps {
sh 'mvn test'
}
}
stage('Integration Test') {
steps {
sh 'mvn integration-test'
}
}
}
}
}
}
- Conditional pipeline:
This type of pipeline executes different stages based on a condition.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Deploy') {
when {
branch 'master'
}
steps {
sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
}
}
}
}
- Declarative pipeline:
This type of pipeline uses a declarative syntax, which makes it easier to read and write.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
}
}
}
post {
always {
sh 'rm -rf target'
}
success {
slackSend channel: '#notifications', message: 'Build successful!'
}
failure {
slackSend channel: '#notifications', message: 'Build failed!'
}
}
}
- Scripted pipeline:
This type of pipeline uses a scripted syntax, which allows for more flexibility and customization.
node {
stage('Build') {
sh 'mvn clean install'
}
stage('Test') {
sh 'mvn test'
}
stage('Deploy') {
sshagent(['my-ssh-key']) {
sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
}
}
post {
always {
sh 'rm -rf target'
}
success {
slackSend channel: '#notifications', message: 'Build successful!'
}
failure {
slackSend channel: '#notifications', message: 'Build failed!'
}
}
}