Install Jenkins from code
A pinned image, pinned plugins, JCasC security and a first pipeline.
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: abee321fdc8fa04585b6bdbabc7e7063b3a3a8472239863a27e5c73125e76bd3Most 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
# 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.txtRUN 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:
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:
jenkins:numExecutors: 0securityRealm:local:allowsSignup: falseusers:- id: adminpassword: ${JENKINS_ADMIN_PASSWORD}authorizationStrategy:loggedInUsersCanDoAnything:allowAnonymousRead: falsenodes:- permanent:name: lab-agentlabelString: linuxremoteFS: /home/jenkins/agentnumExecutors: 2launcher: inboundunclassified: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:
name: lab-jenkinsservices:controller:build: controllerimage: secopslog-jenkins:2.580.2container_name: lab-jenkinsenvironment:JENKINS_ADMIN_PASSWORD: ${JENKINS_ADMIN_PASSWORD:?run ./up.sh}CASC_JENKINS_CONFIG: /casc.dports: ["127.0.0.1:8080:8080"]volumes:- lab-jenkins-home:/var/jenkins_home- ./casc.d:/casc.d:rohealthcheck:test: ["CMD", "curl", "-fs", "-o", "/dev/null", "http://localhost:8080/login"]interval: 3sretries: 100agent:profiles: [agent]build: agentimage: secopslog-jenkins-agent:3384container_name: lab-jenkins-agentenvironment:JENKINS_URL: http://controller:8080/JENKINS_AGENT_NAME: lab-agentJENKINS_SECRET: ${AGENT_SECRET:-unset}git:build: gitdimage: secopslog-gitd:1container_name: lab-gitports: ["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:
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
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:
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.
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.
FROM jenkins/jenkins:lts-jdk21 and plugins.txt lists git and workflow-aggregator with no versions. Two builds a month apart differ. Why?curl -u admin:correct-password -X POST .../createItem?name=x returns 403. The password is right. What is the likely cause?jk.sh fetches in a cookie session.JENKINS_HOME, which is on the volume, and the lab showed them surviving.JENKINS_HOME and survive with the volume.