Jenkins Pipeline
Jenkins Pipeline – Detailed Explanation for Interviews
✅ What is Jenkins Pipeline?
Jenkins Pipeline is a suite of plugins that allows users to define and automate the entire CI/CD process as code, stored in source control. The pipeline defines a series of steps, stages, and conditions that make up your build–test–deploy workflow.
It brings:
-
Pipeline as Code via
Jenkinsfile -
Error handling & retries
-
Parallel execution
-
Human approval input
-
Integration with Docker, Kubernetes, Git, etc.
๐งฑ Two Types of Jenkins Pipelines
1. Declarative Pipeline (Recommended for most users)
Introduced in Jenkins 2.x, it provides a structured and opinionated syntax that’s easier to write and read.
๐น Key Features
| Feature | Description |
|---|---|
pipeline {} | Starts the pipeline block |
agent | Where to run the pipeline |
stages | Sequence of build/test/deploy phases |
steps | Actual commands inside a stage |
| Built-in checks | Validates syntax and structure before execution |
| Easier to maintain | Great for teams and large organizations |
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'mvn clean install'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh 'mvn test'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
sh './deploy.sh'
}
}
}
}
2. Scripted Pipeline (More flexible, but complex)
Written in Groovy, this is the original pipeline syntax, offering programmatic control, custom logic, and dynamic behavior.
๐น Key Features
| Feature | Description |
|---|---|
| Fully Groovy-based | Can use loops, conditionals, try-catch |
| High flexibility | Good for advanced users or dynamic jobs |
| Steeper learning curve | Less readable and error-prone |
node {
stage('Build') {
echo 'Building...'
sh 'mvn clean install'
}
stage('Test') {
echo 'Testing...'
sh 'mvn test'
}
stage('Deploy') {
echo 'Deploying...'
sh './deploy.sh'
}
}
๐ Declarative vs Scripted: Comparison Table
| Feature | Declarative Pipeline | Scripted Pipeline |
|---|---|---|
| Syntax | Simple, structured | Groovy-based scripting |
| Learning Curve | Easy | Steep |
| Flexibility | Moderate | High |
| Error handling | Limited (post blocks) | Full (try-catch-finally) |
| Readability | High (team-friendly) | Lower (more complex logic) |
| Community Recommendation | ✅ Preferred | ⚠️ Use only for dynamic needs |
| Linting/Syntax Validation | Yes (built-in) | No (error appears at runtime) |
Advanced Features for Senior Roles
| Feature | Usage |
|---|---|
post blocks | For cleanup, success/failure notifications |
when conditions | Stage execution control based on conditions |
input | Manual approval during deployment |
parallel | Run test suites or jobs in parallel |
parameters | Pass dynamic inputs to jobs |
tools | Auto-install Maven, JDK, etc. |
environment | Define env variables per stage or globally |
| Shared Libraries | Reuse pipeline logic across multiple jobs |
pipeline {
agent any
parameters {
string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Git branch')
}
environment {
MAVEN_HOME = '/usr/local/maven'
}
stages {
stage('Checkout') {
steps {
git branch: "${params.BRANCH_NAME}", url: 'https://github.com/example/repo.git'
}
}
stage('Build') {
tools {
maven 'Maven 3.6.3'
}
steps {
sh 'mvn clean package'
}
}
stage('Test') {
parallel {
stage('Unit Tests') {
steps { sh 'mvn test' }
}
stage('Integration Tests') {
steps { sh 'mvn verify -P integration' }
}
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
input message: "Approve deployment to production?"
sh './deploy.sh'
}
}
}
post {
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed.'
}
}
}
Interview Pro Tips
-
๐ฌ Talk about real use-cases: “We used
parallelto split long-running tests and reduce pipeline time by 60%.” -
๐งช Mention testing: “Used
Jenkinsfilelinting andpipeline-model-definitionplugin to validate before execution.” -
๐ก Security & maintainability: “Moved logic into shared libraries to reduce duplication and apply RBAC on credentials.”
Summary
| Jenkins Pipeline Key Points |
|---|
Supports Pipeline-as-Code via Jenkinsfile |
| Declarative = easy, structured (preferred) |
| Scripted = powerful, dynamic (advanced users) |
| Enables CI/CD best practices: testing, deployment, approvals |
| Use shared libraries, parameters, and parallelism to scale |
No comments :
Post a Comment