-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
103 lines (98 loc) · 3.62 KB
/
.gitlab-ci.yml
File metadata and controls
103 lines (98 loc) · 3.62 KB
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
stages:
- build
- deploy
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE/python-app:$CI_COMMIT_SHA
DOCKER_LATEST: $CI_REGISTRY_IMAGE/python-app:latest
HELM_EXPERIMENTAL_OCI: 1
KUBERNETES_NAMESPACE: "python-app"
build-docker:
stage: build
image: docker:24.0.0
services:
- docker:24.0.0-dind
variables:
DOCKER_TLS_CERTDIR: ""
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_VERIFY: 0
before_script:
- echo "Building Docker image for commit $CI_COMMIT_SHA"
- rm -rf /root/.docker
- export DOCKER_HOST=tcp://docker:2375
- export DOCKER_TLS_VERIFY=""
- export DOCKER_TLS_CERTDIR=""
- export DOCKER_CERT_PATH=""
- unset DOCKER_TLS_VERIFY
- unset DOCKER_TLS_CERTDIR
- unset DOCKER_CERT_PATH
- until docker --host=tcp://docker:2375 version; do echo "Waiting for Docker daemon..."; sleep 2; done
- echo $CI_REGISTRY_PASSWORD | docker --host=tcp://docker:2375 login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
script:
- cd code/
- echo "Building Python Flask application Docker image..."
- docker --host=tcp://docker:2375 build -t $DOCKER_IMAGE -t $DOCKER_LATEST .
- echo "Pushing images to GitLab Container Registry..."
- docker --host=tcp://docker:2375 push $DOCKER_IMAGE
- docker --host=tcp://docker:2375 push $DOCKER_LATEST
- echo "Docker images pushed successfully!"
rules:
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_MERGE_REQUEST_ID
tags:
- k3s
deploy-to-k3s:
stage: deploy
image: alpine/helm:3.12.0
before_script:
- echo "Preparing deployment to K3s cluster..."
- apk add --no-cache curl bash
- curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
- chmod +x kubectl
- mv kubectl /usr/local/bin/
- mkdir -p ~/.kube
- echo "$KUBECONFIG_CONTENT" | base64 -d > ~/.kube/config
- chmod 600 ~/.kube/config
- kubectl cluster-info
- kubectl get nodes
- kubectl create namespace $KUBERNETES_NAMESPACE --dry-run=client -o yaml | kubectl apply -f -
- kubectl create secret docker-registry gitlab-registry-secret
--docker-server=$CI_REGISTRY
--docker-username=$CI_REGISTRY_USER
--docker-password=$CI_REGISTRY_PASSWORD
--docker-email=$GITLAB_USER_EMAIL
--namespace=$KUBERNETES_NAMESPACE
--dry-run=client -o yaml | kubectl apply -f -
script:
- echo "Starting deployment process..."
- echo "Deploying PostgreSQL database..."
- helm upgrade --install postgres ./infra/postgres/
--namespace $KUBERNETES_NAMESPACE
--wait
--timeout=10m
--create-namespace
- echo "Waiting for PostgreSQL to be ready..."
- kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=postgres -n $KUBERNETES_NAMESPACE --timeout=300s
- echo "Deploying Python Flask application..."
- helm upgrade --install python-app ./chart/
--namespace $KUBERNETES_NAMESPACE
--set image.tag=$CI_COMMIT_SHA
--set image.repository=$CI_REGISTRY_IMAGE/python-app
--wait
--timeout=10m
--create-namespace
- echo "Checking deployment status..."
- kubectl get pods -n $KUBERNETES_NAMESPACE
- kubectl get services -n $KUBERNETES_NAMESPACE
- echo "Application will be available at:"
- echo "http://***.***.***.***:30080 (Master Node)"
- echo "http://***.***.***.***:30080 (Worker Node 1)"
- echo "http://***.***.***.***:30080 (Worker Node 2)"
environment:
name: production
url: http://***.***.***.***:30080
rules:
- if: $CI_COMMIT_BRANCH == "main"
tags:
- k3s
dependencies:
- build-docker