-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
131 lines (115 loc) · 5.03 KB
/
Jenkinsfile
File metadata and controls
131 lines (115 loc) · 5.03 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
pipeline {
agent any
environment {
DOCKER_REPOSITORY = 'underthekey/sentence-api'
REGISTRY_CREDENTIAL = 'underthekey-docker-hub'
dockerImage = ''
DEPLOY_URL = 'https://sentence.underthekey.com'
}
parameters {
string(name: 'DOCKER_NETWORK', defaultValue: 'proxy-network', description: 'docker network name')
string(name: 'IMAGE_NAME', defaultValue: 'sentence-api', description: 'docker image name')
choice(name: 'ENV_TYPE', choices: ['dev', 'prod'], description: 'Choose the environment type')
}
stages {
stage('environment setup') {
steps {
script {
env.BRANCH_NAME = params.ENV_TYPE == 'prod' ? 'main' : 'dev'
}
}
}
stage('git clone') {
steps {
git branch: "${env.BRANCH_NAME}",
credentialsId: 'github-token',
url: 'https://github.com/underthekey/sentence-api'
}
}
stage('jar build') {
steps {
sh 'chmod +x ./gradlew'
sh './gradlew clean bootJar'
}
}
stage('image build & docker-hub push') {
steps {
script {
docker.withRegistry('', REGISTRY_CREDENTIAL) {
sh 'docker buildx create --use --name mybuilder'
sh "docker buildx build --platform linux/amd64 -t $DOCKER_REPOSITORY:$BUILD_NUMBER --push ."
sh "docker buildx build --platform linux/amd64 -t $DOCKER_REPOSITORY:latest --push ."
}
}
}
}
stage('previous docker rm') {
steps {
sshagent(credentials: ['udtk-ubuntu']) {
sh """
ssh -o StrictHostKeyChecking=no ${UDTK_SERVER_ACCOUNT}@${UDTK_SERVER_IP} '
docker ps -q --filter "name=sentence-api" | xargs -r docker stop
docker ps -aq --filter "name=sentence-api" | xargs -r docker rm -f
docker images ${DOCKER_REPOSITORY}:${env.IMAGE_NAME}-latest -q | xargs -r docker rmi
'
"""
}
}
}
stage('docker-hub pull') {
steps {
sshagent(credentials: ['udtk-ubuntu']) {
sh """
ssh -o StrictHostKeyChecking=no ${UDTK_SERVER_ACCOUNT}@${UDTK_SERVER_IP} 'docker pull ${DOCKER_REPOSITORY}:latest'
"""
}
}
}
stage('service start') {
steps {
withCredentials([file(credentialsId: 'udtk-sentence-api-credentials', variable: 'ENV_CREDENTIALS')]) {
sshagent(credentials: ['udtk-ubuntu']) {
sh """
scp -o StrictHostKeyChecking=no $ENV_CREDENTIALS ${UDTK_SERVER_ACCOUNT}@${UDTK_SERVER_IP}:~/udtk-sentence-api-credentials
"""
sh """
ssh -o StrictHostKeyChecking=no ${UDTK_SERVER_ACCOUNT}@${UDTK_SERVER_IP} '
SERVER_PORT=\$(grep SERVER_PORT ~/udtk-sentence-api-credentials | cut -d "=" -f2)
docker run -i -e TZ=Asia/Seoul --env-file ~/udtk-sentence-api-credentials \\
--name ${params.IMAGE_NAME} --network ${params.DOCKER_NETWORK} \\
-p \${SERVER_PORT}:\${SERVER_PORT} \\
--restart unless-stopped \\
-d ${DOCKER_REPOSITORY}:latest
rm -f ~/udtk-sentence-api-credentials
'
"""
}
}
}
}
stage('service test & alert send') {
steps {
sh """
#!/bin/bash
for retry_count in \$(seq 20)
do
if curl -s "${DEPLOY_URL}/ping" > /dev/null
then
echo "Build Success!"
curl -d '{"title":"sentence-api ${env.BRANCH_NAME} release:$BUILD_NUMBER","body":"Deployment Succeeded🚀"}' -H "Content-Type: application/json" -X POST ${PUSH_ALERT}
exit 0
fi
if [ \$retry_count -eq 20 ]
then
echo "Build Failed!"
curl -d '{"title":"sentence-api ${env.BRANCH_NAME} release:$BUILD_NUMBER","body":"Deployment Failed😢"}' -H "Content-Type: application/json" -X POST ${PUSH_ALERT}
exit 1
fi
echo "The server is not alive yet. Retry health check in 5 seconds..."
sleep 5
done
"""
}
}
}
}