Understanding Jenkins Pipeline
Nitin Joshi
Automation Architect, Selenium, Playwright, Jenkins, Framework design, CI-CD, groovy, BDD, TestNG, Rest Assured, Karate, Java, Team Management
A Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. A pipeline consists of a series of stages and steps that automate the process of building, testing, and deploying code.
Creating a new Jenkins Pipeline
Add a new item and select item type as Pipeline.
Details in a Jenkins pipeline Job
Pipeline Syntax - Pipeline syntax is used to generate steps code for different types of step:
JenkinsFile
A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline, which is a set of automated steps used to build, test, and deploy software. Jenkins Pipelines are used in continuous integration and continuous delivery (CI/CD) processes.
Example of a basic Jenkins Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
领英推荐
Understanding different Keywords in Jenkins Pipeline
Basic flow in a pipeline are:
Understanding the details in a pipeline
Example of a detailed Pipeline
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '5'))
}
parameters {
string(name: 'SERVER', defaultValue: 'NODE1', description: 'Please provide server details')
choice(name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Deployment environment')
}
environment {
MAVEN_HOME = tool 'Maven 3.6.3'
PATH = "${MAVEN_HOME}/bin:${env.PATH}"
}
triggers {
cron('H/15 * * * *')
}
tools {
jdk 'JDK 11'
}
stages {
stage('Approval') {
steps {
input 'Do you want to proceed with the build?'
}
}
stage('Checkout') {
steps {
echo 'Checking out code...'
// git url: 'https://github.com/your-repository/project.git', branch: 'main'
}
}
stage('Build') {
when {
expression {
return params.ENVIRONMENT != 'prod'
}
}
steps {
echo 'Building the project...'
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh 'mvn test'
}
}
stage('Package') {
steps {
echo 'Packaging the project...'
sh 'mvn package'
}
}
stage('Deploy') {
parallel {
stage('Deploy to Dev') {
when {
expression {
return params.ENVIRONMENT == 'dev'
}
}
steps {
echo 'Deploying to development environment...'
// Deployment steps go here
}
}
stage('Deploy to QA') {
when {
expression {
return params.ENVIRONMENT == 'qa'
}
}
steps {
echo 'Deploying to QA environment...'
// Deployment steps go here
}
}
stage('Deploy to Prod') {
when {
expression {
return params.ENVIRONMENT == 'prod'
}
}
steps {
echo 'Deploying to production environment...'
// Deployment steps go here
}
}
}
}
}
post {
always {
echo 'Cleaning up...'
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}
Parallel Execution in pipeline
Parallel execution in Jenkins Pipeline can be managed using the parallel directive. This allows multiple stages or steps to run simultaneously, which can significantly reduce the overall pipeline execution time.
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
echo 'unit test'
}
}
stage('Functional Tests') {
steps {
echo 'functional test'
}
}
}
}