Sub topic 13
From CI to Continuous Delivery and Retraining
Assume you know Python and pytest, use Git pull requests, and have basic experience with YAML-based CI services. This chapter focuses on what happens after CI has tested an ML change: how to package the result, promote it safely, and start retraining without creating uncontrolled deployments.
What CI hands off
A passing CI run does not automatically mean that a model should serve production traffic. CI should produce a traceable candidate. The candidate includes more than a model file.
| Artifact | What it records | Why it matters |
|---|---|---|
| Model artifact | Serialized model and format version | Allows exact loading and rollback |
| Data reference | Dataset version, URI, or content hash | Shows what trained the model |
| Metrics | Candidate and baseline results | Supports promotion decisions |
| Environment | Dependency and Python versions | Helps reproduce predictions |
| Source revision | Git commit or tag | Connects behavior to code |
Store these items together in an artifact repository or model registry. A registry is a service that stores model files, metadata, and lifecycle states such as candidate, staging, and production.
A useful handoff separates validation from promotion:
- CI trains or evaluates a candidate on a fixed dataset.
- CI runs code, data, model, and quality-gate tests.
- CI publishes the candidate and its metadata.
- A delivery process promotes it to staging.
- A human or an automated policy approves production deployment.
- The previous production version remains available for rollback.
Build a traceable release
The release metadata should identify every input that can affect the result. For example, a small Python script can create a manifest containing file hashes:
from hashlib import sha256
from pathlib import Path
import json
import sys
def file_hash(path: Path) -> str:
digest = sha256()
with path.open("rb") as file:
for chunk in iter(lambda: file.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def main() -> None:
if len(sys.argv) < 3:
raise SystemExit("usage: make_manifest.py OUTPUT INPUT [INPUT ...]")
output = Path(sys.argv[1])
inputs = [Path(value) for value in sys.argv[2:]]
manifest = {
"inputs": {str(path): file_hash(path) for path in inputs},
"source_revision": "set-by-ci",
"status": "candidate",
}
output.write_text(json.dumps(manifest, indent=2) + "\n")
if __name__ == "__main__":
main()The CI job should replace source_revision with the actual commit identifier and record the dependency lock file. Avoid relying on a timestamp or a floating package version as the only identifier. Two artifacts created on different days may otherwise be impossible to compare.
Connect CI to delivery
Continuous delivery means that CI prepares a tested release and makes it available for deployment. Continuous deployment goes further by deploying an approved release automatically.
A typical path looks like this:
Keep deployment permissions separate from ordinary test permissions. A pull request should not be able to deploy directly to production. Instead, the delivery job should accept a specific candidate identifier, verify its quality-gate results, and deploy that immutable version.
Design retraining triggers
Retraining should have an explicit trigger and a bounded process. Common triggers include:
- A fixed schedule, such as weekly or monthly.
- Data drift beyond an agreed threshold.
- A drop in confirmed production quality.
- A business event that changes the data-generating process.
- A manual request for investigation.
A trigger should start a candidate run, not replace production immediately. The run must use a defined data window, exclude future information, and evaluate against a stable holdout set. If the candidate fails a quality gate, retain the current model and alert the owner.
Prevent feedback loops. For example, a deployment event should not automatically count as a new training-data event unless that behavior is intentional. Record whether a run was started by a schedule, drift alert, or manual action.
Keep rollback and ownership clear
Every production model needs a rollback target. Keep the previous known-good version until the new version has passed its observation period. Monitor both technical signals, such as latency and error rate, and ML signals, such as missing values, prediction distributions, and delayed labels.
Define who can approve promotion, who investigates failed retraining, and who can disable an automated trigger. These decisions belong in the repository or delivery documentation, not only in an operator's memory.
Exercise
A nightly retraining job produces a candidate whose accuracy is below the minimum threshold. Should the delivery process deploy it because the job completed successfully?
Show answer
No. Job completion only shows that the process ran. The quality gate must reject the candidate, keep the current production model, publish the failure details, and alert the responsible owner.