forked from SWM-304/TeamPlanner-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile:v2
172 lines (142 loc) · 5.28 KB
/
Jenkinsfile:v2
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
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS') // set timeout 1 hour
}
environment {
TIME_ZONE = 'Asia/Seoul'
//github
TARGET_BRANCH = 'develop'
REPOSITORY_URL= 'https://github.com/SWM-304/TeamPlanner-BE.git'
//docker-hub
registryCredential = 'docker-hub'
//aws ecr
CONTAINER_NAME = 'teamplanner-backend-container'
AWS_CREDENTIAL_NAME = 'AWS_ECR'
ECR_PATH = '129715120090.dkr.ecr.ap-northeast-2.amazonaws.com'
IMAGE_NAME = '129715120090.dkr.ecr.ap-northeast-2.amazonaws.com/teamplanner-backendserver'
REGION = 'ap-northeast-2'
}
stages {
stage('init') {
steps {
echo 'init stage'
deleteDir()
}
post {
success {
echo 'success init in pipeline'
}
failure {
error 'fail init in pipeline'
}
}
}
stage('Prepare') {
steps {
echo 'Cloning Repository'
git branch: 'develop',
credentialsId: 'repo-and-hook-access-token-credentials',
url: 'https://github.com/SWM-304/TeamPlanner-BE.git'
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
error 'This pipeline stops here...'
}
}
}
// 일단은 테스트없이 빌드
stage('Build Gradle') {
steps {
echo 'Build Gradle'
dir('.'){
sh '''
pwd
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
chmod +x ./gradlew
./gradlew build --exclude-task test
'''
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
// 도커 이미지를 만든다. build number로 태그를 주되 latest 태그도 부여한다.
stage('Build Docker') {
steps {
echo 'Build Docker'
sh """
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
docker builder prune
docker build -t $IMAGE_NAME:$BUILD_NUMBER .
docker tag $IMAGE_NAME:$BUILD_NUMBER $IMAGE_NAME:latest
"""
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
// 빌드넘버 태그와 latest 태그 둘 다 올린다.
stage('Push Docker') {
steps {
echo 'Push Docker'
script {
// cleanup current user docker credentials
sh 'rm -f ~/.dockercfg ~/.docker/config.json || true'
docker.withRegistry("https://${ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") {
docker.image("${IMAGE_NAME}:${BUILD_NUMBER}").push()
docker.image("${IMAGE_NAME}:latest").push()
}
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('S3 script upload') {
steps {
echo 'deploy push application And S3 script upload'
sh'''
cd /var/jenkins_home/workspace
zip teamplanner-backend.zip teamPlannerBackEnd_jenkinsFile
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
aws deploy push --application-name teamplanner-webapp --s3-location s3://teamplanner-codeploy-bucket/teamplanner-backend.zip --ignore-hidden-files
'''
}
}
stage('push deploy application') {
steps {
echo 'deploy push application And S3 script upload'
sh'''
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
aws deploy create-deployment --application-name teamplanner-webapp --s3-location "bucket=teamplanner-codeploy-bucket,key=teamplanner-backend.zip,bundleType=zip" --deployment-group-name teamplanner-backend-deploy --deployment-config-name CodeDeployDefault.OneAtATime --description "Deploy Init for teamplanner-backend-deploy-group"
'''
}
}
stage('Clean Up Docker Images on Jenkins Server') {
steps {
echo 'Cleaning up unused Docker images on Jenkins server'
// Clean up unused Docker images, including those created within the last hour
sh "docker image prune -f --all --filter \"until=1h\""
}
}
}
post {
success {
slackSend (channel: '#cicd-notification', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: '#cicd-notification', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}