Jenkins CI/CD pipeline tutorial

Jenkins CI/CD Pipeline Tutorial: Build Your First Pipeline

Introduction

Jenkins is the CI/CD tool most DevOps engineers still meet in real companies, even in 2026. GitHub Actions may be the first pipeline a beginner builds — our GitHub Actions Tutorial covers that — but Jenkins runs the pipelines of banks, telecoms and large enterprises that have used it for a decade, and knowing it is still one of the most bankable skills on a DevOps CV. This tutorial teaches Jenkins from zero: what it is, how to install it, and how to write a Declarative Pipeline that builds, tests and deploys a real application.

Version note (26 Nov 2026): the current Jenkins LTS is 2.580.3, released 25 November 2026 (the previous LTS, 2.580.2, was released 28 October 2026). Use the LTS line, not the weekly line, for anything you intend to keep running. Jenkins 2.555.1 and later require Java 21 or Java 25 — Java 17 is no longer supported, and the old lts-jdk17 Docker image stopped at 2.541.3. If you follow an older tutorial that installs Java 17, it will not work on the current LTS. See the official Jenkins documentation for installation and Pipeline syntax. For the full release history, see the Jenkins LTS changelog.

What Jenkins Is, and When to Use It

Jenkins is an open-source automation server. You give it a job — “when code is pushed to this repository, build it, run the tests, and if they pass, deploy it” — and it runs that job on a schedule, on a trigger, or on demand. The job is defined as code, in a file called a Jenkinsfile, stored in the same repository as the application. That is the whole idea; everything else is detail.

Jenkins differs from GitHub Actions in one important way: it is self-hosted. You run the Jenkins controller yourself — on a server, a virtual machine or in a container — instead of renting runners from a platform. That is extra work, and it is also the reason large organisations choose it: the pipelines, the credentials and the build history stay inside their own network, and Jenkins can reach internal systems that a cloud runner cannot.

Use Jenkins when the team already runs it, when builds must stay on-premises, or when you need plugins for tools GitHub Actions does not cover well. Use GitHub Actions when the code already lives on GitHub and you want the shortest path from zero to a working pipeline. The concepts transfer: a pipeline is still stages, steps, agents and credentials, whatever the tool.

Installing Jenkins (the LTS, in Docker)

The fastest reliable way to learn Jenkins is the official Docker image. Install Docker first — our Docker Tutorial for Beginners walks through it — then run:

docker run -d --name jenkins \
  -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts-jdk21

Open http://localhost:8080. Jenkins asks for the initial admin password, which it prints in the container log:

docker logs jenkins

Copy the password, create your admin user, and install the suggested plugins when Jenkins offers them. That bundle includes the Pipeline plugin, Git, and the tools this tutorial needs. The -v jenkins_home:/var/jenkins_home flag matters: without a volume, every job, plugin and credential disappears when the container is deleted.

If you install Jenkins directly on a server instead, install Java 21 first, then add the Jenkins repository for your distribution and install the jenkins package from jenkins.io. Either way, keep Jenkins behind a reverse proxy with HTTPS before you expose it beyond your own machine — the setup wizard warns you about this for a reason.

Your First Pipeline: a Jenkinsfile That Tests Code

A Declarative Pipeline is written in Groovy-flavoured syntax, but you only need a small vocabulary: pipeline, agent, stages, stage, steps. Create a repository with a small Node.js application, then add a file called Jenkinsfile at the root:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Install') {
            steps {
                sh 'npm ci'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
    }

    post {
        always {
            echo "Pipeline finished with status: ${currentBuild.currentResult}"
        }
    }
}

In Jenkins, create a new item of type Pipeline, point it at your repository, and set the script path to Jenkinsfile. Click Build Now. Jenkins checks the code out, runs npm ci, runs the tests, and shows each stage green or red. Break a test deliberately and build again — a pipeline you have seen fail is one you can trust when it passes.

Three details worth understanding now. agent any means “run this on any available agent” — on a single-controller setup, that is the controller itself; in production you would use labelled agents. checkout scm checks out the same commit that triggered the build. And the post block runs after the stages whatever happened, which is where notifications and cleanup belong.

Adding Build and Deploy Stages

A test-only pipeline is half a pipeline. Extend the Jenkinsfile with a build stage and a deploy stage that only runs on the main branch:

