-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
227 lines (214 loc) · 8.79 KB
/
Jenkinsfile
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def helmDeploy(Map args) {
dir("helm-chart/app/${args.app_name}") {
echo "[INFO] Step 1: Helm Deploy dry-run"
sh """helm upgrade --install ${args.app_name} --namespace ${args.namespace} --set image.tag="${args.image_tag}" -f ${args.app_env}.yaml . --dry-run --debug"""
echo "[INFO] Step 2: Helm Deploy"
sh """helm upgrade --install ${args.app_name} --namespace ${args.namespace} --set image.tag="${args.image_tag}" -f ${args.app_env}.yaml . -w """
}
}
def toJson = {
input ->
groovy.json.JsonOutput.toJson(input)
}
pipeline {
agent {
kubernetes {
label "agent-${UUID.randomUUID().toString()}"
yamlFile 'yaml/pod.yaml'
}
}
stages {
stage('Prepare') {
steps {
script {
def madkitty_url= "http://madkitty-api.devops-tools.svc.cluster.local/${app_env}/${app_name}"
response = httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
url: madkitty_url,
validResponseCodes: '200'
// writeFile file: 'mad_config.json', text: response.content
// def config = readJSON file: 'mad_config.json'
def config = readJSON text: response.content
def harbor_url = "harbor.mymyhub.com"
def image_repo = "${harbor_url}/images/${app_name}"
config.each {
env["APP_"+it.key.toUpperCase()] = it.value
}
env.HARBOR_URL = harbor_url
env.APP_IMAGE_REPO = image_repo
if (config.type == "nodejs") {
env.APP_UT_TAG = "nodejs"
env.APP_UT_CMD = "yarn install && yarn test:coverage"
} else {
error '[ERROR] Invalid App Type'
}
// props = readJSON text: env.APP_CONFIG
// println(props.name)
}
}
post {
success {
script {
echo "Stage: success ..."
}
}
unsuccessful {
script {
echo "Stage: unsuccessful ..."
}
}
}
}
stage('Git'){
steps {
script {
git branch: 'dev',
credentialsId: '45ffa5c8-48bf-4c18-b40f-334bc25d0c56',
url: env.APP_GIT_URL
def image_tag = sh(script: "git rev-parse --short HEAD",returnStdout:true).trim()
env.APP_IMAGE_TAG = image_tag
def robot_msg = sh(script: "git log ${image_tag} --format='%s (%h)' -1",returnStdout:true).trim()
env.APP_ROBOT_MSG = robot_msg
}
}
post {
success {
script {
echo "Stage: success ..."
}
}
unsuccessful {
script {
echo "Stage: unsuccessful ..."
}
}
}
}
stage('Unit Test'){
steps {
script {
// container(APP_UT_TAG) {
// sh APP_UT_CMD
// }
echo "[INFO] Sonarqube"
}
}
post {
success {
script {
echo "Stage: success ..."
}
}
unsuccessful {
script {
echo "Stage: unsuccessful ..."
}
}
}
}
stage('Docker'){
steps {
script {
container('docker') {
echo "[Step] Compile docker image"
def url= "https://${env.HARBOR_URL}/api/v2.0/projects/images/repositories/${env.app_name}/artifacts/${env.APP_IMAGE_TAG}/tags"
def response = httpRequest httpMode: 'GET',
url: url,
authentication: 'harbor-token'
if (response.status == 200) {
echo "[INFO] Skipped ${env.APP_IMAGE_REPO}:${env.APP_IMAGE_TAG} Exists"
} else {
docker.withRegistry("https://${env.HARBOR_URL}", "harbor-token") {
echo "[INFO] Build"
if (env.APP_SPECIAL_DIR) {
container = docker.build("${env.APP_IMAGE_REPO}:${env.APP_IMAGE_TAG}", ". --build-arg SPECIAL_NAME=${env.APP_SPECIAL_NAME} --build-arg SPECIAL_DIR=${env.APP_SPECIAL_DIR}")
} else {
container = docker.build("${env.APP_IMAGE_REPO}:${env.APP_IMAGE_TAG}", ".")
}
echo "[INFO] Push"
container.push()
echo "[INFO] Delete"
sh "docker rmi ${env.APP_IMAGE_REPO}:${env.APP_IMAGE_TAG}"
}
}
}
}
}
post {
success {
script {
echo "Stage: success ..."
}
}
unsuccessful {
script {
echo "Stage: unsuccessful ..."
}
}
}
}
stage('Helm'){
steps {
script {
withCredentials([usernamePassword(credentialsId: 'harbor-token', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASSWORD')]) {
container('helm') {
withKubeConfig([credentialsId: app_env]) {
helmDeploy(
username : DOCKER_USER,
password : DOCKER_PASSWORD,
app_name : env.app_name,
app_env : env.app_env,
namespace : env.APP_NAMESPACE,
image_tag : env.APP_IMAGE_TAG
)
}
}
}
}
}
post {
success {
script {
echo "Stage: success ..."
}
}
unsuccessful {
script {
echo "Stage: unsuccessful ..."
}
}
}
}
}
post {
success {
script {
def body = [
"title": "${app_env} ${app_name} Build Success",
"text": "${env.APP_ROBOT_MSG}\nurl: ${BUILD_URL}"
]
def response = httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
url: "https://open.feishu.cn/open-apis/bot/hook/${env.APP_WEBHOOK}",
requestBody: toJson(body),
validResponseCodes: '200'
}
}
unsuccessful {
script {
def body = [
"title": "${app_env} ${app_name} Build Failed",
"text": "${env.APP_ROBOT_MSG}\nurl: ${BUILD_URL}"
]
def response = httpRequest consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
url: "https://open.feishu.cn/open-apis/bot/hook/${env.APP_WEBHOOK}",
requestBody: toJson(body),
validResponseCodes: '200'
}
}
}
}