Jenkins Tutorial: Installation, Declarative Pipelines, Agents, Credentials and Shared Libraries

DevOps Certification 26 min min read Updated: Nov 17, 2026 Intermediate
Jenkins Tutorial: Installation, Declarative Pipelines, Agents, Credentials and Shared Libraries
Intermediate Topic 2 of 4

What Is Jenkins?

Jenkins is a free, open-source automation server. It runs jobs that build, test and deploy software. It is self-hosted, very flexible and has thousands of plugins. You will meet it in many companies, so it is an important DevOps skill.

Version note: Jenkins has weekly releases and LTS (long-term support) releases. Use the current LTS in production, which currently requires Java 17 or 21. Check jenkins.io for the latest LTS and its Java requirement.

Architecture

  • Controller (formerly master): the web UI, scheduling and configuration. Do not run builds on it
  • Agents (nodes): machines, containers or Pods that run the builds
  • Executors: slots on an agent for parallel builds
  • Jobs/Pipelines: work definitions. Modern Jenkins uses Pipeline jobs defined in a Jenkinsfile

Install Jenkins with Docker

bash docker run -d --name jenkins \ -p 8080:8080 -p 50000:50000 \ -v jenkins_home:/var/jenkins_home \ jenkins/jenkins:lts-jdk21 docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword # open http://localhost:8080, paste the password, choose "Install suggested plugins", create an admin user

Useful plugins: Pipeline, Git, GitHub Branch Source, Credentials Binding, Docker Pipeline, Kubernetes, Blue Ocean (optional), Pipeline Utility Steps, Warnings Next Generation, Configuration as Code (JCasC).

Your First Pipeline

Create a repository with a file named Jenkinsfile. In Jenkins: New Item, Pipeline (or Multibranch Pipeline), point to the repository.

groovy pipeline { agent any stages { stage('Checkout') { steps { checkout scm } } stage('Build') { steps { sh 'echo Building... && mkdir -p out && date > out/build.txt' } } stage('Test') { steps { sh 'echo Running tests... && exit 0' } } } post { success { echo 'Pipeline succeeded' } failure { echo 'Pipeline failed' } always { archiveArtifacts artifacts: 'out/**', allowEmptyArchive: true } } }

Declarative Pipeline Syntax Reference

BlockPurpose
pipeline { }Root of a declarative pipeline
agentWhere to run: any, none, label 'linux', docker { image '...' }, kubernetes { ... }
stages / stageNamed steps of the process
stepsCommands such as sh, bat, echo, junit
environmentEnvironment variables and credentials
parametersUser inputs when starting a build
optionsTimeout, retry, build discarder, timestamps
triggersPoll SCM, cron, upstream
whenRun a stage only on a condition (branch, tag, expression)
postActions after the run: always, success, failure, unstable, cleanup

A Realistic CI/CD Pipeline (Docker and Kubernetes)

