forked from apache/incubator-kie-kogito-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.deploy
294 lines (254 loc) · 10.9 KB
/
Jenkinsfile.deploy
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
@Library('jenkins-pipeline-shared-libraries')_
// As Cekit is not compatible with podman, we use docker in that pipeline
IMAGES = [
"kogito-quarkus-ubi8",
"kogito-quarkus-jvm-ubi8",
"kogito-quarkus-ubi8-s2i",
"kogito-springboot-ubi8",
"kogito-springboot-ubi8-s2i",
"kogito-data-index",
"kogito-jobs-service",
"kogito-management-console"
]
// Should be a single pipeline (not multibranch)
pipeline {
agent {
label 'kogito-image-slave && !master'
}
// Needed for local build
tools {
jdk 'kie-jdk11'
}
parameters {
string(name: 'DISPLAY_NAME', defaultValue: '', description: 'Setup a specific build display name')
string(name: 'BUILD_BRANCH_NAME', defaultValue: 'master', description: 'Which branch to build ? Set if you are not on a multibranch pipeline.')
string(name: 'GIT_AUTHOR', defaultValue: 'kiegroup', description: 'Which Git author repository ?')
booleanParam(name: 'IMAGE_USE_OPENSHIFT_REGISTRY', defaultValue: false, description: 'Set to true if image should be deployed in Openshift registry.In this case, IMAGE_REGISTRY_CREDENTIALS, IMAGE_REGISTRY and IMAGE_NAMESPACE parameters will be ignored')
string(name: 'IMAGE_REGISTRY_CREDENTIALS', defaultValue: '', description: 'Image registry credentials to use to deploy images. Will be ignored if no IMAGE_REGISTRY is given')
string(name: 'IMAGE_REGISTRY', defaultValue: '', description: 'Image registry to use to deploy images')
string(name: 'IMAGE_NAMESPACE', defaultValue: 'kiegroup', description: 'Image namespace to use to deploy images')
string(name: 'IMAGE_NAME_SUFFIX', defaultValue: '', description: 'Image name suffix to use to deploy images. In case you need to change the final image name, you can add a suffix to it.')
string(name: 'IMAGE_TAG', defaultValue: '', description: 'Image tag to use to deploy images')
string(name: 'MAVEN_ARTIFACT_REPOSITORY', defaultValue: '', description: 'Maven repository where the build artifacts are present')
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Skip tests')
}
environment {
JAVA_HOME = "${GRAALVM_HOME}"
OPENSHIFT_API = credentials("OPENSHIFT_API")
OPENSHIFT_REGISTRY = credentials("OPENSHIFT_REGISTRY")
OPENSHIFT_CREDS_KEY = "OPENSHIFT_CREDS"
}
stages {
stage('Initialization') {
steps {
script {
clean()
if (params.DISPLAY_NAME != "") {
currentBuild.displayName = params.DISPLAY_NAME
}
checkout(githubscm.resolveRepository("kogito-images", params.GIT_AUTHOR, params.BUILD_BRANCH_NAME, false))
// Set the mirror url only if no artifact repository is given
if (params.MAVEN_ARTIFACT_REPOSITORY == ''
&& env.MAVEN_MIRROR_REPOSITORY != null
&& env.MAVEN_MIRROR_REPOSITORY != ''){
echo "Set Maven mirror url"
env.MAVEN_MIRROR_URL = env.MAVEN_MIRROR_REPOSITORY
}
}
}
}
stage('Update Maven information') {
steps {
script {
// Update artifacts
updateArtifactCmd = "python3 scripts/update-maven-information.py"
if(params.MAVEN_ARTIFACT_REPOSITORY != ''){
updateArtifactCmd += " --repo-url ${params.MAVEN_ARTIFACT_REPOSITORY}"
}
sh updateArtifactCmd
// Debug purpose in case of issue
sh "cat modules/kogito-data-index/module.yaml"
sh "cat modules/kogito-jobs-service/module.yaml"
sh "cat modules/kogito-management-console/module.yaml"
}
}
}
stage('Setup tests repository') {
when {
expression {
return params.MAVEN_ARTIFACT_REPOSITORY != ''
}
}
steps {
script {
// Update repo in tests
sh "python3 scripts/update-tests-maven-repo.py --repo-url ${params.MAVEN_ARTIFACT_REPOSITORY}"
// Debug purpose in case of issue
sh "cat tests/test-apps/clone-repo.sh"
sh "cat tests/features/kogito-quarkus-ubi8-s2i.feature"
sh "cat tests/features/kogito-springboot-ubi8-s2i.feature"
}
}
}
stage('Validate CeKit Image and Modules descriptors') {
steps {
sh """
curl -Ls https://github.com/kiegroup/kie-cloud-tools/releases/download/1.0-SNAPSHOT/cekit-image-validator-runner.tgz --output cekit-image-validator-runner.tgz
tar -xzvf cekit-image-validator-runner.tgz
chmod +x cekit-image-validator-runner
"""
sh "./cekit-image-validator-runner modules/"
sh """
./cekit-image-validator-runner image.yaml
./cekit-image-validator-runner kogito-data-index-overrides.yaml
./cekit-image-validator-runner kogito-jobs-service-overrides.yaml
./cekit-image-validator-runner kogito-management-console-overrides.yaml
./cekit-image-validator-runner kogito-quarkus-jvm-overrides.yaml
./cekit-image-validator-runner kogito-quarkus-overrides.yaml
./cekit-image-validator-runner kogito-quarkus-s2i-overrides.yaml
./cekit-image-validator-runner kogito-springboot-overrides.yaml
./cekit-image-validator-runner kogito-springboot-s2i-overrides.yaml
"""
}
}
stage('Prepare offline kogito-examples') {
when {
expression {
return !params.SKIP_TESTS;
}
}
steps {
sh "make clone-repos"
}
}
stage('Build and Test Images'){
steps{
script {
build_stages = [:]
IMAGES.each{ image -> build_stages[image] = {
initWorkspace(image)
dir(getWorkspacePath(image)){
try{
sh "make ${image} ignore_test=${params.SKIP_TESTS} cekit_option='--work-dir .'"
}
finally{
junit testResults: 'target/test/results/*.xml', allowEmptyResults: true
}
}
}
}
parallel build_stages
}
}
}
stage('Tagging') {
steps {
script {
tagImages()
}
}
}
stage('Pushing') {
steps {
script {
if (isDeployImageInOpenshiftRegistry()) {
loginOpenshiftRegistry()
} else if (getDeployImageRegistryCredentials() != ''){
loginContainerRegistry(getDeployImageRegistry(), getDeployImageRegistryCredentials())
}
pushImages()
}
}
}
}
post {
always {
script{
clean()
}
}
}
}
void clean() {
cleanWorkspaces()
cleanImages()
// Clean Cekit cache, in case we reuse an old node
sh "rm -rf \$HOME/.cekit/cache"
}
void cleanImages(){
sh "docker rm -f \$(docker ps -a -q) || date"
sh "docker rmi -f \$(docker images -q) || date"
}
void tagImages() {
for(String imageName : IMAGES) {
sh "docker tag quay.io/kiegroup/${imageName}:latest ${buildImageName(imageName)}"
}
}
void pushImages() {
for(String imageName : IMAGES) {
sh "docker push ${buildImageName(imageName)}"
}
}
String buildImageName(String imageName) {
String finalImageName = imageName
if(getDeployImageNameSuffix() != ''){
finalImageName += "-" + getDeployImageNameSuffix()
}
return "${getDeployImageRegistry()}/${getDeployImageNamespace()}/${finalImageName}:${getDeployImageTag()}"
}
void loginOpenshift(){
withCredentials([usernamePassword(credentialsId: env.OPENSHIFT_CREDS_KEY, usernameVariable: 'OC_USER', passwordVariable: 'OC_PWD')]){
sh "oc login --username=${OC_USER} --password=${OC_PWD} --server=${env.OPENSHIFT_API} --insecure-skip-tls-verify"
}
}
void loginOpenshiftRegistry(){
loginOpenshift()
// username can be anything. See https://docs.openshift.com/container-platform/4.4/registry/accessing-the-registry.html#registry-accessing-directly_accessing-the-registry
sh "set +x && docker login -u anything -p \$(oc whoami -t) ${env.OPENSHIFT_REGISTRY}"
}
void loginContainerRegistry(String registry, String credsId){
withCredentials([usernamePassword(credentialsId: credsId, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PWD')]) {
sh "docker login -u ${REGISTRY_USER} -p ${REGISTRY_PWD} ${registry}"
}
}
////////////////////////////////////////////////////////////////////////
// Deploy image information
////////////////////////////////////////////////////////////////////////
boolean isDeployImageInOpenshiftRegistry(){
return params.IMAGE_USE_OPENSHIFT_REGISTRY
}
String getDeployImageRegistryCredentials(){
return isDeployImageInOpenshiftRegistry() ? "" : params.IMAGE_REGISTRY_CREDENTIALS
}
String getDeployImageRegistry(){
return isDeployImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.IMAGE_REGISTRY
}
String getDeployImageNamespace(){
return isDeployImageInOpenshiftRegistry() ? "openshift" : params.IMAGE_NAMESPACE
}
String getDeployImageNameSuffix(){
return params.IMAGE_NAME_SUFFIX
}
String getDeployImageTag(){
if (params.IMAGE_TAG != ""){
return params.IMAGE_TAG
} else {
return sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
}
}
////////////////////////////////////////////////////////////////////////
// Workspaces
////////////////////////////////////////////////////////////////////////
void initWorkspace(String image){
sh "mkdir -p ${getWorkspacePath(image)}"
sh "rsync -av --progress . ${getWorkspacePath(image)} --exclude workspaces"
}
void cleanWorkspaces(){
sh "rm -rf ${getWorkspacesPath()}"
}
String getWorkspacesPath(){
return "${WORKSPACE}/workspaces"
}
String getWorkspacePath(String image){
return "${getWorkspacesPath()}/${image}"
}