Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

jenkins

The classic open-source automation server: pipelines as code in a Jenkinsfile, thousands of plugins, agents on any OS.

Level
DevSecOps service
Group
Git and CI/CD
Needs root
For changes
Package
jenkins

What it does

Jenkins is a Java web app (port 8080) that runs jobs. Modern jobs are Pipelines defined in a Jenkinsfile in the repository (declarative syntax: pipeline { agent … stages { … } }). The controller schedules work; agents (VMs, containers, Kubernetes pods) run it. Many companies still run Jenkins, so you meet it in DevOps jobs even when new projects use GitHub Actions or GitLab CI.

Jenkins is a common attack target: keep it and its plugins updated, never open it to the internet without SSO, and do not run builds on the controller.

How to write it

systemctl {start|status} jenkins
java -jar jenkins-cli.jar -s URL -auth USER:TOKEN COMMAND

Main parts

Jenkinsfile blocks

pipeline
The whole declarative pipeline.
agent
Where it runs (any, label, docker, kubernetes).
stages / stage / steps
The work.
environment
Variables, also credentials('id').
when
Conditions (branch, tag, changes).
post
Steps after the run (always, success, failure).
options
timeout, retries, build history.
parameters
Inputs for a build.

Options 7

Most used

CLI commands

who-am-i
Check the login.
build JOB [-s] [-p K=V]
Start a build (wait with -s).
console JOB [N]
Show console output.
list-jobs
List jobs.
install-plugin NAME
Install a plugin.
safe-restart
Restart when builds are done.
reload-configuration
Reload config from disk.

Examples 10

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

  1. 1Install on RHEL

    curl -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 install -y java-21-openjdk jenkins
    systemctl enable --now jenkins
    cat /var/lib/jenkins/secrets/initialAdminPassword
  2. 2Run Jenkins in a container

    docker run -d --name jenkins -p 127.0.0.1:8080:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk21
  3. 3A declarative Jenkinsfile

    cat Jenkinsfile
    pipeline {
      agent { docker { image 'python:3.12-slim' } }
      options { timeout(time: 20, unit: 'MINUTES') }
      stages {
        stage('Test')  { steps { sh 'pip install -r requirements.txt && pytest -q' } }
        stage('Scan')  { steps { sh 'trivy fs --exit-code 1 --severity HIGH,CRITICAL .' } }
        stage('Build') { when { branch 'main' } steps { sh 'docker build -t shop:$BUILD_NUMBER .' } }
      }
      post { failure { echo 'Build failed' } }
    }
  4. 4Use stored credentials safely

    cat Jenkinsfile
        stage('Push') {
          steps {
            withCredentials([usernamePassword(credentialsId: 'ghcr', usernameVariable: 'U', passwordVariable: 'P')]) {
              sh 'echo "$P" | docker login ghcr.io -u "$U" --password-stdin'
            }
          }
        }
  5. 5Use the CLI with an API token

    curl -O http://localhost:8080/jnlp/jenkins-cli.jar
    java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:API_TOKEN who-am-i
    java -jar jenkins-cli.jar -s http://localhost:8080 -auth admin:API_TOKEN build shop/main -s
  6. 6Start a build with the REST API

    curl -X POST -u admin:API_TOKEN http://localhost:8080/job/shop/job/main/build
  7. 7Configuration as Code (JCasC)

    cat jenkins.yaml
    jenkins:
      numExecutors: 0
      securityRealm:
        local:
          allowsSignup: false
    unclassified:
      location:
        url: https://ci.example.com/
  8. 8Read the logs

    journalctl -u jenkins -n 50
  9. 9Back up Jenkins

    tar czf jenkins-backup-$(date +%F).tgz -C /var/lib jenkins --exclude='jenkins/workspace'
  10. 10Run agents as Kubernetes pods

    cat Jenkinsfile
    pipeline {
      agent { kubernetes { yamlFile 'ci/pod.yaml' } }
      stages { stage('Test') { steps { container('python') { sh 'pytest -q' } } } }
    }

Watch out

  • Set controller executors to 0; run builds only on agents.
  • Update plugins often — most Jenkins CVEs are in plugins.
  • sh "echo ${PASSWORD}" with double quotes leaks the secret into logs; use single quotes so the shell expands it.

Files

/var/lib/jenkins/
JENKINS_HOME: jobs, plugins, secrets.
/var/lib/jenkins/secrets/initialAdminPassword
First-start password.
Jenkinsfile
The pipeline in the repository.

Tips to remember

  • Use the Kubernetes plugin for agents as pods; each build gets a clean environment.

Exit codes

0Success (CLI).
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 jenkins