-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJenkinsfile
More file actions
149 lines (134 loc) Β· 5.36 KB
/
Copy pathJenkinsfile
File metadata and controls
149 lines (134 loc) Β· 5.36 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
pipeline {
agent any
environment {
DISCORD_WEBHOOK = credentials('discord-webhook')
DB_DRIVER = credentials('DB_DRIVER')
DB_HOST = credentials('DB_HOST')
DB_PORT = credentials('DB_PORT')
DB_NAME = credentials('DB_NAME')
DB_USERNAME = credentials('DB_USERNAME')
DB_PASSWORD = credentials('DB_PASSWORD')
JWT_SECRET = credentials('JWT_SECRET')
ACCESS_TOKEN_EXPIRE_TIME = credentials('ACCESS_TOKEN_EXPIRE_TIME')
REFRESH_TOKEN_EXPIRE_TIME = credentials('REFRESH_TOKEN_EXPIRE_TIME')
IMAGE_NAME = 'homeaid-backend'
IMAGE_TAG = 'latest'
}
tools {
jdk 'OpenJDK17'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Set Variables') {
steps {
script {
def author = sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim()
def email = sh(script: "git log -1 --pretty=format:'%ae'", returnStdout: true).trim()
def message = sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim()
def hash = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
env.BUILD_USER = "${author} (${email})"
env.GIT_COMMIT_MSG = message
env.GIT_COMMIT_HASH = hash
echo "π Commit by: ${env.BUILD_USER}"
echo "π Message: ${env.GIT_COMMIT_MSG}"
echo "π Hash: ${env.GIT_COMMIT_HASH}"
}
}
}
stage('Build and Test') {
steps {
sh 'chmod +x ./gradlew'
withEnv([
"DB_DRIVER=${DB_DRIVER}",
"DB_HOST=${DB_HOST}",
"DB_PORT=${DB_PORT}",
"DB_NAME=${DB_NAME}",
"DB_USERNAME=${DB_USERNAME}",
"DB_PASSWORD=${DB_PASSWORD}",
"JWT_SECRET=${JWT_SECRET}",
"ACCESS_TOKEN_EXPIRE_TIME=${ACCESS_TOKEN_EXPIRE_TIME}",
"REFRESH_TOKEN_EXPIRE_TIME=${REFRESH_TOKEN_EXPIRE_TIME}"
]) {
sh './gradlew clean build'
}
}
}
stage('Stop and Remove Backend Container & Image') {
when {
expression { env.BRANCH_NAME == 'dev' }
}
steps {
sh """
docker stop backend-app || true
docker rm backend-app || true
IMAGE_ID=\$(docker images -q ${IMAGE_NAME}:${IMAGE_TAG})
if [ ! -z "\$IMAGE_ID" ]; then
docker rmi \$IMAGE_ID
else
echo "μ΄λ―Έμ§ μμ β μμ μλ΅"
fi
echo "π§½ Dangling μ΄λ―Έμ§ μ 리"
DANGLING_IDS=\$(docker images -f "dangling=true" -q)
if [ ! -z "\$DANGLING_IDS" ]; then
docker rmi \$DANGLING_IDS || true
else
echo "Dangling μ΄λ―Έμ§ μμ"
fi
"""
}
}
stage('Build & Run via Docker Compose') {
when {
expression { env.BRANCH_NAME == 'dev' }
}
steps {
sh """
docker-compose -f docker-compose/docker-compose.yml -f docker-compose/docker-compose.backend.yml build backend
docker-compose -f docker-compose/docker-compose.yml -f docker-compose/docker-compose.backend.yml up -d backend
"""
}
}
}
post {
success {
script {
def message = """{
"embeds": [{
"title": "β
CI/CD μ±κ³΅",
"description": "**π¦ Repository:** `${env.JOB_NAME}`\\n**πΏ Branch:** `${env.BRANCH_NAME}`\\n**π§ Commit by:** `${env.BUILD_USER}`\\n**π Message:** ${env.GIT_COMMIT_MSG}\\n[π Jenkins λ‘κ·Έ νμΈνκΈ°](${env.BUILD_URL})",
"color": 5763719
}],
"content": "β
CI/CD μλ£: `${env.BRANCH_NAME}` λΈλμΉμ
λλ€!"
}"""
sh """
curl -H "Content-Type: application/json" \
-X POST \
-d '${message}' \
${DISCORD_WEBHOOK}
"""
}
}
failure {
script {
def message = """{
"embeds": [{
"title": "β CI/CD μ€ν¨",
"description": "**π¦ Repository:** `${env.JOB_NAME}`\\n**πΏ Branch:** `${env.BRANCH_NAME}`\\n**π§ Commit by:** `${env.BUILD_USER}`\\n**π Message:** ${env.GIT_COMMIT_MSG}\\n[π Jenkins λ‘κ·Έ νμΈνκΈ°](${env.BUILD_URL})",
"color": 16711680
}],
"content": "β μ€λ₯ λ°μ: `${env.BRANCH_NAME}` λΈλμΉ νμΈ μλ§!"
}"""
sh """
curl -H "Content-Type: application/json" \
-X POST \
-d '${message}' \
${DISCORD_WEBHOOK}
"""
}
}
}
}