Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Jenkins Pipeline Implementation Guide

Tech May 17 3

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 commands
  • output: Prints messages to console
  • archiveOutputs: Stores build artifacts
  • currentExecution: 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}"
            )
        }
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.