pipeline {
    agent any

    environment {
        IMAGE = "my-app:${env.BUILD_NUMBER}"
    }

    stages {
        stage('Test') {
            steps {
                sh 'npm ci'
                sh 'npm test'
            }
        }
        stage('Build image') {
            steps {
                sh "docker build -t ${IMAGE} ."
            }
        }
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                sh "docker stop my-app || true"
                sh "docker rm my-app || true"
                sh "docker run -d --name my-app -p 3000:3000 ${IMAGE}"
            }
        }
    }
}

The when { branch 'main' } guard is the important idea: a pull-request build tests the code but never deploys it. The environment block defines variables once, so the image tag is the same in every stage. BUILD_NUMBER is a built-in Jenkins variable — tagging images with the build number means every deployment refers to an exact, immutable image, unlike the moving :latest tag.

Credentials, Secrets and Webhooks

Real pipelines need credentials: a registry password, a cloud key, an API token. Jenkins stores them under Manage Jenkins → Credentials, encrypted, and injects them into a build as environment variables. Never put a real credential in a Jenkinsfile — the file is in Git, and Git remembers everything:

stage('Push image') {
    steps {
        withCredentials([usernamePassword(
            credentialsId: 'docker-hub',
            usernameVariable: 'REG_USER',
            passwordVariable: 'REG_PASS'
        )]) {
            sh 'echo "$REG_PASS" | docker login -u "$REG_USER" --password-stdin'
            sh "docker push ${IMAGE}"
        }
    }
}

Jenkins never prints credential values in the build log — it masks them automatically. Finally, connect the repository to Jenkins with a webhook so every push triggers a build: in GitHub, add a webhook to http://your-jenkins/github-webhook/ with the push event, and enable “GitHub hook trigger for GITScm polling” on the job. Without the webhook you are polling, which is slower and noisier.

Jenkins vs GitHub Actions — Which to Learn First?

Both are worth knowing; the order depends on your goal. GitHub Actions is faster to start, needs no server, and is the default for new open-source and small-team projects — start there if you are learning CI/CD for the first time. Jenkins is the tool you will meet in large, older codebases, and it rewards deeper knowledge: shared libraries, agents, plugin management and pipeline-as-code at scale. Learn Actions first for speed, then Jenkins for employability — our GitHub Actions Tutorial and this guide use the same pipeline shape on purpose, so the second one is mostly new syntax, not new concepts.

Common Beginner Mistakes

  • Running everything on the controller. Use agents for builds; the controller should orchestrate, not compile.
  • One giant stage. Split checkout, test, build and deploy so a failure tells you exactly where it happened.
  • Secrets in the Jenkinsfile or in Git. Use the credentials store; rotate anything that was ever committed.
  • No when guard on deploy. Every branch build should not deploy to production.
  • Letting plugins drift. Pin plugin versions, update on a schedule, and test updates on a copy first — a broken plugin update is the most common cause of a Jenkins outage.
  • No backup of JENKINS_HOME. Jobs, credentials and configuration live there. Back it up before upgrades.

FAQ

Is Jenkins still relevant in 2026? Yes. It is no longer the only choice, but it remains the standard CI/CD server in large enterprises, and Jenkins skills still appear in DevOps job descriptions alongside GitHub Actions and GitLab CI.

Do I need to know Groovy to write a Jenkinsfile? No. Declarative Pipeline syntax is a small, fixed vocabulary — the examples in this tutorial cover most of what you will write day to day. Scripted Pipeline uses full Groovy, and you can learn it later if you need it.

How much does Jenkins cost? Jenkins itself is free and open source. You pay for the machine that runs it and for the agents — which is the trade-off against a hosted service that charges per minute.

Which Java version does Jenkins need? Java 21 or Java 25 for Jenkins 2.555.1 and later, including the current LTS 2.580.3. Java 17 does not work on these versions.

Conclusion

You now have a working Jenkins pipeline: code checked out, tests run, an image built and deployed only from the main branch, with credentials stored properly. The next steps are the ones that make a pipeline production-grade — a shared library for the stages every repository repeats, separate agents for builds, and a staging environment between test and production. Those are the same ideas in every CI/CD tool, which is why learning Jenkins properly makes the next tool easier, not harder. To plan what to learn next, follow our DevOps Engineer Roadmap.

—

Scroll to Top