Operations 8 min read

Understanding Jenkins Pipeline: Concepts, Syntax, and Practical Code Examples

This article introduces Jenkins Pipeline fundamentals, explains stages, nodes, and steps, compares Declarative and Scripted syntaxes, and provides detailed code examples for Windows, Docker, and Git operations to illustrate building, testing, and deploying applications.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Understanding Jenkins Pipeline: Concepts, Syntax, and Practical Code Examples

Premise Introduction

Jenkins' core strength lies in its Pipeline technology, which enables automation of builds by encapsulating configuration steps into scripts, linking tasks across master and slave nodes, and visualizing complex release processes.

Basic Concepts

Stage: Logical grouping of operations that can span multiple nodes.

Node: A Jenkins execution environment, either a master or a slave.

Step: The smallest unit of work, ranging from directory creation to Docker image building, provided by plugins.

Pipeline Configuration

Create a New "Pipeline" Job

Configure Pipeline Script

Enable Poll SCM

Pipeline Syntax

Pipeline supports two syntaxes:

Declarative Pipeline – a structured, newer format introduced in Pipeline 2.5.

Scripted Pipeline – a Groovy‑based flexible format.

Common Points

Both syntaxes persist pipeline code, can use built‑in or plugin‑provided steps, and support shared libraries for extension.

Differences

Declarative enforces stricter syntax and a fixed structure, making it easier for most users.

Scripted offers greater flexibility, allowing complex custom logic via Groovy.

Examples of Both Syntaxes

Declarative Pipeline (Windows Environment)

pipeline {
    agent any  // Run on any available node
    stages {
        stage('Prepare') {
            steps {
                // Clean publish directory
                bat ''if exist D:\publish\LoginServiceCore (rd /s /q D:\publish\LoginServiceCore)
                if exist C:\Users\Administrator\.nuget (rd /s /q C:\Users\Administrator\.nuget) && exit''
            }
        }
        // Checkout code
        stage('Checkout') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: '*/master']],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', url: 'http://xxx/xxx/xxx']]
                ])
            }
        }
        // Build
        stage('Build') {
            steps {
                bat ''cd "D:\Program Files (x86)\Jenkins\workspace\LoginServiceCore\LoginApi.Hosting.Web"
                dotnet restore
                dotnet build
                dotnet publish --configuration Release --output D:\publish\LoginServiceCore''
            }
        }
        // Deploy
        stage('Deploy') {
            steps {
                bat ''cd D:\PipelineScript\LoginServiceCore
                python LoginServiceCore.py''
            }
        }
        // Automated Test (Python)
        stage('Test') {
            steps {
                bat ''cd D:\PipelineScript\LoginServiceCore
                python LoginServiceCoreApitest.py''
            }
        }
    }
}

Scripted Pipeline

node('master') { // Run on master node
    stage 'Prepare' // Clean publish directory
    bat ''if exist D:\publish\LoginServiceCore (rd /s /q D:\publish\LoginServiceCore)
    if exist C:\Users\Administrator\.nuget (rd /s /q C:\Users\Administrator\.nuget) && exit''

    // Checkout code
    stage 'Checkout'
    checkout([
        $class: 'GitSCM',
        branches: [[name: '*/master']],
        doGenerateSubmoduleConfigurations: false,
        extensions: [],
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d', url: 'http://xxx/xxx/xxx']]
    ])

    // Build
    stage 'Build'
    bat ''cd "D:\Program Files (x86)\Jenkins\workspace\LoginServiceCore\LoginApi.Hosting.Web"
    dotnet restore
    dotnet build
    dotnet publish --configuration Release --output D:\publish\LoginServiceCore''

    // Deploy
    stage 'Deploy'
    bat ''cd D:\PipelineScript\LoginServiceCore
    python LoginServiceCore.py''

    // Automated Test (Python)
    stage 'Test'
    bat ''cd D:\PipelineScript\LoginServiceCore
    python LoginServiceCoreApitest.py''
}

Pipeline Docker Script Example

node {
    // Code checkout
    stage('get Code') {
        git credentialsId: 'git-credentials-id', url: 'http://192.168.19.250/libo/test.git'
    }

    // Unit testing inside Docker image
    stage('unit testing') {
        docker.image('golang:1.7').inside {
            sh './script/unittest.sh'
        }
    }

    // Build inside Docker image
    stage('Build') {
        def confFilePath = 'conf/app.conf'
        def config = readFile confFilePath
        writeFile file: confFilePath, text: config
        docker.image('golang:1.7').inside {
            sh './script/build.sh'
        }
    }

    // Build image and push to registry
    def imagesName = '192.168.18.250:5002/ufleet/uflow:v0.9.1.${BUILD_NUMBER}'
    stage('Image Build And Push') {
        docker.withRegistry('http://192.168.18.250:5002', 'registry-credentials-id') {
            docker.build(imagesName).push()
        }
    }

    // Deploy the newly built container
    stage('deploy iamegs') {
        try {
            sh 'docker rm -f cicdDemo'
        } catch (e) {
            // ignore errors
        }
        docker.image(imagesName).run('-p 9091:80 --name cicdDemo')
    }
}

Git Credential Handling

withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
    sh '''
        printf "machine github.com
login $GIT_USERNAME
 password $GIT_PASSWORD" >> ~/.netrc
        // continue script as necessary working with git repo...
    '''
}

Git Checkout Example

checkout scm: ([
    $class: 'GitSCM',
    userRemoteConfigs: [[credentialsId: '******', url: ${project_url}]],
    branches: [[name: 'refs/tags/${project_tag}']]
])
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdGitPipelineGroovyJenkins
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.