forked from automata-network/automata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
215 lines (192 loc) · 6.29 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
pipeline {
environment {
PROFILE = "release"
GIT_COMMIT_ID = sh (script: 'git rev-parse --short HEAD', returnStdout: true).trim()
GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B', returnStdout: true).trim()
REGISTRY_URL = credentials('automata-docker-registry-url')
REGISTRY_BASE_REPO = credentials('automata-docker-registry-base-repo')
}
parameters {
choice(
description: 'Choose your operation',
name: 'operation',
choices: ['deploy', 'upgrade', 'upgrade-skip-compile', 'delete']
)
string(
description: 'Skip compile, specify image to deploy',
name: 'image'
)
text(
description: 'Extern nodes',
name: 'syncNodes'
)
choice(
description: 'Choose the number of light nodes to run',
name: 'lightCount',
choices: ['', '0', '1', '2', '3', '4']
)
booleanParam(
description: "Keep chain's data after delete (Only effective when deploying)",
name: 'keepDataAfterDelete',
defaultValue: true
)
booleanParam(
description: "Insert key to validator",
name: 'insertKey',
defaultValue: true
)
}
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: rust
image: chansonchan/automata-builder:nightly-2020-10-25
command:
- cat
tty: true
volumeMounts:
- mountPath: /cache/target
name: cache
subPath: ${env.JOB_NAME}
- mountPath: /usr/local/cargo/registry/index
name: cache
subPath: cargo/registry/index
- mountPath: /usr/local/cargo/registry/cache
name: cache
subPath: cargo/registry/cache
- mountPath: /usr/local/cargo/git/db
name: cache
subPath: cargo/git/db
- name: image-builder
image: docker:latest
command:
- cat
tty: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker
- name: sshpass
image: chansonchan/sshpass
command:
- cat
tty: true
volumes:
- name: cache
persistentVolumeClaim:
claimName: cache
- name: docker
hostPath:
path: /var/run/docker.sock
"""
}
}
stages {
stage('compile') {
when {
beforeAgent true
allOf {
expression { params.operation ==~ /(deploy|upgrade)/ }
expression { return params.image ? false : true }
}
}
steps {
container('rust') {
script {
echo "${GIT_COMMIT_MSG}"
sh "ln -s /cache/target ./target"
sh "cargo build --${env.PROFILE} --bin automata"
sh "cp ./target/${PROFILE}/automata ."
}
}
}
}
stage('build and push docker image') {
when {
beforeAgent true
allOf {
expression { params.operation ==~ /(deploy|upgrade)/ }
expression { return params.image ? false : true }
}
}
environment {
REGISTRY = credentials('c5425e91-91d8-4084-8011-82c6497cd40a')
}
steps {
container('image-builder') {
script {
def registryTag = "${env.REGISTRY_URL}/${env.REGISTRY_BASE_REPO}/automata:${env.GIT_COMMIT_ID}"
echo "build and tag image"
sh "docker build -t ${registryTag} -f .jenkins/AppDockerfile ."
echo "push image"
sh "docker login ${env.REGISTRY_URL} -u $REGISTRY_USR -p $REGISTRY_PSW"
sh "docker push ${registryTag}"
}
}
}
}
stage('operate') {
environment {
K8S_MASTER_IP = credentials('jiaxing-k8s-master-ip')
K8S_MASTER = credentials('381816aa-abe9-4a66-8842-5f141dff42b4')
STORAGE_CLASS_NAME = credentials('automata-storageClassName')
IMAGE_PULL_SECRETS = credentials('automata-imagePullSecrets')
CHAIN_KEY_SECRET = credentials('automata-chainKeySecret')
NAMESPACE = credentials('automata-namespace')
}
steps {
container('sshpass') {
script {
if (params.operation == "delete") {
sh "sshpass -p $K8S_MASTER_PSW ssh -T -o StrictHostKeyChecking=no $K8S_MASTER_USR@$K8S_MASTER_IP" +
" \"helm uninstall -n ${env.NAMESPACE} ata\""
} else {
def operate, additionalSet = ""
if (params.operation == "deploy") {
operate = "install"
if (params.keepDataAfterDelete != null) {
additionalSet += "--set chain.keepData=${params.keepDataAfterDelete} "
}
} else {
operate = "upgrade"
}
if (params.lightCount != '') {
additionalSet += "--set chain.lightCount=${params.lightCount} "
}
def syncNodes = "\"{", first = true
for (item in params.syncNodes.tokenize('\n')) {
if (first) {
syncNodes += item
first = false
} else {
syncNodes += ", ${item}"
}
}
syncNodes += "}\""
if (!first) {
additionalSet += "--set chain.syncNodes=${syncNodes} "
}
def image = "${env.REGISTRY_URL}/${env.REGISTRY_BASE_REPO}/automata:${env.GIT_COMMIT_ID}"
if (params.image) {
image = params.image
} else {
additionalSet += "--set imagePullSecrets=${env.IMAGE_PULL_SECRETS} "
}
sh "sshpass -p $K8S_MASTER_PSW scp -o StrictHostKeyChecking=no -r .jenkins/automata-chart $K8S_MASTER_USR@$K8S_MASTER_IP:/tmp/automata-chart"
sh "sshpass -p $K8S_MASTER_PSW ssh -T -o StrictHostKeyChecking=no $K8S_MASTER_USR@$K8S_MASTER_IP" +
" \"helm ${operate} --atomic -n ${env.NAMESPACE} ${additionalSet}" +
"--set storageClassName=${env.STORAGE_CLASS_NAME} " +
"--set image=${image} " +
"--set chain.keySecret=${env.CHAIN_KEY_SECRET} " +
"--set chain.insertKey=${params.insertKey} " +
"ata /tmp/automata-chart && rm -rf /tmp/automata-chart\""
}
}
}
}
}
}
}