Operations 18 min read

Master Jenkins CI/CD: Install, Configure, and Write Declarative Pipelines

This guide introduces Jenkins as an open-source CI tool, explains continuous integration and delivery concepts, walks through Docker-based installation, and provides detailed examples of both scripted and declarative pipelines, including common directives, parameters, triggers, and best practices for effective DevOps automation.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Jenkins CI/CD: Install, Configure, and Write Declarative Pipelines

DevOps - Jenkins

Jenkins Overview

Jenkins is an open-source Java-based continuous integration tool that monitors repetitive tasks and provides an easy-to-use platform for continuous integration of software projects.

What is Continuous Integration (CI)?

CI (Continuous Integration) is a software development practice where developers trigger build, compile, and unit-test steps immediately after committing new code, repeating this process for every commit to improve efficiency and avoid integration problems.

What is Continuous Delivery (CD)?

CD (Continuous Delivery) builds on CI by automatically deploying the integrated code to an environment that closely resembles production, so after code review and packaging, it is continuously delivered to a real runtime environment.

Installing Jenkins

Install Jenkins using Docker. First install Docker, then grant ownership of the directory that will store Jenkins data:

# groupadd jenkins -g 1000 && useradd jenkins -u 1000 -g jenkins
# chown -R 1000:1000 /jenkins
uid and gid must be 1000 to match the Jenkins image.

Start the container:

# docker network create jenkins

# docker run \
  --name jenkins-docker \
  --detach \
  --privileged \
  --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume /jenkins/certs:/certs/client \
  --volume /jenkins/data:/var/jenkins_home \
  --publish 8080:8080 \
  --publish 50000:50000 \
  jenkins/jenkins:latest

## Get the initial admin password
# docker exec -it jenkins-docker cat /var/jenkins_home/secrets/initialAdminPassword

Then access Jenkins at http://<server_ip>:8080.

Pipeline Overview

To use Jenkins effectively, you need to master its pipeline feature.

Pipeline Basics

Default file name is Jenkinsfile Uses Groovy syntax

Can perform code integration, compilation, quality checks, and deployment – essentially a scripted workflow.

Pipeline Script Types

Scripted syntax

node {
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

Declarative syntax

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'

                script {
                    def browsers = ['chrome', 'firefox']
                    for (int i = 0; i < browsers.size(); ++i) {
                        echo "Testing the ${browsers[i]} browser"
                    }
                }
            }
        }
    }
}

It is recommended to write Jenkinsfiles using the declarative syntax because it offers better readability and flexibility while still allowing scripted blocks.

How to Get Started with Jenkinsfile

Study official Pipeline Syntax documentation and examine example Jenkinsfiles.

Learn to use the built-in snippet generator.

Common Declarative Directives

pipeline – top-level block that encloses the entire pipeline.

agent – defines where the pipeline or a stage runs. Options: any, none, label, node (with optional customWorkspace).

stages – a sequence of stage blocks.

stage – a named logical step within stages.

steps – the actual commands executed inside a stage, such as sh or echo.

post – actions that run after the pipeline or a stage finishes (e.g., always, success, failure, etc.).

environment – defines global or stage-specific environment variables; can reference credentials via credentials().

options – pipeline-level options like buildDiscarder, timeout, timestamps, disableConcurrentBuilds, etc.

parameters – input parameters for the pipeline (string, text, boolean, choice, password).

triggers – defines automatic triggers such as cron or pollSCM.

tools – declares tools (Maven, JDK, Gradle) to be available in the pipeline.

input – pauses a stage and asks for user input.

when – conditional execution of a stage based on branch, environment, expression, etc.

parallel – runs multiple nested stages concurrently; can be combined with failFast.

script – allows arbitrary Groovy code inside a declarative pipeline.

Scripted Pipeline

Scripted pipelines are written in pure Groovy DSL and support full language features, including if/else and try/catch/finally for flow control.

node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
}
node {
    stage('Example') {
        try {
            sh 'exit 1'
        } catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

These examples illustrate how to build, test, and deploy applications using Jenkins pipelines.

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/cdDevOpsPipelineJenkins
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.