-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
184 lines (163 loc) · 6.07 KB
/
Jenkinsfile
File metadata and controls
184 lines (163 loc) · 6.07 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
pipeline {
agent{
label 'slave2'
}
parameters {
booleanParam(name: 'BUILD_FRONTEND', defaultValue: true, description: 'Build the frontend project')
booleanParam(name: 'BUILD_BACKEND', defaultValue: true, description: 'Build the backend project')
}
environment {
FRONTEND_DIR = "${WORKSPACE}/front"
BACKEND_DIR = "backend"
IMAGE_NAME = "react-app"
CONTAINER_NAME = "react-app-1"
PORT = "3000"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Frontend') {
when {
expression { params.BUILD_FRONTEND }
}
stages {
stage('Setup Environment') {
steps {
script {
// 현재 작업 디렉토리 확인
sh "pwd"
sh "ls"
// front 디렉토리 존재 확인 및 내용 리스트
//sh "ls -la ${FRONTEND_DIR}"
// 환경 파일 복사
withCredentials([file(credentialsId: 'react-env-file', variable: 'ENV_FILE')]) {
//sh "cp \$ENV_FILE ${FRONTEND_DIR}/.env"
// 복사 후 확인
//sh "ls -la ${FRONTEND_DIR}/.env"
sh '''
cp \$ENV_FILE ${FRONTEND_DIR}/.env
ls -la ${FRONTEND_DIR}/.env
'''
}
}
}
}
stage('Install Dependencies') {
steps {
dir("${FRONTEND_DIR}") {
sh 'npm install'
}
}
}
stage('Build') {
steps {
dir("${FRONTEND_DIR}") {
sh 'npm run build'
}
}
}
// stage('Test') {
// steps {
// dir("${FRONTEND_DIR}") {
// sh 'npm test'
// }
// }
// }
stage('Build Docker Image') {
steps {
script {
// Building Docker image for the React app
sh '''
ls ${FRONTEND_DIR}
docker build -t ${IMAGE_NAME} ${FRONTEND_DIR}
'''
}
}
}
stage('Deploy to Docker') {
steps {
script {
// Stopping any existing container if it's running
sh '''
docker ps -q --filter "name=${CONTAINER_NAME}" | grep -q . && docker stop ${CONTAINER_NAME} || true
docker ps -aq --filter "name=${CONTAINER_NAME}" | grep -q . && docker rm ${CONTAINER_NAME} || true
'''
// Running the Docker container for the React app
sh '''
docker run -d --name ${CONTAINER_NAME} -p ${PORT}:${PORT} ${IMAGE_NAME}
'''
}
}
}
}
}
stage('Backend') {
when {
expression { params.BUILD_BACKEND }
}
stages {
stage('Setup Environment') {
steps {
dir("${BACKEND_DIR}") {
script {
withCredentials([file(credentialsId: 'backend-env-file', variable: 'ENV_FILE')]) {
sh "cp \$ENV_FILE .env"
}
}
}
}
}
stage('Install Dependencies') {
steps {
dir("${BACKEND_DIR}") {
sh 'npm install' // 또는 해당 백엔드의 의존성 설치 명령어
}
}
}
stage('Build') {
steps {
dir("${BACKEND_DIR}") {
sh 'npm run build' // 또는 해당 백엔드의 빌드 명령어
}
}
}
stage('Test') {
steps {
dir("${BACKEND_DIR}") {
sh 'npm test' // 또는 해당 백엔드의 테스트 명령어
}
}
}
stage('Deploy') {
steps {
dir("${BACKEND_DIR}") {
sh 'echo "Deploying backend..."'
// 실제 백엔드 배포 명령어를 여기에 추가하세요
}
}
}
}
}
}
post {
success {
echo 'Pipeline succeeded! Selected projects have been built and deployed.'
}
failure {
echo 'Pipeline failed. Please check the logs for errors.'
}
always {
script {
if (params.BUILD_FRONTEND) {
sh "rm -f ${FRONTEND_DIR}/.env"
}
if (params.BUILD_BACKEND) {
sh "rm -f ${BACKEND_DIR}/.env"
}
}
}
}
}