CI test and build stages
Prerequisite: node-scaling-and-cost-boundaries. Objective: Design deterministic pipeline. Each example is independent. Tested with Node.js 22.16.0 on Linux. Save as main.mjs in a fresh disposable directory and run node main.mjs. These are static code blocks; external system behavior is not established by a local model.
How it works
A useful pipeline validates source and lockfiles, runs tests using an explicit Node version, builds once, records artifact identity and test evidence, and publishes only to an approved staging destination. A failing check blocks promotion. Review CI actions at immutable commit revisions and keep untrusted pull-request jobs separate from privileged release jobs. Do not expose production credentials to untrusted code or run package lifecycle hooks without review.
Observe a real test-runner gate
import assert from 'node:assert/strict';import {spawnSync} from 'node:child_process';import {writeFileSync} from 'node:fs';writeFileSync('gate.test.mjs',"import test from 'node:test';import assert from 'node:assert/strict';test('sum',()=>assert.equal(2+2,4));");const result=spawnSync(process.execPath,['--test','gate.test.mjs'],{encoding:'utf8',timeout:2000});assert.equal(result.status,0);console.log('tests passed');Expected output:
tests passedThe native test runner succeeds inside a disposable directory; no hosted CI is triggered.
Reject a failed pipeline stage
import assert from 'node:assert/strict';const checks={test:true,build:true,integration:false};function promotable(checks){return ['test','build','integration'].every(k=>checks[k]===true);}assert.equal(promotable({test:true,build:true}),false);console.log(promotable(checks));Expected output:
falseMissing or failed required checks deny promotion.
Common mistake
Do not deploy merely because the build command returned zero, or rebuild differently in each environment without accounting for artifact identity.
Practice
Add an integration test gate and show what should happen when it fails.
Reference candidate: node-ci-test-and-build-stages-lookup (route unverified). Documentation anchors: https://docs.github.com/en/actions/reference/security/secure-use. External versions and full integration are pending.
Administrative staged lab: staged-deployment-labs/ci/pipeline.md. Read its README for prerequisites, execution boundaries and cleanup. Not a verified public route.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CI test and build stages — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What should a failing required test do?
Why pin third-party CI actions to reviewed immutable revisions?
What does the local test runner prove?