Install Jenkins from code

A pinned image, pinned plugins, JCasC security and a first pipeline.

Beginner13 min · lesson 3 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (16 files, 7 KB): install.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/install.tar.gz && tar -xzf install.tar.gz, which creates ~/lab/install/. SHA-256: abee321fdc8fa04585b6bdbabc7e7063b3a3a8472239863a27e5c73125e76bd3

Most Jenkins servers start the same way: someone clicks through a setup wizard, installs "suggested plugins", and tweaks security settings until things work. Two years later the server is important, no one remembers those settings, and upgrading it feels dangerous. If the disk dies, nobody can rebuild it the same way.

This lesson builds Jenkins the other way around: everything that defines the server is a file in Git. That's what the lab has been doing all along, so here you'll read those files and test what they promise.

A pinned image with pinned plugins

controller/Dockerfile
# Jenkins LTS, pinned by digest, with every plugin pinned to an exact version.
FROM jenkins/jenkins:2.580.2-lts-jdk21@sha256:74401de6a6f92e1afe1b02d1c8e3d0c33f263bfb5275dd8813f9150355f2367b
# A fixed heap keeps a runaway build or plugin from taking the whole VM. 1 GB suits this lab;
# size a real controller from its monitoring, not from this number.
ENV JAVA_OPTS="-Xms512m -Xmx1g -Djenkins.install.runSetupWizard=false"
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt

The FROM line names the current LTS (long-term support) release, 2.580.2 on JDK 21, and pins it by digest. A tag can be moved to different content later, while a digest can't. Use LTS releases for a server that others depend on. The weekly line gets features sooner and is meant for people who want to test them. The ENV line fixes the Java heap at 1 GB, so a runaway build or plugin can't take the whole VM, and turns off the setup wizard, because the configuration comes from a file instead. Size a real controller's heap from its monitoring, not from this number.

plugins.txt lists exact versions, including every dependency, not only the plugins you chose. Without those pins, building the same Dockerfile next month could give you different plugins:

ubuntu@secopslog-cicd:~/lab/install · Jenkins 2.580.2
$ head -2 controller/Dockerfile; wc -l < controller/plugins.txt; grep -E "^(git|workflow-aggregator|configuration-as-code):" controller/plugins.txt
# Jenkins LTS, pinned by digest, with every plugin pinned to an exact version. FROM jenkins/jenkins:2.580.2-lts-jdk21@sha256:74401de6a6f92e1afe1b02d1c8e3d0c33f263bfb5275dd8813f9150355f2367b 75 configuration-as-code:2147.v8a_8d38668c72 git:5.11.0 workflow-aggregator:608.v67378e9d3db_1

To change a version, you edit the file and rebuild the image, so every plugin change goes through review and shows up in git log. Lesson "Plugin hygiene" covers how to find out when these pins need to move.

Security from a file

The Configuration as Code plugin, usually called JCasC, reads YAML and applies it to Jenkins at startup:

casc.d/00-base.yaml
jenkins:
numExecutors: 0
securityRealm:
local:
allowsSignup: false
users:
- id: admin
password: ${JENKINS_ADMIN_PASSWORD}
authorizationStrategy:
loggedInUsersCanDoAnything:
allowAnonymousRead: false
nodes:
- permanent:
name: lab-agent
labelString: linux
remoteFS: /home/jenkins/agent
numExecutors: 2
launcher: inbound
unclassified:
location:
url: http://controller:8080/

No self-signup. No anonymous read access. No executors on the controller. One admin, whose password comes from an environment variable referenced as ${JENKINS_ADMIN_PASSWORD}, so the file itself is safe to commit. The Compose file wires the parts together:

compose.yaml
name: lab-jenkins
services:
controller:
build: controller
image: secopslog-jenkins:2.580.2
container_name: lab-jenkins
environment:
JENKINS_ADMIN_PASSWORD: ${JENKINS_ADMIN_PASSWORD:?run ./up.sh}
CASC_JENKINS_CONFIG: /casc.d
ports: ["127.0.0.1:8080:8080"]
volumes:
- lab-jenkins-home:/var/jenkins_home
- ./casc.d:/casc.d:ro
healthcheck:
test: ["CMD", "curl", "-fs", "-o", "/dev/null", "http://localhost:8080/login"]
interval: 3s
retries: 100
agent:
profiles: [agent]
build: agent
image: secopslog-jenkins-agent:3384
container_name: lab-jenkins-agent
environment:
JENKINS_URL: http://controller:8080/
JENKINS_AGENT_NAME: lab-agent
JENKINS_SECRET: ${AGENT_SECRET:-unset}
git:
build: gitd
image: secopslog-gitd:1
container_name: lab-git
ports: ["127.0.0.1:9418:9418"]
volumes: ["lab-git-repos:/srv/git"]
volumes:
lab-jenkins-home:
lab-git-repos:

