forked from MindSync-Workspace/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
107 lines (99 loc) · 3.26 KB
/
Jenkinsfile
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
# Jenkinsfile
pipeline {
agent any
environment {
DOCKER_IMAGE = "fastapi-app"
DOCKER_TAG = "${BUILD_NUMBER}"
GCP_VM_IP = credentials('gcp-vm-ip')
GCP_VM_USER = credentials('gcp-vm-user')
SSH_KEY = credentials('gcp-ssh-key')
GITHUB_TOKEN = credentials('github-token')
}
triggers {
// Poll SCM setiap menit untuk perubahan
pollSCM('* * * * *')
// Webhook trigger
githubPush()
}
stages {
stage('Checkout') {
steps {
// Clean workspace sebelum checkout
cleanWs()
checkout scm
}
}
stage('Run Tests') {
steps {
sh '''
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
pip install pytest
pytest tests/
'''
}
}
stage('Build and Push Docker Images') {
steps {
script {
sh '''
docker-compose -f docker/docker-compose.yml build
'''
}
}
}
stage('Deploy to GCP VM') {
steps {
script {
// Update source code di VM
sh """
ssh -i ${SSH_KEY} -o StrictHostKeyChecking=no ${GCP_VM_USER}@${GCP_VM_IP} '
cd /home/${GCP_VM_USER}/fastapi-app && \
git pull origin main && \
docker-compose -f docker/docker-compose.yml down && \
docker-compose -f docker/docker-compose.yml build --no-cache && \
docker-compose -f docker/docker-compose.yml up -d
'
"""
}
}
}
stage('Health Check') {
steps {
script {
sh """
sleep 30
curl -f http://${GCP_VM_IP}:8000/health || exit 1
"""
}
}
}
}
post {
success {
script {
// Update GitHub deployment status
sh """
curl -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d '{"state": "success", "environment": "production"}' \
https://api.github.com/repos/OWNER/REPO/deployments/latest/statuses
"""
}
}
failure {
script {
// Update GitHub deployment status
sh """
curl -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d '{"state": "failure", "environment": "production"}' \
https://api.github.com/repos/OWNER/REPO/deployments/latest/statuses
"""
}
}
}
}