forked from peteryu24/tl1p
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
154 lines (129 loc) · 4.67 KB
/
Jenkinsfile
File metadata and controls
154 lines (129 loc) · 4.67 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
pipeline {
agent any
tools {
gradle 'gradle'
}
environment {
imageName = 'haisley77/release'
registryCredential = 'docker_token'
releaseServerAccount = credentials('RELEASE_SERVER_ACCOUNT')
releaseServerUri = credentials('RELEASE_SERVER_URI')
releasePort = credentials('RELEASE_SERVER_PORT')
githubTargetBranch = 'release/v1.0'
githubCloneUrl = credentials('GITHUB_CLONE_URL')
}
post {
success {
echo 'deployment success'
}
failure {
echo 'deployment failure'
}
}
stages {
stage('Git Clone') {
steps {
git branch: "${githubTargetBranch}", credentialsId: 'github_token',
url: "${env.githubCloneUrl}"
}
}
stage('Setup build environment') {
steps {
script {
sh 'rm -rf env'
sh 'mkdir -p env'
sh "chown -R jenkins:jenkins env"
sh "chmod -R 755 env"
sh 'rm -rf src/main/resources'
sh 'mkdir -p src/main/resources'
sh "chown -R jenkins:jenkins src/main/resources"
sh "chmod -R 755 src/main/resources"
}
withCredentials([file(credentialsId: 'db', variable: 'dbFile')]) {
script {
sh 'cp $dbFile env/db.env'
}
}
withCredentials([file(credentialsId: 'test-db', variable: 'testdbFile')]) {
script {
sh 'cp $testdbFile env/test-db.env'
}
}
withCredentials([file(credentialsId: 'security', variable: 'securityFile')]) {
script {
sh 'cp $securityFile env/security.env'
}
}
withCredentials([file(credentialsId: 'application.yml', variable: 'ymlFile')]) {
script {
sh 'cp $ymlFile src/main/resources/application.yml'
}
}
}
}
stage('Build') {
steps {
echo 'Building...'
sh 'chmod +x ./gradlew'
sh './gradlew clean bootJar'
}
}
stage('Test') {
steps {
echo 'Test...'
sh './gradlew test'
}
}
stage('[Backend] Image Build & DockerHub Push') {
when {
expression { env.GITHUB_PR_STATE != 'opened' }
}
steps {
script {
docker.withRegistry('', registryCredential) {
// sh "docker build -t $imageName:$BUILD_NUMBER ."
sh "docker build -t $imageName:latest ."
// sh "docker push $imageName:$BUILD_NUMBER"
sh "docker push $imageName:latest"
}
}
}
}
stage('DockerHub Pull') {
when {
expression { env.GITHUB_PR_STATE != 'opened' }
}
steps {
sshagent(credentials: ['ubuntu_key']) {
sh """
ssh -o StrictHostKeyChecking=no ${env.releaseServerAccount}@${env.releaseServerUri} 'sudo docker pull $imageName:latest'
"""
}
}
}
stage('Service Start') {
when {
expression { env.GITHUB_PR_STATE != 'opened' }
}
steps {
sshagent(credentials: ['ubuntu_key']) {
sh """
ssh -o StrictHostKeyChecking=no ${env.releaseServerAccount}@${env.releaseServerUri} '
if [ \$(docker ps -q --filter "name=tl1p") ]; then
docker stop tl1p
docker rm tl1p
fi
sudo docker run -i -e TZ=Asia/Seoul \
--env-file=/home/ubuntu/env/db.env \
--env-file=/home/ubuntu/env/security.env \
--env-file=/home/ubuntu/env/test-db.env \
--name tl1p \
-p ${env.releasePort}:${env.releasePort} \
-d $imageName:latest
'
"""
}
}
}
}
}