-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_jenkins
More file actions
149 lines (131 loc) · 5.27 KB
/
back_jenkins
File metadata and controls
149 lines (131 loc) · 5.27 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
pipeline {
agent any
environment {
AWS_REGION = 'ap-northeast-2'
ACCOUNT_ID = '273354621375'
ECR_URI = "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
APP_NAME = 'spring-app'
BLUE_IP = '172.31.55.206' // Blue EC2 (Amazon Linux)
GREEN_IP = '172.26.11.74' // Green EC2 (Ubuntu)
LB_IP = '172.31.35.103' // Nginx LB EC2
/* ─────────────── 임시로 Green 비활성화 ───────────────
► 피어링 완료되면 false 로만 바꾸면 됩니다 */
GREEN_DISABLED = 'true'
}
stages {
/* ─────────────── 소스 체크아웃 ─────────────── */
stage('Checkout') {
steps { checkout scm }
}
/* ─────────────── 변수 계산 ─────────────── */
stage('Prepare Variables') {
steps {
script {
def isGreenBranch = (env.BRANCH_NAME == 'develop-be')
def color = isGreenBranch ? 'green' : 'blue'
env.TAG = "${env.BUILD_NUMBER}-${color}"
echo "📦 Branch=${env.BRANCH_NAME}, DeployColor=${color}, TAG=${env.TAG}"
}
}
}
/* ─────────────── JAR 빌드 ─────────────── */
stage('Build JAR') {
steps {
dir('backend') {
sh '''
chmod +x gradlew
./gradlew clean bootJar
'''
}
}
}
/* ─────────────── Buildx 준비 ─────────────── */
stage('Setup Buildx') {
steps {
sh '''
docker buildx create --name multi-builder --driver docker-container --use || true
docker buildx inspect multi-builder --bootstrap
'''
}
}
/* ─────────────── 이미지 빌드 & 푸시 ─────────────── */
stage('Build & Push Image') {
steps {
dir('backend') {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws key'
]]) {
sh """
aws ecr get-login-password --region $AWS_REGION | \
docker login --username AWS --password-stdin $ECR_URI
docker buildx build \
--platform linux/amd64,linux/arm64 \
--provenance=false \
-t $ECR_URI/$APP_NAME:\$TAG \
--push .
"""
}
}
}
}
/* ─────────────── 대상 서버 배포 ─────────────── */
stage('Deploy to Target') {
steps {
script {
boolean greenOff = (env.GREEN_DISABLED == 'true')
boolean isGreenBranch = (env.BRANCH_NAME == 'develop-be')
// ▸ Green 이 꺼져있으면 develop‑be 도 Blue 로 보냄
def targetIP = (!greenOff && isGreenBranch) ? env.GREEN_IP : env.BLUE_IP
def sshCred = (!greenOff && isGreenBranch) ? 'green-ssh' : 'blue-ec2-ssh'
def sshUser = (!greenOff && isGreenBranch) ? 'ubuntu' : 'ec2-user'
def composeFile = (!greenOff && isGreenBranch)
? '/opt/green/docker-compose.green.yml'
: '/opt/blue/docker-compose.blue.yml'
/* ▼ 환경파일 동기화 블록 추가 ▼ */
configFileProvider([configFile(fileId: 'blue-env', variable: 'ENV_FILE')]) {
sshagent([sshCred]) {
sh """
scp -o StrictHostKeyChecking=no \$ENV_FILE ${sshUser}@${targetIP}:/opt/blue/blue.env
ssh -o StrictHostKeyChecking=no ${sshUser}@${targetIP} <<'EOF'
aws ecr get-login-password --region ${AWS_REGION} | \
docker login --username AWS --password-stdin ${ECR_URI}
docker pull ${ECR_URI}/${APP_NAME}:${TAG}
docker compose -f ${composeFile} up -d
EOF
"""
}
}
}
}
/* ─────────────── LB 트래픽 스위치 ─────────────── */
stage('Switch Traffic') {
when {
allOf {
not { environment name: 'GREEN_DISABLED', value: 'true' } // Green 살아있을 때만
anyOf { branch 'develop-be'; branch 'main' }
}
}
steps {
script {
def isGreenBranch = (env.BRANCH_NAME == 'develop-be')
def fromIP = isGreenBranch ? env.BLUE_IP : env.GREEN_IP
def toIP = isGreenBranch ? env.GREEN_IP : env.BLUE_IP
sshagent(['lb-ssh']) {
sh """
ssh -o StrictHostKeyChecking=no ec2-user@${LB_IP} <<'EOF'
sudo sed -i 's/${fromIP}/${toIP}/' /etc/nginx/conf.d/loadbalancer.conf
sudo nginx -s reload
EOF
"""
}
}
}
}
}
/* ─────────────── 결과 알림 ─────────────── */
post {
success { echo "✅ ${env.BRANCH_NAME} 배포 완료 (TAG=${env.TAG})" }
failure { echo "❌ ${env.BRANCH_NAME} 배포 실패 – 콘솔 로그 확인" }
}
}