This repository implements a robust MLOps Framework for predicting defects in Terraform infrastructure code. It demonstrates how to integrate continuous defect prediction into a CI/CD pipeline using reusable GitHub Actions hosted in the stilab-ets organization.
This framework is designed with MLOps principles at its core, ensuring the system is adaptable, scalable, and maintainable:
- Training Flexibility:
- Incremental Learning: The model allows for continuous updates with new data without the need for full retraining (using
partial_fit). - Online Learning: Capable of adapting to codebase evolution in real-time as new commits are pushed.
- Incremental Learning: The model allows for continuous updates with new data without the need for full retraining (using
- Data Collection Modes:
- From Scratch (Full History): Can process the entire Git history to build comprehensive training datasets.
- Just-In-Time (JIT): Optimized for CI/CD to collect metrics only for immediate changes in the current commit.
The pipeline consists of three main components:
- Client Repository (This repo): Contains Terraform code (
.tf) and the workflow configuration (.github/workflows/predict_defects.yml). - Metrics Collector (
stilab-ets/tf-metrics-collector): Analyzes code changes and extracts software metrics. - ML Model (
stilab-ets/tf-ml-defect-model): Uses a pre-trained machine learning model to predict if changed blocks are defect-prone.
The pipeline automatically manages artifacts in your configured S3 Bucket (tf-metrics-storage). This ensures data persistence and model versioning.
s3://tf-metrics-storage/
├── history/
│ └── prediction_history_master.csv # Accumulated global history
├── metrics/
│ ├── metrics_current.csv # Latest JIT metrics
│ └── metrics_history.csv # Historical metrics context
├── predictions/
│ └── predictions_<COMMIT>_<TIME>.csv # Prediction results (Versioned)
└── pre_trained_defect_models/ # ML Models
├── trained_models/
│ └── logisticreg_model.joblib
└── model_features/
Key Features:
- Automatic Versioning: Every prediction run saves a file with the Commit Hash and Timestamp.
- Model Management: Models are downloaded dynamically from
pre_trained_defect_models.
This pipeline uses Just-In-Time (JIT) defect prediction to ensure fast feedback loops.
In predict_defects.yml, we enable JIT mode by passing the current commit hash:
commit_hash: ${{ github.sha }}This tells the Metrics Collector to look only at the lines you just changed.
To ensure process metrics (like Developer Experience or File Stability) are measured effectively in JIT mode, the pipeline downloads metrics_history.csv from S3.
This provides the necessary historical context (e.g., "how many times has this developer changed this file?") without needing to re-process the entire repository history. This hybrid approach combines the speed of JIT with the accuracy of full history.
You need the following GitHub Secrets configured in your repository:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGIONS3_BUCKET(Where models and history are stored)
You can customize the pipeline behavior by editing the workflow file:
| Input | Default | Description |
|---|---|---|
threshold |
0.5 |
Probability threshold (0.0 - 1.0). Higher = Less sensitive, Lower = More sensitive. |
model |
logisticreg_model |
The ML model to use for prediction. |
The actions are maintained in separate repositories. To update or modify the logic:
- Repo: stilab-ets/tf-metrics-collector
- Update: Modify
scripts/collect_metrics.py. - Release: Push to
main. The client will pick up changes automatically if it uses@main.
- Repo: stilab-ets/tf-ml-defect-model
- Update: Retrain models or modify
ml/services/user_training_service.py. - Release: Push to
main.