-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
148 lines (137 loc) · 5.69 KB
/
Jenkinsfile
File metadata and controls
148 lines (137 loc) · 5.69 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
pipeline {
agent any
environment {
REPO = 'ktb-cpplab/cpplab-be'
ECR_REPO = '891612581533.dkr.ecr.ap-northeast-2.amazonaws.com/cpplab/be'
ECR_CREDENTIALS_ID = 'ecr:ap-northeast-2:AWS_CREDENTIALS'
GITHUB_CREDENTIALS_ID = 'github_token'
REMOTE_USER = 'ubuntu'
ECS_CLUSTER_NAME = 'cpplab-prod'
ECS_SERVICE_NAME = 'prod-be-service'
AWS_REGION = 'ap-northeast-2'
BRANCH_NAME = "${env.GIT_BRANCH}"
DOCKER_TAG = "${env.BUILD_NUMBER}" // Jenkins build number
AWS_CREDENTIALS_ID = 'AWS_CREDENTIALS'
APPLICATION_YML = 'application.yml'
PROMETHEUS_YML = 'prometheus.yml'
}
stages {
stage('Checkout') {
steps {
script {
currentBuild.description = 'Checkout'
git branch: "${BRANCH_NAME}", url: "https://github.com/${REPO}.git", credentialsId: "${GITHUB_CREDENTIALS_ID}"
// src/main/resources 디렉토리 생성 및 권한 수정
sh '''
mkdir -p src/main/resources/yaml
chmod -R u+w src/main/resources
'''
}
}
}
stage('Check Changes') {
steps {
script {
currentBuild.description = 'Check Changes'
def changes = sh(script: "git diff --name-only HEAD~1 HEAD", returnStdout: true).trim()
echo "Changed files:\n${changes}"
if (changes ==~ /^README(\.md)?$/) {
echo "Only README file has changed. Skipping build and deployment."
currentBuild.result = 'SUCCESS'
error("Skipping pipeline as only README file has changed.")
}
}
}
}
stage('Prepare Application Properties') {
steps {
script {
// src/main/resources 디렉토리 생성
sh 'mkdir -p src/main/resources/yaml'
sh 'mkdir -p src/main/resources/logging'
}
withCredentials([file(credentialsId: 'application-yml', variable: 'APPLICATION_YML')]) {
sh '''
cp $APPLICATION_YML src/main/resources/application.yml
'''
}
withCredentials([file(credentialsId: 'application-prometheus-yml', variable: 'APPLICATION_PROMETHEUS_YML')]) {
sh '''
cp $APPLICATION_PROMETHEUS_YML src/main/resources/application-prometheus.yml
'''
}
withCredentials([file(credentialsId: 'application-dev-yml', variable: 'APPLICATION_DEV_YML')]) {
sh '''
cp $APPLICATION_DEV_YML src/main/resources/yaml/application-dev.yml
'''
}
withCredentials([file(credentialsId: 'application-log-yml', variable: 'APPLICATION_LOG_YML')]) {
sh '''
cp $APPLICATION_LOG_YML src/main/resources/logging/application-log.yml
'''
}
}
}
stage('Build Docker Image') {
steps {
script {
currentBuild.description = 'Build Docker Image'
dockerImage = docker.build("${ECR_REPO}:${DOCKER_TAG}")
}
}
}
stage('Push to ECR') {
steps {
script {
currentBuild.description = 'Push to ECR'
docker.withRegistry("https://${ECR_REPO}", "${ECR_CREDENTIALS_ID}") {
dockerImage.push("${DOCKER_TAG}")
dockerImage.push("latest")
}
}
}
}
stage('Deploy to ECS') {
steps {
script {
currentBuild.description = 'Update ECS Service'
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: "${AWS_CREDENTIALS_ID}"]]) {
sh """
aws ecs update-service --cluster ${ECS_CLUSTER_NAME} --service ${ECS_SERVICE_NAME} --force-new-deployment --region ${AWS_REGION}
"""
}
}
}
}
}
post {
success {
withCredentials([string(credentialsId: 'Discord-Backend-Webhook', variable: 'DISCORD')]) {
discordSend description: """
제목: ${currentBuild.displayName}
결과: 성공
실행 시간: ${currentBuild.duration / 1000}s
""",
link: env.BUILD_URL,
title: "✅ ${env.JOB_NAME} : ${currentBuild.displayName} 성공",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'Discord-Backend-Webhook', variable: 'DISCORD')]) {
discordSend description: """
제목: ${currentBuild.displayName}
결과: 실패
실패한 단계: ${currentBuild.description ?: 'Unknown'}
실행 시간: ${currentBuild.duration / 1000}s
""",
link: env.BUILD_URL,
title: "❌ ${env.JOB_NAME} : ${currentBuild.displayName} 실패",
webhookURL: "$DISCORD"
}
}
always {
sh "rm -f src/main/resources/application.yml src/main/resources/yaml/application-dev.yml"
}
}
}