-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.gitlab-ci.yml
155 lines (137 loc) · 5.45 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Made by: Henrik Lagrosen, Omid Khodaparast
# ========================= VARIABLES ======================== #
variables:
# The path to our k8n agent
KUBE_CONTEXT: "courses/dit826/2024/group1:fraud-detection"
# ========================== STAGES ========================== #
stages:
- pre-build-check
- versioning
- build
- test
- deploy
# =========================== JOBS =========================== #
# --------------------------- pre-build-check --------------------------- #
# Checks that the environment.yml was manually updated
env_test:
stage: pre-build-check
tags:
- docker
script:
- |
if grep -q "ID-986429187489" environment.yml; then
echo "No problems found";
else
echo "Changes to the environment.yml need to be done manually, else resolving it will be to slow";
exit 1;
fi
# -------------------------- version ------------------------- #
version_new_model:
# Use docker runner
tags:
- docker
# Use the latest version of Ubuntu as the base image
image: ubuntu:latest
# This job is part of the versioning stage
stage: versioning
rules:
# This job runs only for merge requests and if the target brach is main
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
# A new model is detected if changes are made to the db_setup or ML_pipeline folders
changes:
- db_setup/**/*
- fraud_detection/detector/services/ml_pipeline/**/*
- when: never
# Install required tools
before_script:
- apt-get update && apt-get install -y curl jq
script:
- |
echo "New model detected"
echo "Current model version: ${LATEST_MODEL_VERSION}"
# Extract the current model version
V=$(echo "${LATEST_MODEL_VERSION}" | cut -d. -f1) # Extract V from LATEST_MODEL_VERSION
NEW_VERSION=$((V + 1)).0
# Update the variable via the GitLab API
curl --header "PRIVATE-TOKEN: ${GITLAB_ACCESS_TOKEN}" \
--request PUT \
--form "value=${NEW_VERSION}" \
"https://git.chalmers.se/api/v4/projects/${CI_PROJECT_ID}/variables/LATEST_MODEL_VERSION"
# Confirm that the new version was updated
UPDATED_VERSION=$(curl --header "PRIVATE-TOKEN: ${GITLAB_ACCESS_TOKEN}" \
"https://git.chalmers.se/api/v4/projects/${CI_PROJECT_ID}/variables/LATEST_MODEL_VERSION" | jq -r '.value')
echo "Updated model version: ${UPDATED_VERSION}"
# Create and push the new tag
echo "Creating new tag"
curl --request POST \
--header "PRIVATE-TOKEN: ${GITLAB_ACCESS_TOKEN}" \
--url "https://git.chalmers.se/api/v4/projects/${CI_PROJECT_ID}/repository/tags" \
--data "tag_name=${UPDATED_VERSION}" \
--data "ref=${CI_COMMIT_BRANCH}" \
--data "message=New model version: ${UPDATED_VERSION}"
# --------------------------- build -------------------------- #
# NOTE - inspired by https://docs.gitlab.com/ee/user/packages/container_registry/build_and_push_images.html
build_docker_image:
stage: build
image: docker:latest
tags:
- docker-build # Tag for gitlab-runner that can build images
services:
- docker:dind # dind service for building the image in the runners container
script:
# Logs in to the GitLab Container Registry using standard environment variables
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
# Buildes the image for right platform
- |
DOCKER_BUILDKIT=1 docker build --platform linux/amd64 \
-t "$CI_REGISTRY_IMAGE/backend:latest" \
--build-arg VERSION_TAG=${LATEST_MODEL_VERSION} \
-f ./fraud_detection/dockerfile .
# Pushes the image to the GitLab Container Registry
- docker push "$CI_REGISTRY_IMAGE/backend:latest"
rules:
# Only runs for pushes to main
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
# ... and if the fraud_detection folder has changed
changes:
- fraud_detection/**/*
- gx/**/*
- when: never
# --------------------------- test --------------------------- #
run_unit_tests:
stage: test
image: continuumio/miniconda3:latest
tags:
- docker
before_script:
- conda init
- source ~/.bashrc
- conda env create -f environment.yml
- conda activate prj
script:
- cd fraud_detection
- python3 manage.py test --noinput
# -------------------------- deploy -------------------------- #
deploy_to_k8s:
tags:
- docker
stage: deploy
image:
name: bitnami/kubectl:latest # Image that has the kubectl tool
entrypoint: [""]
script:
- kubectl config use-context $KUBE_CONTEXT # Sets the context to the correct cluster connection
- kubectl apply -f deployment.yml # Applies the deployment file incase it has changed
# This forces the pods to restart with the new image. Not a good practice but otherwise it will
# try to spawn another pod with the new image. That will cause our storage usage to be exceeded
# so it will freeze, which is just awesome. This causes a tiny amount of downtime, no big deal.
- kubectl delete pods -l app=backend
rules:
# Only runs for pushes to main
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
# ... and if the fraud_detection folder or the deployment configuration has changed
changes:
- fraud_detection/**/*
- gx/**/*
- deployment.yml
- when: never