-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.groovy
152 lines (136 loc) · 3.63 KB
/
deploy.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
def createTag() {
return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
}
pipeline {
agent any
options {
disableConcurrentBuilds()
timestamps()
}
environment {
appTag = createTag()
cfToken = credentials("cloudflaretoken")
gitlabToken = credentials("gitlabtoken")
appName = "${env.JOB_BASE_NAME}"
}
stages {
stage("preparation") {
steps {
script {
sh "rm -rf cisrc && mkdir -p cisrc"
load "pipeline/default.env.groovy"
load "pipeline/"+env.JOB_BASE_NAME+".env.groovy"
//python jinja2 handle template
docker.image('python:3.10').inside {
sh """
export HOME=/tmp
export PIP_CACHE_DIR=\$HOME/.cache/pip
pip3 install --user --no-cache-dir jinja2
export PYTHONPATH=\$HOME/.local/lib/python3.*/site-packages:\$PYTHONPATH
python3 pipeline/render.py
"""
}
//groovy handle template
//def deployYaml = readFile('pipeline/deploy.yaml.tpl')
//def envOutput = sh(script: 'env', returnStdout: true).trim()
//def envVars = [:]
//envOutput.split('\n').each { line ->
// def keyValue = line.split('=')
// if (keyValue.size() == 2) {
// envVars[keyValue[0]] = keyValue[1]
// }
//}
//envVars.each { key, value ->
// deployYaml = deployYaml.replaceAll("\\\$\\{${key}\\}", value)
//}
//writeFile file: "pipeline/"+env.appName+'-deploy.yaml', text: deployYaml
}
}
}
stage("gitlabe PullCode") {
steps {
script {
echo "gitURL: ${gitURL}"
echo "gitBranch: ${gitBranch}"
}
dir("cisrc") {
git url: env.gitURL,
credentialsId: env.gitCredentials,
branch: env.gitBranch
}
}
}
stage("docker Build") {
steps {
script {
dir("cisrc") {
sh "cp ../pipeline/${env.JOB_BASE_NAME}.dockerfile ./Dockerfile"
docker.withRegistry(env.harborURL, env.harborCredentials){
sh """
docker build --build-arg gitlabToken=${gitlabToken} -t $appImage:$appTag .
docker push $appImage:$appTag
docker rmi $appImage:$appTag
"""
}
}
}
}
}
stage("k8s Deploy") {
steps {
script {
dir("cisrc") {
sh "cp ../pipeline/${env.appName}-deploy.yaml ./deploy.yaml"
withKubeConfig([serverUrl: env.k8sURL, credentialsId: env.k8sCredentials]) {
sh """
kubectl apply -f deploy.yaml
"""
}
}
}
}
}
stage("cloudflare cachePurge") {
when {
expression { return env.isCFPurge.toBoolean() }
}
steps {
script {
sh 'chmod +x ./pipeline/cloudflare.sh && ./pipeline/cloudflare.sh $zoneId $cfToken'
}
}
}
stage("k8s Rollback") {
options {
timeout(time: 12, unit: "HOURS")
}
input {
ok "submit"
message "Rollback"
parameters {
booleanParam(name: 'ROLLBACK', defaultValue: false, description: "If you want rollback, please select.")
}
}
steps {
script {
if (ROLLBACK == "true") {
withKubeConfig([serverUrl: env.k8sURL, credentialsId: env.k8sCredentials]) {
sh """
kubectl rollout undo deployment $appName -n $k8sNamespace
"""
}
}
}
}
}
}
post {
always {
script {
wrap([$class: 'BuildUser']) {
currentBuild.displayName = "#${BUILD_NUMBER} ${BUILD_USER}"
}
}
}
}
}