-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
227 lines (198 loc) · 8.78 KB
/
Copy pathJenkinsfile
File metadata and controls
227 lines (198 loc) · 8.78 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
pipeline {
agent any
options {
timestamps()
timeout(time: 45, unit: 'MINUTES')
disableConcurrentBuilds()
}
environment {
COMPOSE_FILE = 'docker-compose.yml'
ENV_FILE = '.env'
APP_SERVICES = 'redis backend frontend edge-nginx loki alloy grafana'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Prepare Runtime Config') {
steps {
script {
withCredentials([file(credentialsId: 'MY_ENV_FILE', variable: 'ENV_SOURCE_FILE')]) {
sh '''
set -eu
install -m 600 "${ENV_SOURCE_FILE}" "${ENV_FILE}"
'''
}
echo "Refreshed ${env.ENV_FILE} from Jenkins credential: MY_ENV_FILE"
}
}
}
stage('Validate Runtime Config') {
steps {
sh '''
set -eu
set +x
test -f "${ENV_FILE}" || {
echo "Missing ${ENV_FILE}. Provide required runtime env values (RDS/Redis/JWT, etc.)."
exit 1
}
get_env_value() {
key="$1"
awk -F= -v target="$key" '
/^[[:space:]]*#/ { next }
/^[[:space:]]*$/ { next }
{
line=$0
sub(/\r$/, "", line)
pos=index(line, "=")
if (pos == 0) next
current=substr(line, 1, pos - 1)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", current)
if (current == target) {
print substr(line, pos + 1)
found=1
exit
}
}
END {
if (!found) exit 1
}
' "${ENV_FILE}"
}
strip_outer_quotes() {
value="$1"
printf '%s' "$value" | sed -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//"
}
REQUIRED_VARS="DB_URL DB_USERNAME DB_PASSWORD AUTH_JWT_SECRET API_PATH VITE_API_BASE_URL REDIS_HOST REDIS_PORT"
for var in ${REQUIRED_VARS}; do
value="$(get_env_value "${var}" || true)"
value="$(strip_outer_quotes "${value}")"
if [ -z "${value}" ]; then
echo "Missing required env var: ${var}"
exit 1
fi
done
ssl_enabled="$(get_env_value "SSL_ENABLED" || true)"
ssl_enabled="$(strip_outer_quotes "${ssl_enabled}")"
ssl_enabled="$(printf '%s' "${ssl_enabled}" | tr '[:upper:]' '[:lower:]')"
if [ "${ssl_enabled:-false}" = "true" ]; then
ssl_cert_path="$(get_env_value "SSL_CERT_PATH" || true)"
ssl_key_path="$(get_env_value "SSL_KEY_PATH" || true)"
ssl_cert_path="$(strip_outer_quotes "${ssl_cert_path}")"
ssl_key_path="$(strip_outer_quotes "${ssl_key_path}")"
if [ -z "${ssl_cert_path}" ] || [ -z "${ssl_key_path}" ]; then
echo "SSL_ENABLED=true but SSL_CERT_PATH/SSL_KEY_PATH are missing."
exit 1
fi
echo "SSL cert path precheck is skipped. edge-nginx runtime will validate mounted cert files."
fi
'''
}
}
stage('Docker Build') {
steps {
sh '''
docker version
docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" build --pull
'''
}
}
stage('Deploy') {
steps {
sh '''
docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" up -d ${APP_SERVICES}
'''
}
}
stage('Smoke Test') {
steps {
sh '''
api_path="$(docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" exec -T backend printenv API_PATH 2>/dev/null || true)"
api_path="$(printf '%s' "${api_path}" | tr -d '\r')"
if [ -n "${api_path}" ] && [ "${api_path#'/'}" = "${api_path}" ]; then
api_path="/${api_path}"
fi
if [ "${api_path}" != "/" ]; then
api_path="${api_path%/}"
fi
for i in $(seq 1 20); do
if docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" exec -T frontend \
wget -q --spider http://127.0.0.1/ >/dev/null 2>&1; then
readiness_ok=0
if [ -n "${api_path}" ]; then
readiness_body="$(docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" exec -T frontend \
wget -q -O - "http://backend:8080${api_path}/actuator/health/readiness" || true)"
if printf '%s' "${readiness_body}" | grep -Eq '"status"[[:space:]]*:[[:space:]]*"UP"'; then
readiness_ok=1
fi
fi
if [ "${readiness_ok}" -ne 1 ]; then
readiness_body="$(docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" exec -T frontend \
wget -q -O - "http://backend:8080/actuator/health/readiness" || true)"
if printf '%s' "${readiness_body}" | grep -Eq '"status"[[:space:]]*:[[:space:]]*"UP"'; then
readiness_ok=1
fi
fi
if [ "${readiness_ok}" -eq 1 ]; then
echo "Smoke test passed"
exit 0
fi
fi
sleep 5
done
echo "Smoke test failed"
docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" ps
exit 1
'''
}
}
stage('Notification') {
steps {
success {
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend(color: 'good',
message: "빌드 성공: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)",
endpoint: 'https://meeting.ssafy.com/hooks/kip759cnntfw8nr3mpaqqqacgcL',
channel: 'a605_noti_channel'
)
}
}
failure {
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend(color: 'danger',
message: "빌드 실패: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)",
endpoint: 'https://meeting.ssafy.com/hooks/kip759cnntfw8nr3mpaqqqacgc',
channel: 'a605_noti_channel'
)
}
}
}
}
}
post {
always {
script {
if (fileExists(env.ENV_FILE)) {
sh 'docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" ps || true'
} else {
echo "Skipping compose ps in post: ${env.ENV_FILE} not found"
}
}
}
failure {
script {
if (fileExists(env.ENV_FILE)) {
sh 'docker compose -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" logs --tail=100 || true'
} else {
echo "Skipping compose logs in post failure: ${env.ENV_FILE} not found"
}
}
}
}
}