Jenkins Pipeline Implementation Guide
Pipeline Execution Models
Jenkins supports several execution approaches:
- Freestyle projects for beginners with UI-driven configuration
- Pipeline-as-code for advanced users via Jenkinsfile definitions
Pipeline extends Jenkins with pwoerful capabilities for continuous delivery pipelines defined in code, typically stored in source control.
Pipeline Fundamentals
Requirements
- Jenkins 2.x or later
- Pipeline plugin installed
Core Concepts
- Pipeline: User-defined CD workflow model
- Node: Execution environment (controller/agent)
- Stage: Logical division of build process
- Step: Individual task within a stage
Syntax Approaches
Two pipeline syntax variants exist:
- Declarative: Structured, predefined format
- Scripted: Flexible Groovy-based syntax
// Declarative syntax
pipelineDefinition {
executor any
phases {
phase('Compile') {
actions {
// Build steps
}
}
}
}
// Scripted syntax
executionNode {
phase('Compile') {
// Build steps
}
}
Pipeline Structure
pipelineDefinition {
executor any
phases {
phase('Greeting') {
actions {
output 'Hello World'
}
}
}
}
Key Sections
- Executor: Defines execution environment
- Environment: Sets global or stage-specific variables
- Parameters: Configures build parameters
- Script: Embeds Groovy code
Conditional Execution
phase('Deploy') {
when {
branch 'main'
beforeExecutor true
}
actions {
output 'Deploying application'
}
}
Parallel Execution
phase('Parallel Tasks') {
parallelExecution {
phase('Task A') {
executor { label 'runner-a' }
actions { output 'Task A running' }
}
phase('Task B') {
executor { label 'runner-b' }
actions { output 'Task B running' }
}
}
}
Essential Steps
executeShell: Runs shell commandsoutput: Prints messages to consolearchiveOutputs: Stores build artifactscurrentExecution: Accesses build information
Jenkinsfile Implementation
Jenkinsfile defines pipelines using domain-specific language (DSL). Best practices:
- Store in source control with application code
- Maintain separate from UI configuration
Pipeline Mechanics
Jenkins converts pipelines to Groovy scripts using the Pipeline plugin.
Project Implementation Example
pipelineDefinition {
executor any
parameters {
gitBranchSelector(
credentialsId: 'git-credentials',
name: 'SELECTED_BRANCH',
remoteURL: 'http://repo.example.com/project.git'
)
choice(
choices: ['development', 'production'],
name: 'TARGET_ENV'
)
}
phases {
phase('Fetch Code') {
actions {
sourceControl scmGit(
branches: [[name: '${SELECTED_BRANCH}']],
userRemoteConfigs: [[
credentialsId: 'git-credentials',
url: 'http://repo.example.com/project.git'
]]
)
}
}
phase('Build') {
actions {
executeShell 'mvn clean package'
if (fileExists("target/${JOB_BASE_NAME}.jar")) {
archiveOutputs "target/${JOB_BASE_NAME}.jar"
} else {
error 'Build artifact missing'
}
}
}
phase('Deploy') {
actions {
executeShell '''
// Deployment script content
'''
sshPublisher(publishers: [
sshTransfer(
remoteDirectory: '/deploy/path',
sourceFiles: 'target/${JOB_BASE_NAME}.jar'
)
])
}
}
}
post {
always {
notificationBot (
robot: "ci-alerts",
title: "${JOB_BASE_NAME} Build Result",
text: "Build ${currentExecution.result} for ${JOB_BASE_NAME}"
)
}
}
}