-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
170 lines (150 loc) ยท 6.02 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
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
pipeline {
agent any
environment {
DOCKER_HUB_NAMESPACE = "zlxldgus123"
DOCKER_TAG = "latest"
DEPLOY_DIR = "/home/ubuntu/deploy"
GIT_BRANCH = "back/develop"
}
stages {
stage('Git Clone') {
steps {
script {
git branch: "${GIT_BRANCH}", credentialsId: 'gitlab_login', url: 'https://lab.ssafy.com/s11-webmobile1-sub2/S11P12E202.git'
}
}
}
stage('Show Git Branch') {
steps {
script {
def branch = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
echo "Current Git Branch: ${branch}"
}
}
}
stage('Show Directory Structure') {
steps {
script {
sh 'find .'
}
}
}
stage('Show Dockerfiles') {
steps {
script {
def services = ['battle', 'user', 'multi']
for (service in services) {
def dockerfilePath = "backend/${service}/Dockerfile"
def dockerfileExists = fileExists(dockerfilePath)
if (dockerfileExists) {
echo "Dockerfile for ${service} exists, displaying content."
sh "cat ${dockerfilePath}"
} else {
echo "Dockerfile for ${service} does not exist, skipping."
}
}
}
}
}
stage('Show Docker Compose File') {
steps {
script {
def dockerComposeFilePath = "${WORKSPACE}/docker-compose.yml"
def dockerComposeFileExists = fileExists(dockerComposeFilePath)
if (dockerComposeFileExists) {
echo "docker-compose.yml exists, displaying content."
sh "cat ${dockerComposeFilePath}"
} else {
echo "docker-compose.yml does not exist."
}
}
}
}
stage('Build Docker Images and Push') {
steps {
script {
def services = ['problem', 'user', 'multi']
for (service in services) {
def image = "${DOCKER_HUB_NAMESPACE}/${service}:${DOCKER_TAG}"
def dockerfilePath = "backend/${service}/Dockerfile"
// Check if Dockerfile exists
def dockerfileExists = fileExists(dockerfilePath)
if (dockerfileExists) {
echo "Dockerfile for ${service} exists, proceeding with build and push."
withCredentials([usernamePassword(credentialsId: 'dockerhub_credentials', usernameVariable: 'DOCKER_HUB_USERNAME', passwordVariable: 'DOCKER_HUB_PASSWORD')]) {
sh """
docker build -t ${image} -f ${dockerfilePath} backend/${service}
docker login -u \$DOCKER_HUB_USERNAME -p \$DOCKER_HUB_PASSWORD
docker push ${image}
"""
}
} else {
echo "Dockerfile for ${service} does not exist, skipping."
}
}
}
}
}
stage('Deploy') {
steps {
script {
// Docker Compose ์ค์น ์ฌ๋ถ ํ์ธ ๋ฐ ์ค์น
sh """
export PATH=\$PATH:/usr/local/bin
if ! command -v docker-compose &> /dev/null
then
echo "docker-compose could not be found. Installing..."
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-\$(uname -s)-\$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
else
echo "docker-compose is already installed."
fi
"""
// DEPLOY_DIR ๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ฑํ๊ณ ํ์ผ ๋ชฉ๋ก ํ์ธ
sh """
echo "Current User: \$(whoami)"
if [ ! -d "${DEPLOY_DIR}" ]; then
mkdir -p ${DEPLOY_DIR}
fi
cd ${DEPLOY_DIR}
# ๋ฆฌํฌ์งํ ๋ฆฌ์์ ๊ฐ์ ธ์จ docker-compose.yml ํ์ผ ๋ณต์ฌ
cp ${WORKSPACE}/docker-compose.yml ${DEPLOY_DIR}/
echo "Directory Contents:"
ls -al
if [ -f "docker-compose.yml" ]; then
echo "docker-compose.yml exists."
cat docker-compose.yml
else
echo "docker-compose.yml does not exist."
exit 1
fi
docker-compose down
docker-compose up --build -d
"""
}
}
}
stage('Docker Cleanup') {
steps {
script {
sh """
echo "Cleaning up old Docker images..."
docker images --filter "dangling=false" --filter "reference=${DOCKER_HUB_NAMESPACE}/*" --format "{{.ID}}:{{.Tag}}" | grep -v ":${DOCKER_TAG}" | awk -F ':' '{print \$1}' | xargs -r docker rmi
"""
}
}
}
}
post {
always {
echo 'Cleaning up...'
cleanWs()
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}