Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

jenkins

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

Niveau
DevSecOps-service
Groep
Git en CI/CD
Root nodig
Voor wijzigingen
Pakket
jenkins

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Belangrijkste delen

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.

Opties 7

Meest gebruikt

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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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' } } } }
    }

Let op

  • 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.

Bestanden

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

Tips om te onthouden

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

Exitcodes

0Success (CLI).
1Error.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man 8 jenkins