Continuous Integration
Learning Objectives
- You can explain pipeline dependencies and implement stage execution, result handling and cleanup.
- You can isolate test state and preserve meaningful failure status.
- You can explain how CI supports collaboration without requiring a hosted service.
Two people can run the same tests and reach different conclusions if they use different dependency versions, forget a suite, or leave different data in the database. How can a team check a change in a way that another person can repeat?
Continuous integration (CI) combines frequent integration of changes into a shared codebase with automated checks. A small CI pipeline can run the existing checks from one entry point and retain their results for a source revision. We will follow a renamed response field through such a run, including what happens when a check fails.
Pipeline stages and dependencies
A build artifact is the generated output prepared for release, such as the client’s HTML, JavaScript and CSS files. Figure 1 puts quick source checks before runtime suites and the build. Suites that reset the same database run sequentially so one cannot remove another’s fixture. Producing the artifact after verification ties it to the accepted source.
| Stage | Question |
|---|---|
| API/client source checks | Do the current sources and Hono RPC contract agree? |
| API tests | Do operations retain their response, permission and database rules? |
| Client tests | Does client logic respond correctly to its controlled inputs? |
| Browser workflow | Can the connected application complete the selected task? |
| Build | Can this revision produce its release artifact? |
Use the dependencies to determine a valid stage order. For example, this proposed fragment is invalid:
api-checkbrowser-testclient-checkHere, browser-test starts before client-check and before either runtime
suite. Moving only client-check earlier still leaves those runtime dependencies
unsatisfied. Independent source checks can swap with each other, and the two
runtime suites can swap once both source checks pass.
Arrange the stages in the puzzle using these dependencies. Both source checks must finish before either runtime suite starts; both suites precede the browser workflow and final build. The order inside either pair may vary. Later we will handle whether each stage actually succeeds.
CI stage order
0 / 5 points
Arrange all six stages so their dependencies are satisfied. Use the arrow buttons or drag the blocks, then choose Submit solution.
api-checkandclient-checkmust both finish before eitherapi-testorclient-teststarts.api-testandclient-testmust both finish beforebrowser-teststarts.buildfollows the browser workflow and is last.
Use each stage once. Either order inside the source-check pair is valid; either order inside the runtime-suite pair is valid. A correct complete order earns 5 points.
A response-field mismatch should fail the source stage before starting a browser. A changed authorization rule may compile and fail an API test instead. Keep raw API and concurrency checks alongside the typed client.
Following a response-field change
Suppose the public listing page expects title. A change renames the producer’s field to name, but leaves the view unchanged. The source check can catch that mismatch before starting a database or browser:
interface ListingSummary { title: string;}
const listingHeading = (listing: ListingSummary) => { return listing.title.toUpperCase();};
const response = { name: "Desk lamp" };
listingHeading(response);deno check reports that the argument lacks the required title property.
This example trace shows where the diagnostic stops the pipeline:
api-check: passedclient-check: failed - required property 'title' is missingruntime suites: not startedbrowser workflow: not startedbuild: not startedcleanup: completedrun result: failedCleanup releases the run’s resources while retaining its failure. Restoring the expected field lets the source checks pass and the runtime suites begin; their results still depend on the behavior they test.
Optional: run a small local check, test and build pipeline
If Bash and Deno 2 are already available, create a fresh directory and save the following three files there. The example has no package imports or downloads. It builds one HTML file; the full application’s API and browser suites remain separate checks.
Save as page.ts:
export const response = { title: "Desk lamp" };
export const listingHeading = (listing: { title: string }) => listing.title.toUpperCase();
export const renderPage = () => `<!doctype html><title>Listing</title><h1>${listingHeading(response)}</h1>`;
if (import.meta.main) { await Deno.writeTextFile(Deno.args[0], renderPage());}Save as page_test.ts:
import { renderPage } from "./page.ts";
Deno.test("page contains the listing heading", () => { if (!renderPage().includes("<h1>DESK LAMP</h1>")) { throw new Error("Expected the upper-case listing heading"); }});Save as ci.sh:
#!/usr/bin/env bashset -eubuild_dir=$(mktemp -d)trap 'rm -rf "$build_dir"' EXIT
deno check page.ts page_test.tsdeno test --no-check page_test.tsdeno run --allow-write="$build_dir" page.ts "$build_dir/index.html"cp "$build_dir/index.html" index.htmlRun bash ci.sh from that directory. The source check runs first, the test
checks rendered output, and the build writes index.html only after both pass.
--no-check avoids repeating the source check during the test stage. The
temporary build directory is removed when the script exits; index.html remains
as the artifact. The build command’s --allow-write permission is limited to
that temporary directory. See the Deno test options
for the test command’s flags.
Now change only the first line of page.ts to
export const response = { name: "Desk lamp" }; and run bash ci.sh again.
The source check reports the missing title and the script exits with a
nonzero status. Run echo $? immediately afterward to inspect it. The test and
build do not run, the temporary directory is removed, and any earlier
index.html remains an artifact of the earlier run. Restore title and rerun:
the checks pass and the build replaces that artifact.
For a runtime failure, keep title and change toUpperCase() to
toLowerCase(). The source check passes, but the test fails and stops the build.
Restore toUpperCase() and rerun to return to a passing pipeline. This
distinguishes an incompatible field from incorrect behavior with compatible
types.
Failure diagnostics and exit status
A command’s exit status reports its outcome: 0 means success and a nonzero value means failure. A failed run is useful only if it reached the source check and reported the intended mismatch. An unavailable compiler would also stop the run, but would tell us nothing about the field. A useful runner probe starts from a passing pipeline, introduces the intended mismatch, checks its diagnostic and nonzero exit, then restores a passing run.
Also read the final status after cleanup. If the last printed line says that cleanup succeeded, has the runner retained the earlier failure? Use both the diagnostic and the reported status to assess the following run.
Pipeline records after a type change
0 / 5 points
A pipeline passes before a deliberate response-field type mismatch is introduced, and passes again after the original field type is restored. Consider four possible records from the run with the mismatch present. An exit status of 0 means success; a nonzero status means failure.
| Run | Verification observation | Cleanup status | Final status |
|---|---|---|---|
| A | Missing dependency; source check never starts | 0 | 1 |
| B | Source check reports the intended field mismatch and exits 2 | 0 | 0 |
| C | Source check reports the intended field mismatch and exits 2 | 0 | 2 |
| D | Source check was skipped; build succeeds | 1 | 1 |
Which record shows that the intended failure was detected and remained a failed pipeline run?
One entry point for sequential stages
A shell pipeline can use commands as stages and a trap for cleanup. The next
exercise isolates that lifecycle in TypeScript: each stage and
cleanup are promise-returning callbacks. Calling a callback starts work;
await waits for its result before deciding whether to start the next stage.
A finally block can arrange cleanup after completion or an exception.
A return inside finally replaces a result that was about to be returned from try, even if cleanup succeeded. Predict how this affects the status in the next example.
Read a cleanup return
0 / 5 points
release() returns a fulfilled Promise<void>. Read this function:
const checkedRun = async (): Promise<number> => { try { return 4; } finally { await release(); return 0; }};What value does its promise resolve to?
Cleanup also needs to wait for the active stage to settle. Imagine that an API test is still waiting for its database request: cleaning up the database at that point would interrupt the test. Trace the pending work and retained result through these events before implementing the callback helper.
| Event | Retained result | What happens next |
|---|---|---|
| A stage is pending | No result yet | Wait; do not start the next stage or cleanup. |
The stage resolves to 0 | Success so far | Start the next stage. |
| The next stage throws | Failure (1 in the supplied callback contract) | Skip remaining stages and await cleanup. |
Cleanup later resolves to 0 | The earlier failure still applies | Finish with failure. |
If all stages succeed, a cleanup failure becomes the result instead. Cleanup belongs to the supplied run context, so its callback still runs for an empty stage list. Figure 2 brings these paths together: cleanup runs in both cases, while the earlier stage outcome determines which status takes priority.
Implement this lifecycle using the controlled callbacks in the next exercise.
Stage execution and cleanup
0 / 45 points
Complete runStages(stages, execute, cleanup): Promise<number> in pipeline-result.ts and preserve its supplied exports.
stages is an ordered list of names. The supplied execute and cleanup callbacks return promises of integer statuses from 0 through 255. Status 0 means success; any nonzero status means failure. Either callback may throw or return a rejected promise; count either outcome as status 1.
Run stages in order, waiting for each result before starting another. After the first failed stage, start no later stage. Cleanup must run exactly once and finish before runStages resolves, including when the stage list is empty.
The returned status follows this priority:
- Return the first failed stage’s status, using 1 if its
executecallback throws or rejects. - If all stages succeeded or there were no stages, return cleanup’s status, using 1 if
cleanupthrows or rejects.
Examples:
| Stage result | Cleanup result | Returned status |
|---|---|---|
| First failure is 2 | 7 | 2 |
| First failure is 3 | 0 | 3 |
| All succeed | 7 | 7 |
| Empty list | 0 | 0 |
| Execution throws | 0 | 1 |
| All succeed | Throws | 1 |
Choose Submit solution. Sequential execution and stopping are worth 20 points; result priority is worth 15; exactly-once cleanup is worth 10. Use the supplied callbacks.
The completed helper gives one run a repeatable lifecycle. A runner can use the same ordering and status rules when its callbacks invoke the development checks. We still need to consider what happens when two such runs overlap.
Isolated state for each CI run
Now imagine that the failed revision and a corrected revision are being checked at the same time. A unique Compose project name isolates default resource names. A test configuration can also avoid fixed host ports and use temporary database storage. Each run then starts its own application and test accounts, including the same-origin account path used by authenticated browser checks.
If both runs still point to the same schema, the second can recreate it just after the first inserts a test account. The first run’s next request can then fail because its account has disappeared. Either isolate the mutable state or run its users one after another. These choices can be combined: each run owns its database, while suites that share that database run in order.
The Compose project-name documentation explains the naming behavior. Cleanup must target that run’s configuration. A broad volume-deletion command in a development directory can remove unrelated data.
Parallel test jobs
0 / 5 points
Two CI jobs each drop and recreate the same test schema. They pass separately but fail in parallel. Which change addresses the cause?
A hosted runner can invoke the same entry point. Limit its permissions and keep production secrets out of jobs running untrusted contributions; the official Actions guidance develops that deployment-specific concern.
Keep the result with useful diagnostics, excluding secrets. Retain dependency declarations and their generated lockfiles together so another run can install the recorded versions. The API and client checked together must also come from one source revision.
That shared revision is also a boundary of our example: its source checks compare the current producer and consumer. The next chapter follows the same kind of field change into a release, where an older browser bundle may still be open.
Check Your Understanding
- Which information lets another person repeat and interpret a CI run?
- Why should source checks run before browser workflows?
- What can still conflict despite different Compose project names?
- Why must an intended failing probe inspect the cause of failure?
- Which exit status should cleanup preserve after a failed stage?