Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 13Jenkins · Git and CI/CD

Install and set up Jenkins

Jenkins is the classic CI/CD server: it builds, tests, scans and deploys your code on every change. Many companies still run their main pipelines on Jenkins, with thousands of plugins. This guide installs the Jenkins LTS version with Java on RHEL and Ubuntu, finishes the setup wizard, makes the first safe settings (no builds on the controller, HTTPS through Nginx, backups), and runs a first pipeline from a Jenkinsfile.

  • 11 Lessons
  • 18 Commands

$ run as a normal user# run as root (or with sudo)

13.1

Know it before you install

The Jenkins controller is a Java web app that keeps the jobs, plugins and credentials. Agents (other machines or containers) run the builds. A pipeline is written as code in a Jenkinsfile in your Git repository.

ItemValue
Packagejenkins (from pkg.jenkins.io, LTS = "stable")
NeedsJava 17 or 21 (java-21-openjdk / openjdk-21-jre) and fontconfig
Servicejenkins.service, runs as user jenkins
Data (JENKINS_HOME)/var/lib/jenkins/ — jobs, plugins, credentials, config
Port8080/tcp (web), 50000/tcp (inbound agents, only if you use them)
Settingssystemctl edit jenkins (port, Java options)
Logsjournalctl -u jenkins

What you need: 2 CPU, 4 GB RAM and 50 GB disk for a small team (builds and artifacts fill the disk).

13.2

Install on RHEL, Rocky, Alma and Fedora

curl -fsSL -o /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
dnf -y install fontconfig java-21-openjdk
dnf -y install jenkins
systemctl daemon-reload
systemctl enable --now jenkins
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

Note

Jenkins changes its package signing key from time to time. If dnf or apt says the key is wrong or expired, take the current key file name from pkg.jenkins.io and import it again.

13.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y fontconfig openjdk-21-jre curl
curl -fsSL -o /etc/apt/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" > /etc/apt/sources.list.d/jenkins.list
apt-get update
apt-get install -y jenkins
systemctl status jenkins --no-pager

13.4

The setup wizard

Open http://SERVER:8080. Jenkins asks for the first admin password:

cat /var/lib/jenkins/secrets/initialAdminPassword
3f0c5e6b2a8d4c9e8f1a7b6c5d4e3f2a

Then:

  1. Choose Install suggested plugins (Git, Pipeline, Credentials, …).
  2. Create your own admin user (not admin/admin).
  3. Set the Jenkins URL to the final HTTPS address, for example https://ci.example.com/.

Commands in this lesson

13.5

First configuration: the settings to make first

Make these settings in Manage Jenkins before the first real job:

WhereSettingWhy
Nodes → Built-In Node → ConfigureNumber of executors = 0Builds never run on the controller, so a build can not read its secrets and config.
SecurityAuthorization: Matrix-based or Role-based (plugin)Each team gets only the rights it needs; anonymous users get nothing.
SecurityAgents → TCP port: disable (or fixed port)Close port 50000 when agents connect over SSH.
CredentialsStore Git keys, tokens and passwords herePipelines use them by ID, never in plain text.
SystemJenkins URL, admin email, global build discardersCorrect links; old builds are deleted automatically.
PluginsUpdate all, remove plugins you do not useMost Jenkins CVEs are in plugins.

Put Jenkins behind Nginx with HTTPS and listen only on localhost. Change the port and the listen address with a systemd override:

systemctl edit jenkins
/etc/systemd/system/jenkins.service.d/override.conf
[Service]
Environment="JENKINS_LISTEN_ADDRESS=127.0.0.1"
Environment="JENKINS_PORT=8080"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Xmx2g"
/etc/nginx/conf.d/ci.example.com.conf
server {
    listen 443 ssl;
    server_name ci.example.com;
    ssl_certificate     /etc/letsencrypt/live/ci.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ci.example.com/privkey.pem;
    client_max_body_size 100m;
 
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_http_version 1.1;
        proxy_request_buffering off;
    }
}
systemctl restart jenkins
nginx -t && systemctl reload nginx

13.6

Add an agent

The easiest agent is a Linux machine over SSH with Java and Git:

