Merge pull request #11 from ahmed-n-abdeltwab/feature/make-modular #13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Train and Release Model | |
on: | |
push: | |
branches: [ main ] | |
workflow_dispatch: | |
env: | |
DOCKER_IMAGE: spyware-detector | |
RELEASE_DIR: release | |
jobs: | |
train-and-release: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
actions: read | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Python 3.9 | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.9' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Install jq | |
run: sudo apt-get install -y jq | |
- name: Build Docker image | |
run: | | |
docker build -t $DOCKER_IMAGE . | |
docker images | |
- name: Prepare release directory | |
run: | | |
mkdir -p ./$RELEASE_DIR/latest | |
chmod -R 777 ./$RELEASE_DIR | |
- name: Run training pipeline | |
run: | | |
docker run --rm \ | |
-v $(pwd)/data:/app/data \ | |
-v $(pwd)/models:/app/models \ | |
-v $(pwd)/$RELEASE_DIR:/app/release \ | |
-e PYTHONPATH=/app \ | |
-u 1001 \ | |
$DOCKER_IMAGE | |
- name: Verify artifacts | |
run: | | |
echo "Generated artifacts in release/latest:" | |
ls -lR ./$RELEASE_DIR/latest | |
REQUIRED_FILES=( | |
"model.pkl" | |
"metadata.json" | |
"metrics.json" | |
"feature_structure.json" | |
) | |
missing_files=0 | |
for file in "${REQUIRED_FILES[@]}"; do | |
if [ ! -f "./$RELEASE_DIR/latest/$file" ]; then | |
echo "Error: Required file $file not found!" | |
missing_files=$((missing_files+1)) | |
fi | |
done | |
if [ $missing_files -gt 0 ]; then | |
echo "Error: Missing $missing_files required files!" | |
docker logs $(docker ps -lq) || true | |
exit 1 | |
fi | |
- name: Create release package | |
run: | | |
echo "Contents of release directory before packaging:" | |
ls -lR ./$RELEASE_DIR | |
TIMESTAMP=$(date +%Y%m%d_%H%M%S) | |
RELEASE_NAME="model_$TIMESTAMP" | |
mkdir -p ./$RELEASE_DIR/temp_package | |
cp -r ./$RELEASE_DIR/latest/* ./$RELEASE_DIR/temp_package/ | |
tar -czvf ./$RELEASE_DIR/$RELEASE_NAME.tar.gz -C ./$RELEASE_DIR/temp_package . | |
rm -rf ./$RELEASE_DIR/temp_package | |
echo "Created package:" | |
ls -l ./$RELEASE_DIR/*.tar.gz | |
- name: Upload artifact | |
if: success() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: model-release | |
path: ${{ env.RELEASE_DIR }}/*.tar.gz | |
if-no-files-found: error | |
- name: Create GitHub Release | |
if: startsWith(github.ref, 'refs/tags/') | |
uses: softprops/action-gh-release@v1 | |
with: | |
name: "Model Release $(date +'%Y-%m-%d')" | |
body: | | |
Automated model training results: | |
- Model: $(jq -r '.model_type' ./$RELEASE_DIR/latest/metadata.json) | |
- Timestamp: $(jq -r '.timestamp' ./$RELEASE_DIR/latest/metadata.json) | |
- Metrics: | |
Accuracy: $(jq -r '.metrics.accuracy' ./$RELEASE_DIR/latest/metrics.json) | |
F1 Score: $(jq -r '.metrics.f1' ./$RELEASE_DIR/latest/metrics.json) | |
files: ./$RELEASE_DIR/*.tar.gz | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |