-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile.groovy
175 lines (161 loc) · 5.84 KB
/
Jenkinsfile.groovy
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
def getSplitBranchName(branchName) {
def branchArray = env.GIT_BRANCH.split("/")
script {
echo "branchName ${branchArray[1]}"
}
return branchArray[1]
}
def getStageName(String branchName) {
def master = "master"
if (master == branchName) {
return "production";
}
return "develop";
}
def branchName
def deployStage
def imageName
pipeline {
agent any
environment {
registryCredential = 'docker-hub'
dockerImage = ''
}
stages {
stage('initialize variables') {
steps {
checkout scm
script {
branchName = env.GIT_BRANCH.split("/")[1]
deployStage = getStageName("${branchName}")
imageName = "larrykwon/eroom-api-" + "${deployStage}"
}
}
post {
success {
echo "${branchName}"
echo "${deployStage}"
echo "${imageName}"
}
failure {
error "fail while initializing"
}
}
}
stage('Prepare') {
steps {
echo 'Clonning Repository'
sh 'pwd'
echo 'Pulling...' + deployStage
git url: 'https://github.com/e-room/e-room.git',
branch: branchName,
credentialsId: 'github'
sh 'ls -al'
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Bulid Gradle') {
steps {
echo 'Bulid Gradle'
sh 'pwd'
withGradle {
sh './gradlew clean build --exclude-task test'
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('Bulid Docker') {
steps {
echo 'Bulid Docker'
script {
if (deployStage == 'production') {
sh 'pwd'
dir('./env/prod') {
sh 'pwd'
sh 'cp -r ../../build ./'
sh 'ls'
script {
dockerImage = docker.build imageName
}
}
} else {
sh 'pwd'
dir('./env/dev') {
sh 'pwd'
sh 'cp -r ../../build ./'
sh 'ls'
script {
dockerImage = docker.build imageName
}
}
}
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('Push Docker') {
steps {
echo 'Push Docker' + 'stage: ' + "${deployStage}"
sh 'ls -al'
script {
docker.withRegistry('', registryCredential) {
dockerImage.push()
}
}
}
post {
success {
sh 'yes y | docker image prune'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Docker Run') {
steps {
echo 'Pull Docker Image & Docker Image Run'
script {
if (deployStage == 'production') {
sshagent(credentials: ['ssh']) {
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_PROD} 'docker pull larrykwon/eroom-api-production:latest'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_PROD} 'docker ps -aq --filter name=eroom-api-production | grep . && docker rm -f \$(docker ps -aq --filter name=eroom-api-production)'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_PROD} 'docker run -d --name eroom-api-production -p 8080:8080 larrykwon/eroom-api-production:latest'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_PROD} 'yes y | docker image prune'"
}
} else {
sshagent(credentials: ['ssh']) {
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_DEV} 'docker pull larrykwon/eroom-api-develop:latest'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_DEV} 'docker ps -aq --filter name=eroom-api-develop | grep . && docker rm -f \$(docker ps -aq --filter name=eroom-api-develop)'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_DEV} 'docker run -d --name eroom-api-develop -p 8080:8080 larrykwon/eroom-api-develop:latest'"
sh "ssh -o StrictHostKeyChecking=no ${env.EROOM_API_DEV} 'yes y | docker image prune'"
}
}
}
}
}
}
post {
success {
slackSend(channel: '#tech-deploy', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend(channel: '#tech-deploy', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}