Repository navigation
Expand file tree
/
Copy pathJenkinsfile
More file actions
120 lines (101 loc) · 4.66 KB
/
Copy pathJenkinsfile
File metadata and controls
120 lines (101 loc) · 4.66 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
pipeline {
agent any
environment {
// DOCKER_HUB_CREDENTIALS = credentials('docker-hub-credentials')
// DOCKER_BACKEND_IMAGE_NAME = "bsh7937/andante-backend"
// DOCKER_FRONTEND_IMAGE_NAME = "bsh7937/andante-frontend"
// DOCKER_IMAGE_TAG = "${env.BUILD_NUMBER}"
DOCKER_COMPOSE_DIR = "/home/ubuntu"
BUILD_NUMBER = "${env.BUILD_NUMBER}"
FRONTEND_ENV = credentials('frontend-env')
PYTHON_ENV = credentials('python-env')
}
stages {
stage('Checkout') {
steps {
checkout scm
sh 'chmod +x ./Back/gradlew'
}
}
// stage('Test') {
// steps {
// dir('Back') {
// sh './gradlew test'
// }
// }
// post {
// always {
// junit '**/build/test-results/test/*.xml'
// }
// }
// }
stage('Build & Deploy') {
steps {
script {
// 백엔드 빌드
dir('Back') {
sh './gradlew clean bootJar'
}
// 프론트엔드 빌드
dir('Front/Andante-Front') {
sh 'cp $FRONTEND_ENV .env'
sh 'npm install'
sh 'CI=false npm run build'
}
// EC2로 파일 전송 및 배포
sshagent(credentials: ['ubuntu_ssh_key']) {
sh 'ssh-keyscan -H i12a305.p.ssafy.io >> ~/.ssh/known_hosts'
// 디렉토리 생성
sh '''
ssh ubuntu@i12a305.p.ssafy.io "mkdir -p ~/back ~/front ~/python"
'''
// Dockerfile 전송
sh '''
scp Back/Dockerfile ubuntu@i12a305.p.ssafy.io:~/back/
scp Front/Andante-Front/Dockerfile ubuntu@i12a305.p.ssafy.io:~/front/
scp Python/Dockerfile ubuntu@i12a305.p.ssafy.io:~/python/
'''
// 백엔드 JAR 파일 전송
sh '''
scp Back/build/libs/*.jar ubuntu@i12a305.p.ssafy.io:~/back/app.jar
'''
// 프론트엔드 파일 전송
sh '''
ssh ubuntu@i12a305.p.ssafy.io "rm -rf ~/front/dist ~/front/src && mkdir -p ~/front/dist ~/front/src"
scp Front/Andante-Front/Dockerfile ubuntu@i12a305.p.ssafy.io:~/front/
ssh ubuntu@i12a305.p.ssafy.io "sudo chown -R ubuntu:ubuntu ~/front"
scp -r Front/Andante-Front/dist/* ubuntu@i12a305.p.ssafy.io:~/front/dist/
scp -r Front/Andante-Front/src/assets ubuntu@i12a305.p.ssafy.io:~/front/src/
scp $FRONTEND_ENV ubuntu@i12a305.p.ssafy.io:~/front/.env
ssh ubuntu@i12a305.p.ssafy.io "sudo chmod -R 755 ~/front"
'''
// Python 파일 전송
sh '''
ssh ubuntu@i12a305.p.ssafy.io "chmod -R 755 ~/python"
scp -r Python/* ubuntu@i12a305.p.ssafy.io:~/python/
scp $PYTHON_ENV ubuntu@i12a305.p.ssafy.io:~/python/.env
'''
// 도커 컴포즈 실행
sh '''
ssh ubuntu@i12a305.p.ssafy.io <<EOF
cd ${DOCKER_COMPOSE_DIR}
docker-compose down
docker rmi -f $(docker images -q 'ubuntu-*') 2>/dev/null || true
docker rmi \$(docker images -f "dangling=true" -q) 2>/dev/null || true
docker-compose build --no-cache
docker-compose up -d
docker system prune -af --volumes
exit
EOF
'''
}
}
}
}
}
post {
always {
cleanWs()
}
}
}