dnf -y install java-21-openjdk git
useradd -m -s /bin/bash jenkins
mkdir -p /home/jenkins/.ssh && chmod 700 /home/jenkins/.ssh

Put the public key of a Jenkins credential in /home/jenkins/.ssh/authorized_keys on the agent. Then in Jenkins: Nodes → New Node → Permanent Agent, launch method Launch agents via SSH, a label like linux docker, and the remote root /home/jenkins/agent.

13.7

Your first pipeline

Put this Jenkinsfile in the root of your Git repository:

Jenkinsfile
pipeline {
    agent { label 'linux' }
    options {
        timeout(time: 30, unit: 'MINUTES')
        buildDiscarder(logRotator(numToKeepStr: '20'))
    }
    stages {
        stage('Test') {
            steps { sh 'make test' }
        }
        stage('Build image') {
            steps { sh 'docker build -t registry.example.com/shop:${GIT_COMMIT} .' }
        }
        stage('Scan') {
            steps { sh 'trivy image --exit-code 1 --severity HIGH,CRITICAL registry.example.com/shop:${GIT_COMMIT}' }
        }
        stage('Deploy') {
            when { branch 'main' }
            steps {
                input message: 'Deploy to production?'
                withCredentials([file(credentialsId: 'kubeconfig-prod', variable: 'KUBECONFIG')]) {
                    sh 'kubectl set image deploy/shop shop=registry.example.com/shop:${GIT_COMMIT}'
                }
            }
        }
    }
}

In Jenkins: New Item → Multibranch Pipeline, add the Git repository and its credential. Jenkins finds every branch with a Jenkinsfile and builds it. Add a webhook in GitHub/GitLab to https://ci.example.com/github-webhook/ (GitHub plugin) so builds start at once.

Real uses at work:

  • Build, test and scan every pull request; deploy main after approval.
  • Night jobs: backups, reports, dependency updates.
  • Shared libraries: one pipeline code for many projects.

13.8

Make it safe

Security checklist:

  • 0 executors on the controller; builds only on agents.
  • HTTPS (Nginx) and Jenkins on 127.0.0.1; no public port 8080.
  • Matrix or role-based rights; no anonymous read; SSO/LDAP if you can.
  • Secrets only in Credentials; withCredentials masks them in logs.
  • Update Jenkins LTS and the plugins every month; read the Jenkins security advisories.
  • Back up JENKINS_HOME (at least config.xml, jobs/, credentials.xml, secrets/, plugins/).
  • Limit the Script Console to admins; it can run any code on the controller.

13.9

Update, back up and remove

dnf -y upgrade jenkins
apt-get install --only-upgrade -y jenkins
systemctl stop jenkins
tar -czf /backup/jenkins-$(date +%F).tar.gz -C /var/lib jenkins --exclude='jenkins/workspace' --exclude='jenkins/caches'
systemctl start jenkins
dnf -y remove jenkins

Watch out

JENKINS_HOME/secrets/ holds the key that decrypts all credentials. Keep it in the backup, and keep the backup as secret as the passwords themselves.

13.10

Fix common problems

ProblemCauseFix
Service does not startWrong or missing Java version.java -version (17 or 21); journalctl -u jenkins -n 50.
Address already in use on 8080Another app uses the port.ss -ltnp 'sport = :8080'; change JENKINS_PORT.
"It appears that your reverse proxy set up is broken"Missing proxy headers or wrong Jenkins URL.Set X-Forwarded-* headers and the Jenkins URL to the HTTPS address.
No valid crumb was included in the requestCSRF check fails (proxy or old session).Correct proxy headers; log in again.
Plugin errors after an updatePlugin versions do not fit together.Manage Jenkins → Plugins → update the dependencies; restart.
Disk fullOld builds and workspaces.Build discarder; delete workspace/ of old jobs; agents clean their workspaces.
Build waits: "Waiting for next available executor"No agent with that label is online.Check Nodes; start the agent; fix the label.

13.11

Next steps

1Why set the executors of the built-in node to 0?

Builds then never run on the controller, so a build can not read the controller's files and secrets.

2Which folder must be in every Jenkins backup, and why is it sensitive?

JENKINS_HOME/secrets/ — it holds the key that decrypts all stored credentials.

All commands in this chapter