Skip to content

Commit a9c2350

Browse files
committed
Create CD pipeline
1 parent 73b799b commit a9c2350

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

.github/workflows/cd.yml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: 'Continuous Deployment'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
- release/*
9+
10+
jobs:
11+
deployment:
12+
runs-on: ubuntu-latest
13+
environment: dev
14+
env:
15+
branch: main
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Get the branch name
21+
id: get_branch_name
22+
run: |
23+
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v5
27+
with:
28+
python-version: '3.9'
29+
30+
- name: Authenticate to GCP
31+
uses: 'google-github-actions/auth@v2'
32+
with:
33+
credentials_json: '${{ secrets.CD_SA_KEYS }}'
34+
35+
- name: Install dependencies
36+
run: |
37+
pip install -r requirements.txt -r requirements-dev.txt
38+
39+
- name: Run training script
40+
run: |
41+
python train.py
42+
43+
- name: Authenticate Docker to GAR
44+
uses: docker/login-action@v3
45+
with:
46+
registry: '${{ vars.GCP_REGION }}-docker.pkg.dev'
47+
username: _json_key
48+
password: ${{ secrets.CD_SA_KEYS }}
49+
50+
- name: Build and push Docker image
51+
uses: docker/build-push-action@v6
52+
with:
53+
push: true
54+
tags: '${{ vars.GAR_REPOSITORY }}/${{ vars.GAR_IMAGE_NAME }}-${{ steps.get_branch_name.outputs.branch }}'
55+
56+
- name: Deploy the service to Cloud Run
57+
id: 'deploy'
58+
uses: 'google-github-actions/deploy-cloudrun@v2'
59+
with:
60+
service: '${{ vars.GCR_SERVICE_NAME }}-${{ steps.get_branch_name.outputs.branch }}'
61+
image: '${{ vars.GAR_REPOSITORY }}/${{ vars.GAR_IMAGE_NAME }}-${{ steps.get_branch_name.outputs.branch }}'
62+
region: '${{ vars.GCP_REGION }}'
63+
flags: '--allow-unauthenticated'
64+
65+
outputs:
66+
service_url: ${{ steps.deploy.outputs.url }}
67+
68+
stress_test:
69+
runs-on: ubuntu-latest
70+
needs: deployment
71+
72+
steps:
73+
- uses: actions/checkout@v4
74+
75+
- name: Set up Python
76+
uses: actions/setup-python@v5
77+
with:
78+
python-version: '3.9'
79+
80+
- name: Install dependencies
81+
run: |
82+
pip install -r requirements-test.txt
83+
84+
- name: Run stress test
85+
run: |
86+
make stress-test API_URL=${{ needs.deployment.outputs.service_url }}

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ install: ## Install dependencies
2323
pip install -r requirements-test.txt
2424
pip install -r requirements.txt
2525

26-
STRESS_URL = https://delay-model-dpmrk4cwxq-uw.a.run.app
26+
STRESS_URL = $(API_URL)
2727
.PHONY: stress-test
2828
stress-test:
2929
# change stress url to your deployed app

docs/challenge.md

+2
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ The CI workflows focus on running the tests and assesing the quality of the code
133133
The CD workflows focus on training the model, deploying the API and running the stress testing against it. These workflows only run when there's a push to the `main`, `develop` or `release` branches on the repository.
134134
135135
* Undesirable model tracking
136+
* env variables ci/cd
137+
* makefile argument

train.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pandas as pd
2+
3+
from challenge.model import DelayModel
4+
5+
print("Loading data...")
6+
# Read the data
7+
df = pd.read_csv("data/data.csv")
8+
print("-> Data loaded")
9+
10+
# Create the model
11+
model = DelayModel()
12+
13+
print("Preprocessing data...")
14+
# Preprocess the data
15+
X_train, y_train = model.preprocess(df, "delay")
16+
print("-> Preprocessed data")
17+
18+
19+
print("Training model...")
20+
# Train the model
21+
model.fit(X_train, y_train)
22+
print("-> Model trained")
23+
24+
print("Saving model...")
25+
# Store the model
26+
model.save("challenge/tmp/model_checkpoint.pkl")
27+
print("-> Model saved")

0 commit comments

Comments
 (0)