What CI and CD mean
30 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Last week a teammate's change broke ShopLite's checkout and nobody noticed for three days, because nobody ran the tests. ShopLite runs only on your laptop today; who builds, tests and ships it every time the code changes?
Continuous integration
CI (continuous integration) is the habit of merging small changes into the shared main branch often, while a server builds and tests every push automatically, so a break is found within minutes.
Here is the smallest CI setup for ShopLite: one workflow file that checks out the code, installs Java and runs the tests on every push.
# .github/workflows/ci.yml: the smallest useful CI (details in the next lessons)name: CIon: [push, pull_request]jobs: build-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - uses: actions/setup-java@v5 with: distribution: temurin java-version: '25' - run: ./mvnw -B verifyWithout CI, people work on long branches for weeks and merge at the end. Then many changes collide at once, tests nobody ran fail, and finding the cause takes days. CI turns one big painful merge into many small boring ones.
The server that runs the checks is a CI service. In this module it is GitHub Actions, and this is where ShopLite grows up: from "works on my laptop" to "checked and packaged by a robot on every change".
Order of execution
What happens on each push
Tap the steps in the order they happen.
Continuous delivery vs continuous deployment
Continuous delivery means every change that passes the pipeline produces a release that could go live at any moment, such as a tested Docker image tagged with the commit. A person decides when to deploy.
This program sends the same three commits through both ways of releasing, so you can compare what happens to each green commit.
Example 1 · Delivery or deployment: what happens to each green commit
import java.util.List;
public class DeliveryVsDeploymentDemo { record Commit(String sha, boolean testsPass) {}
static void run(String mode, List<Commit> mainCommits) { System.out.println(mode + ":"); for (Commit c : mainCommits) { String result; if (!c.testsPass()) { result = "pipeline red, nothing released"; } else if (mode.equals("continuous delivery")) { result = "image shoplite-api:" + c.sha() + " ready, waiting for a person"; } else { result = "image shoplite-api:" + c.sha() + " deployed to production"; } System.out.println(" " + c.sha() + " -> " + result); } }
public static void main(String[] args) { List<Commit> main = List.of( new Commit("a1b2c3d", true), new Commit("e4f5a6b", false), new Commit("c7d8e9f", true)); run("continuous delivery", main); run("continuous deployment", main); }}continuous delivery: a1b2c3d -> image shoplite-api:a1b2c3d ready, waiting for a person e4f5a6b -> pipeline red, nothing released c7d8e9f -> image shoplite-api:c7d8e9f ready, waiting for a person continuous deployment: a1b2c3d -> image shoplite-api:a1b2c3d deployed to production e4f5a6b -> pipeline red, nothing released c7d8e9f -> image shoplite-api:c7d8e9f deployed to production
What this code does
- In both modes a red pipeline stops the commit. The only difference is the last step for green commits: a ready image waiting for a decision, or an automatic deploy.
- Both modes tag the image with the commit SHA, so you always know exactly which code a release contains.
Continuous deployment goes one step further: every change that passes the pipeline on main is deployed to production automatically, with no button. It needs very good tests and a quick way to roll back, which means going back to the previous version.
Both are called CD. Most student and early-stage projects start with continuous delivery and add automatic deployment later; ShopLite does the same.
Predict the output
Delivery or deployment?
Decide which one each team uses.
Round 1 of 3
Which kind of CD is this team using?
The pipeline: from push to deploy
A pipeline is the chain of automatic steps from a push to production: build, test, package, deploy. Each step is a gate: if it fails, the change stops there and never reaches the next one.
This program sends three changes through ShopLite's four gates. Each change stops at its first failure.
Example 2 · A pipeline as gates: the first failure stops the change
import java.util.LinkedHashMap;import java.util.Map;
public class PipelineGatesDemo { static void runPipeline(String change, Map<String, Boolean> stages) { System.out.println(change); for (Map.Entry<String, Boolean> stage : stages.entrySet()) { if (!stage.getValue()) { System.out.println(" " + stage.getKey() + ": FAILED, stopped here"); return; } System.out.println(" " + stage.getKey() + ": ok"); } System.out.println(" live"); }
static Map<String, Boolean> stages(boolean build, boolean test, boolean pkg, boolean deploy) { Map<String, Boolean> m = new LinkedHashMap<>(); m.put("build", build); m.put("test", test); m.put("package image", pkg); m.put("deploy + smoke test", deploy); return m; }
public static void main(String[] args) { runPipeline("Fix coupon rounding", stages(true, true, true, true)); runPipeline("Native query with LIMIT in IN", stages(true, false, true, true)); runPipeline("Dockerfile typo", stages(true, true, false, true)); }}Fix coupon rounding build: ok test: ok package image: ok deploy + smoke test: ok live Native query with LIMIT in IN build: ok test: FAILED, stopped here Dockerfile typo build: ok test: ok package image: FAILED, stopped here
What this code does
- Each stage runs only if all earlier ones passed, so a change with a MySQL-only query bug is stopped by the Testcontainers tests and never becomes an image.
- The later a gate catches a problem, the more it costs; that is why fast, thorough tests come before packaging and deploying.
What each gate stops:
- Build: compiles the code. Stops syntax and type errors.
- Test: unit tests, then integration tests on MySQL Testcontainers. Stops logic bugs, broken queries and failing migrations.
- Package: builds the Docker image and pushes it with the commit SHA as tag. Stops a broken Dockerfile.
- Deploy: runs that exact image in an environment, then a smoke test. Stops a bad configuration.
On a pull request only build and test run. Packaging and deploying happen after the merge to main.
Pipeline principles
A reproducible pipeline gives the same result for the same commit every time, because versions (the JDK, the mysql:8.4 image, action versions) are pinned and no test depends on time or order.
A pipeline people trust is fast, reproducible, runs the same commands as your laptop, and fails loudly. The commands below show the third quality.
# On your laptop, before pushing./mvnw verify# In CI: the same Maven command, plus -B (batch mode: no colours or progress bars)./mvnw -B verifyFour qualities make a pipeline people trust:
- Fast: about ten minutes or less, or developers stop waiting for it. Cache dependencies and share test containers.
- Reproducible: the same commit gives the same result every time.
- Same commands as local: CI runs
./mvnw -B verify, which you can run on your laptop. Logic hidden only in YAML cannot be debugged locally. - Fails loudly: a red check blocks the merge, the log points at the failing test, and nobody ignores red builds.
-B is Maven's batch mode: no colours, progress bars or prompts, which keeps CI logs clean.
Find the bug
Which line breaks a principle?
Tap the line that makes the pipeline worse.
The build works today but may break next month. Which line is the problem?
Round 1 of 2GitHub Actions, GitLab CI and Jenkins
GitHub Actions is the CI service built into GitHub. When an event such as a push happens, it runs the steps of a workflow, a YAML file in .github/workflows/, on a machine that GitHub provides.
Here is the same Maven job written for GitLab CI. Only the wrapper changes; the command stays the same.
Example 3 · The same job in GitLab CI
Runs in: a GitLab repository
# .gitlab-ci.ymlstages: [test]build-test: stage: test image: eclipse-temurin:25-jdk services: - docker:dind # Docker for Testcontainers (needs a runner that allows it) variables: DOCKER_HOST: tcp://docker:2375 script: - ./mvnw -B verifyRunning with gitlab-runner 18.x $ ./mvnw -B verify [INFO] Tests run: 153, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS Job succeeded
What this code does
- The core is the same command,
./mvnw -B verify; only the wrapper changes:scriptinstead ofrun, a JDK image instead ofsetup-java, and Docker for Testcontainers added as a service. - This is why the pipeline principle "same commands as local" matters: moving to another CI tool is mostly a syntax change. Docker-in-Docker set-ups vary by runner, so check your GitLab runner's documentation.
Three tools cover most Java jobs you will meet:
- GitHub Actions: built into GitHub, hosted Linux, Windows and macOS runners, a large marketplace of ready actions. Best fit when your code is on GitHub.
- GitLab CI: built into GitLab, one
.gitlab-ci.ymlfile with stages. Best fit when your code is on GitLab. - Jenkins: an open-source server you install and maintain yourself, with pipelines in a
Jenkinsfile. Common in older and on-premise company setups.
The ideas are the same in all three: trigger, jobs, steps, artifacts, secrets. Learn one well and the others are a syntax change.
Next step
Check your understanding
12 quick questions