Jenkins state lives in a named volume, and the web port is published only on the VM's loopback address. Real deployments put a TLS reverse proxy in front, and set unclassified.location.url to the public https:// address. Jenkins builds links and redirects from that URL, so a stale value sends logins and links in notifications to the wrong host. Start it and test what the configuration claims, rather than trusting it:

ubuntu@secopslog-cicd:~/lab/install · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ curl -fsS -o /dev/null http://127.0.0.1:8080/api/json
curl: (22) The requested URL returned error: 403
$ curl -fsS -o /dev/null -u admin:wrong http://127.0.0.1:8080/api/json
curl: (22) The requested URL returned error: 401
$ . ./.env; curl -s -o /dev/null -w "%{http_code}\n" -u "admin:$JENKINS_ADMIN_PASSWORD" -X POST http://127.0.0.1:8080/createItem?name=x
403

The three refusals are three different protections. An anonymous call gets 403: authentication is required even to read. A wrong password gets 401. The third call has the correct password and still gets 403, because it's a POST without a crumb, Jenkins's anti-CSRF token. That protection stops a malicious web page from using a logged-in admin's browser to create jobs. jk.sh handles crumbs for you, and its listing is in your lesson files. API tokens, which a later lesson uses, don't need a crumb.

A first pipeline, and a restart

ubuntu@secopslog-cicd:~/lab/install · Jenkins 2.580.2
$ ./jk.sh '/pluginManager/api/json?tree=plugins[shortName]' | jq '.plugins | length'
75
$ ./seed.sh app app/ && ./newjob.sh hello app && ./run-job.sh hello
pushed app: 8dcd541 first commit job hello reads Jenkinsfile from git://git/app.git Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/hello + echo hello from lab-agent, build 1 hello from lab-agent, build 1 Finished: SUCCESS build #1 result: SUCCESS

Jenkins loaded 75 plugins, matching the 75 lines in plugins.txt. Now check that state survives a restart, since a container is disposable and the volume isn't:

ubuntu@secopslog-cicd:~/lab/install · Jenkins 2.580.2
$ docker compose --progress quiet restart controller && docker compose --progress quiet up -d --wait controller && ./jk.sh "/job/hello/api/json" | jq -r "\"\(.name) lastBuild=#\(.lastBuild.number)\""
hello lastBuild=#1
$ ./down.sh
lab stopped and its data removed

The job and its build history came back, because they live in JENKINS_HOME on the volume. That's the division to keep in mind: the image and YAML define how the server is set up, and the volume holds what happened on it. down.sh deleted the volume too, which is why each lesson starts clean. You'll back that volume up in the last lesson.

Watch out
The plain-HTTP setup and the shared Docker host suit a throwaway lab only. In production, terminate TLS in front of Jenkins and keep the controller away from any network that builds can reach. Also, don't mount the Docker socket into a controller or agent unless you accept that builds become root on that host.

Hands-on

Start the lab. Add systemMessage: "Lab server" under jenkins: in casc.d/00-base.yaml, then run docker compose --progress quiet up -d --force-recreate --wait controller. Look at the UI as the first lesson showed, or read ./jk.sh /configuration-as-code/export -X POST | grep systemMessage. Compare what a reviewer would see for this one-line change with what they would see for the same click in the UI. Finish with ./down.sh.

What you should see: the export contains systemMessage: "Lab server", and the API returns Lab server as the server's description. In review, the change is one line in a YAML diff. The same change made in the UI leaves no record at all.

Remember

Pin the image by digest and every plugin by version, keep configuration in YAML, and test what it promises: curl as nobody, with a wrong password, and with no crumb.

Quick check
01A Dockerfile uses FROM jenkins/jenkins:lts-jdk21 and plugins.txt lists git and workflow-aggregator with no versions. Two builds a month apart differ. Why?
Incorrect — The contents do change here. The tag and the unpinned plugins both resolve to newer releases.
Incorrect — Order is not what differs. A cached build would differ less, and the versions are the actual change.
Correct — Pin the image by digest and pin every plugin, dependencies included, to make builds repeatable.
Incorrect — JCasC configures Jenkins and does not choose plugin versions.
02curl -u admin:correct-password -X POST .../createItem?name=x returns 403. The password is right. What is the likely cause?
Incorrect — Jenkins does not lock accounts by default, and a bad login would give 401.
Correct — API tokens skip the crumb. A password login needs one, which jk.sh fetches in a cookie session.
Incorrect — A missing body gives a different error, and the request does not get that far.
Incorrect — There is no such minimum, and a bad name would not produce this status.
03You delete and recreate the controller container while keeping its volume. What is lost?
Incorrect — They live in JENKINS_HOME, which is on the volume, and the lab showed them surviving.
Incorrect — Credentials are stored encrypted in JENKINS_HOME and survive with the volume.
Incorrect — They come from the image and the volume. Neither needs a manual reinstall.
Correct — That split is the point of the design: definition in files, history on the volume.

Related