What CI is, and where Jenkins fits

A green build, a breaking commit, a red build and a fix, on the lab you keep for the course.

Beginner15 min · lesson 1 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (18 files, 7 KB): whatis.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/whatis.tar.gz && tar -xzf whatis.tar.gz, which creates ~/lab/whatis/. SHA-256: d04aa334fa3636deb3fcf4b89ba5b3b6d007865be7e71070876fd26197f25060

Someone changes one line in calc.py and pushes it on a Friday. The change looks harmless, nobody runs the tests, and the bug surfaces on Monday when another developer pulls the code and everything is broken. By then the commit is buried under twenty others. Continuous integration (CI) closes that gap: a server builds and tests every push, right away, so the person who broke something finds out while the change is still in their head.

In this lesson you start the lab you'll keep for the whole course, then watch that loop happen on a real Jenkins: a passing build, a breaking commit, a failed build that names the commit, and a fix.

CI, CD and Jenkins

The loop on every push
1Push
a commit reaches the shared repository
2Build
on a clean machine, not a laptop
3Test
the same tests, every time
4Report
green or red, with the commit named
Continuous delivery extends this loop: every green build produces something you could release.

Continuous delivery means every passing build produces a release candidate. Continuous deployment goes one step further and ships it with no human approval. Jenkins is one engine that runs these loops. It's open source and self-hosted, and almost everything it does beyond the core comes from plugins. Those two facts are why it suits teams that need to run builds on their own network, and why it needs more care than a hosted service.

ToolWhere it runsPipeline lives inYou operate
JenkinsYour serversJenkinsfileController, agents, plugins, backups
GitLab CI/CDGitLab.com or self-managed.gitlab-ci.ymlRunners, and the whole of GitLab if self-managed
GitHub ActionsGitHub, or your own runners.github/workflows/*.ymlSelf-hosted runners only, if you use them

Start the lab

Everything in this course runs in containers inside a disposable Linux VM with Docker Engine. If you took the Docker courses, reuse that lab VM. If not, the first lesson of Docker for beginners ("What Docker is, and why") sets one up with a small script that also installs the tools these labs call. Give it at least 4 CPUs and 6 GB of memory, and never run these labs on a server you care about. Each lesson has a download link for its files, with the command that fetches them inside the VM into ~/lab/<lesson>/. Check the tools first:

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ docker compose version --short; for t in git jq curl openssl; do command -v $t >/dev/null && echo "$t: ok" || echo "$t: MISSING"; done
5.7.0 git: ok jq: ok curl: ok openssl: ok

If a line says MISSING, install it with sudo apt-get install -y git jq curl openssl. Then start the stack. The first run builds three images and takes several minutes, and later lessons reuse them:

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ docker compose --profile agent ps --format "table {{.Service}}\t{{.Image}}\t{{.Status}}"
SERVICE IMAGE STATUS git secopslog-gitd:1 Up 11 seconds controller secopslog-jenkins:2.580.2 Up 10 seconds (healthy) agent secopslog-jenkins-agent:3384 Up 2 seconds

Three services are running. controller is Jenkins itself. agent is the machine that runs builds. git is a small Git server, so the lab needs no external account. up.sh also created an admin password and stored it in .env, readable only by you. The next lessons take these pieces apart. For now, treat them as a working CI server.

Open the web UI

The scripts in this course talk to Jenkins through its REST API, so every step can be shown as text. Jenkins is also a web application, and it helps to look at each job in a browser after you run it. Port 8080 listens only on the VM's own loopback address, so you reach it through a forwarded port. With Lima, nothing is needed: Lima forwards the VM's loopback ports to your computer by default. With Multipass, run this on your computer and leave it running:

your computer (Multipass)
# once: let your SSH key into the VM (any public key you have works)
multipass exec secopslog-docker -- bash -c 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_ed25519.pub
# each session: forward port 8080 until you press Ctrl+C
ssh -N -L 8080:127.0.0.1:8080 ubuntu@$(multipass info secopslog-docker | awk '/IPv4/{print $2; exit}')

Then open http://127.0.0.1:8080 and sign in as admin. The password is in the lab folder: grep JENKINS_ADMIN_PASSWORD .env. Each job page lists its builds on the left, and a build's Console Output shows the full log that RAW=1 ./run-job.sh prints.

Watch out
The lab Git server has no authentication, and the lab uses plain git:// so that nothing depends on an outside service. It listens only on the VM's loopback address. Real Jenkins jobs fetch over HTTPS or SSH with a credential.

A repo and a green build

The sample app is one function and one test. The pipeline is a file in the repo, next to the code it builds:

app/Jenkinsfile
pipeline {
agent { label 'linux' }
stages {
stage('Test') {
steps {
sh 'python3 -m pytest -q'
}
}
}
}

seed.sh publishes the app/ directory as a repository, and newjob.sh creates a job that reads its pipeline from that repository. run-job.sh starts a build, waits for it and prints the console. It hides Git plumbing and [Pipeline] bookkeeping to keep the output readable, and RAW=1 ./run-job.sh app shows you everything.

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ ./seed.sh app app/
pushed app: 2460084 first commit
$ ./newjob.sh app app
job app reads Jenkinsfile from git://git/app.git
$ ./run-job.sh app
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/app + python3 -m pytest -q . [100%] 1 passed in 0.00s Finished: SUCCESS build #1 result: SUCCESS

The line Running on lab-agent tells you where the work happened. The test ran on the agent from a fresh clone, so it can't depend on anything sitting on your laptop. Finished: SUCCESS is the verdict Jenkins records for this build.

Break it

Change + to - and push, as a developer in a hurry might. Nobody ran the tests locally, and that's the case CI exists for:

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ sed -i 's/a + b/a - b/' app/calc.py && ./commit.sh app/ 'change add'
pushed: 2ddc003 change add
$ ./run-job.sh app
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/app + python3 -m pytest -q F [100%] =================================== FAILURES =================================== ___________________________________ test_add ___________________________________ def test_add(): > assert add(2, 2) == 4 E assert 0 == 4 E + where 0 = add(2, 2) test_calc.py:5: AssertionError =========================== short test summary info ============================ FAILED test_calc.py::test_add - assert 0 == 4 + where 0 = add(2, 2) 1 failed in 0.01s ERROR: script returned exit code 1 Finished: FAILURE build #2 result: FAILURE

The pytest output tells you what broke: assert 0 == 4. The shell step exited with status 1, Jenkins reported script returned exit code 1, and the build finished as FAILURE. Our script passes that on as its own exit status, which is how other tools can react to it. Jenkins also recorded which commits this build picked up:

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ ./jk.sh '/job/app/api/json?tree=builds[number,result,changeSets[items[msg]]]' | jq -r '.builds[] | "#\(.number) \(.result) \([.changeSets[].items[].msg] | join(","))"'
#2 FAILURE change add #1 SUCCESS

Build 2 failed, and it's the build that contains change add. Nobody has to search for the guilty change on Monday, because Jenkins already connected the failure to it. Build 1 shows no message because a job's first build has nothing earlier to compare with.

Fix it and clean up

ubuntu@secopslog-cicd:~/lab/whatis · Jenkins 2.580.2
$ sed -i 's/a - b/a + b/' app/calc.py && ./commit.sh app/ 'restore add'
pushed: dbecd65 restore add
$ ./run-job.sh app | tail -2
Finished: SUCCESS build #3 result: SUCCESS
$ ./down.sh
lab stopped and its data removed

down.sh removes the containers, the network and all lab data, and keeps the images so the next lesson starts quickly. Each lesson starts from nothing and cleans up after itself, so you can take them in any order once your images are built.

A hands-on that says "in a fresh lab" means: start the stack, publish the repo, and create the jobs that hands-on names, from the lesson's folder. seed.sh starts the repo's history over, so you can run it again after a walkthrough. For this lesson:

fresh lab
./up.sh
./seed.sh app app/
./newjob.sh app app # later lessons: ./newjob.sh JOB app [JENKINSFILE]
If this happensDo this
up.sh prints agent did not connectRun ./agent.sh again. If it still fails, docker compose --profile agent logs agent shows why.
A build waits foreverrun-job.sh gives up after 240 seconds and prints what the queue is waiting for. Read that line first.
Containers or a password from another lesson get in the wayEvery lesson uses the compose project lab-jenkins. From any folder: docker compose -p lab-jenkins --profile agent down -v. Then ./up.sh in the new folder.
Jenkins itself misbehavesdocker compose logs controller shows its log.

Run ./down.sh before you move to the next lesson's folder, and most of those problems never happen.

Hands-on

In a fresh lab, add a second test to app/test_calc.py that fails on purpose, for example assert add(-1, 1) == 1. Push it with ./commit.sh app/ "failing test" and run the job. Read the console from the bottom up: find Finished, then the ERROR line above it, then the test failure. Then run RAW=1 ./run-job.sh app and compare what the script normally hides. Finish with ./down.sh.

What you should see: the build ends FAILURE with 1 failed, 1 passed. Reading up from Finished: FAILURE, you reach ERROR: script returned exit code 1, then pytest's summary naming test_add_negative and assert 0 == 1. In the lab run, the raw console was 64 lines against 20 filtered; the extra lines are the Git checkout and [Pipeline] bookkeeping.

Remember

Jenkins only knows what your commands tell it. Here pytest exiting with status 1 made the build red and tied it to change add. A later lesson shows how a stray || true loses that signal.

Quick check
01A team runs its full test suite by hand once a week before release. They add a server that runs the same suite on every push. What has actually changed about a bug introduced on Tuesday?
Incorrect — CI runs after a push. It finds bugs quickly and does not stop anyone writing them.
Incorrect — They are the same tests. A server runs them more often, and that adds no coverage.
Correct — The suite is unchanged. What changes is how soon it runs and how few changes could be responsible.
Incorrect — Deployment is a separate step. Fast feedback on the commit is the benefit of CI alone.
02Which description separates continuous delivery from continuous deployment?
Correct — Both produce a release candidate from each passing build. Only deployment removes the human decision.
Incorrect — The terms describe how far automation goes, and they apply to any kind of software.
Incorrect — Both depend on tests passing. Skipping them would defeat the point of either.
Incorrect — They differ on one specific point: whether a person approves the release.
03In the lab, a shell step in sh 'python3 -m pytest -q' exits with status 1. What decides that the build is a FAILURE?
Incorrect — Jenkins does not read the output. The same result would follow from any command exiting 1.
Incorrect — There is no comparison at this point, and this step publishes no test results to compare.
Incorrect — Commit messages have no effect on results. They are recorded for people to read.
Correct — sh fails the step when the command exits non-zero, and that failure fails the build.

Related