groovy pipeline { agent any options { timeout(time: 30, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '20')) timestamps() disableConcurrentBuilds() } environment { REGISTRY = 'ghcr.io/myorg' IMAGE = "${REGISTRY}/shop-api" TAG = "${env.GIT_COMMIT.take(7)}" } parameters { booleanParam(name: 'DEPLOY', defaultValue: true, description: 'Deploy after build?') } stages { stage('Test') { agent { docker { image 'python:3.13-slim' } } steps { sh 'pip install -r requirements.txt' sh 'pytest --junitxml=report.xml' } post { always { junit 'report.xml' } } } stage('Scan and Build') { parallel { stage('Dependency scan') { steps { sh 'trivy fs --severity HIGH,CRITICAL --exit-code 1 .' } } stage('Build image') { steps { sh 'docker build -t $IMAGE:$TAG .' } } } } stage('Push') { steps { withCredentials([usernamePassword(credentialsId: 'ghcr-creds', usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) { sh 'echo $REG_PASS | docker login ghcr.io -u $REG_USER --password-stdin' sh 'docker push $IMAGE:$TAG' } } } stage('Deploy to staging') { when { allOf { branch 'main'; expression { params.DEPLOY } } } steps { withCredentials([file(credentialsId: 'kubeconfig-staging', variable: 'KUBECONFIG')]) { sh 'kubectl set image deployment/shop-api api=$IMAGE:$TAG -n staging' sh 'kubectl rollout status deployment/shop-api -n staging --timeout=120s' } } } stage('Approve production') { when { branch 'main' } steps { input message: 'Deploy to production?', ok: 'Deploy' } } stage('Deploy to production') { when { branch 'main' } steps { withCredentials([file(credentialsId: 'kubeconfig-prod', variable: 'KUBECONFIG')]) { sh 'kubectl set image deployment/shop-api api=$IMAGE:$TAG -n prod' sh 'kubectl rollout status deployment/shop-api -n prod --timeout=180s' } } } } post { failure { echo "Build failed: ${env.BUILD_URL}" } cleanup { cleanWs() } } }
Groovy quoting: Single quotes '...' do not interpolate variables in Groovy, double quotes "..." do. For secrets, use single-quoted sh strings (as above) so the shell expands them and Groovy never prints them.

Credentials

Store secrets in Manage Jenkins, Credentials. Types: username and password, secret text, SSH private key, secret file, certificate. Use them only through withCredentials or the credentials() helper, which masks values in logs.

groovy environment { AWS_CREDS = credentials('aws-jenkins') // sets AWS_CREDS_USR and AWS_CREDS_PSW }

Triggers: Webhooks and Multibranch

  • GitHub webhook: in the repo add webhook http://JENKINS_URL/github-webhook/ (push events). Faster than polling
  • Multibranch Pipeline: Jenkins scans a repository and creates a pipeline for every branch and pull request that contains a Jenkinsfile
  • Cron: triggers { cron('H 2 * * *') }
  • The H symbol spreads load by hashing the job name

Agents: Docker and Kubernetes

groovy // Build inside a container: clean, reproducible agent { docker { image 'maven:3-eclipse-temurin-21'; args '-v $HOME/.m2:/root/.m2' } } // Dynamic Pod per build using the Kubernetes plugin agent { kubernetes { yaml """ apiVersion: v1 kind: Pod spec: containers: - name: maven image: maven:3-eclipse-temurin-21 command: ['sleep'] args: ['infinity'] """ defaultContainer 'maven' } }
Best practice: Set controller executors to 0 and run builds on ephemeral agents (containers or Pods). It is safer and scales cleanly.

Shared Libraries

A shared library is a Git repository of reusable Groovy functions so that many pipelines share the same logic.

groovy // vars/deployApp.groovy (in the shared library repo) def call(Map cfg) { sh "kubectl set image deployment/${cfg.name} ${cfg.container}=${cfg.image} -n ${cfg.namespace}" sh "kubectl rollout status deployment/${cfg.name} -n ${cfg.namespace} --timeout=120s" } // Jenkinsfile @Library('company-lib@main') _ pipeline { agent any stages { stage('Deploy') { steps { deployApp(name: 'shop-api', container: 'api', image: env.IMAGE, namespace: 'prod') } } } }

Security and Operations

  • Use a reverse proxy with HTTPS, enable security (no anonymous admin), and use SSO or role-based strategy
  • Keep Jenkins and plugins updated, and remove unused plugins (frequent source of vulnerabilities)
  • Define configuration as code with the JCasC plugin and keep it in Git
  • Back up JENKINS_HOME (or use volume snapshots)
  • Limit script approvals in the Script Security plugin
  • Monitor disk usage and clean old builds with buildDiscarder

Common Problems

ProblemFix
docker: command not found in a buildAgent has no Docker. Use a Docker-enabled agent or the Docker Pipeline plugin
Permission denied on the Docker socketAdd the Jenkins user to the docker group (understand the security risk) or use Kaniko, Buildah or BuildKit builders
Pipeline cannot find JenkinsfileCheck branch and script path in the job configuration
Builds are slowUse caching, parallel stages and more agents
Secret appears in logUse withCredentials, avoid double-quoted sh interpolation

Practice Tasks

  1. Run Jenkins in Docker and create a Pipeline from a Jenkinsfile in GitHub
  2. Add a test stage that publishes JUnit results
  3. Use withCredentials to log in to a registry without exposing the password
  4. Add a GitHub webhook and trigger builds on push
  5. Create a shared library with a notifySlack or deployApp step

Conclusion

Jenkins runs pipelines defined in a Jenkinsfile on ephemeral agents. Use declarative syntax, credentials binding, parallel stages, multibranch pipelines and shared libraries, and keep the controller secured and updated.

Quick Summary: Jenkinsfile declarative; agents (docker/k8s); withCredentials; when/input; multibranch; shared library; JCasC.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators