forked from Xeat-KEA/user-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
81 lines (74 loc) · 2.71 KB
/
Jenkinsfile
File metadata and controls
81 lines (74 loc) · 2.71 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
pipeline {
agent any
tools {
gradle 'gradle'
}
environment {
IMAGE_NAME = "hurraypersimmon/codingtext"
IMAGE_TAG = "userservice"
APP_NAME = "ct-userservice-app"
TARGET_HOST = "s112@172.16.211.112"
SSH_CREDENTIALS = "jenkins_private_key"
ACTIVE_PROFILE = "prod"
CONFIG_SERVER_URL = "172.16.211.110:9000"
}
stages {
stage('Checkout') {
steps {
git branch: 'main',
credentialsId: 'github_access_token',
url: 'https://github.com/Xeat-KEA/user-service.git'
}
}
stage('Build Gradle Project') {
steps {
sh '''
echo 'gradlew 빌드 시작'
chmod +x ./gradlew
./gradlew clean build
'''
}
}
stage('Build and Push Docker Image') {
steps {
script {
sh "docker build -t ${IMAGE_NAME}:${IMAGE_TAG} ."
withCredentials([usernamePassword(credentialsId: 'ct-dockerhub', usernameVariable: 'DOCKERHUB_USERNAME', passwordVariable: 'DOCKERHUB_PASSWORD')]) {
docker.withRegistry('https://index.docker.io/v1/', 'ct-dockerhub') {
sh '''
echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
docker push "${IMAGE_NAME}:${IMAGE_TAG}"
'''
}
}
}
}
}
stage('Deploy to VM') {
steps {
script {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
docker system prune -a -f
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} '
docker pull ${IMAGE_NAME}:${IMAGE_TAG}
docker stop ${APP_NAME} || true
docker rm ${APP_NAME} || true
docker run -d --restart always --network host --name ${APP_NAME} \
--env ACTIVE_PROFILE=${ACTIVE_PROFILE} \
--env CONFIG_SERVER_URL=${CONFIG_SERVER_URL} \
${IMAGE_NAME}:${IMAGE_TAG}
docker system prune -a -f
'
"""
}
}
}
}
}
post {
always {
cleanWs() // 빌드 후 작업 공간 정리
}
}
}