forked from apache/incubator-kie-kogito-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.promote
217 lines (183 loc) · 8.09 KB
/
Jenkinsfile.promote
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
// Promote images
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"]
pipeline {
agent {
label 'kogito-image-slave && !master'
}
parameters {
string(name: 'DISPLAY_NAME', defaultValue: '', description: 'Setup a specific build display name')
booleanParam(name: 'BASE_IMAGE_USE_OPENSHIFT_REGISTRY', defaultValue: false, description: 'Set to true if base image should be retrieved from Openshift registry.In this case, BASE_IMAGE_REGISTRY_CREDENTIALS, BASE_IMAGE_REGISTRY and BASE_IMAGE_NAMESPACE parameters will be ignored')
string(name: 'BASE_IMAGE_REGISTRY_CREDENTIALS', defaultValue: '', description: 'Base Image registry credentials to use to deploy images. Will be ignored if no BASE_IMAGE_REGISTRY is given')
string(name: 'BASE_IMAGE_REGISTRY', defaultValue: '', description: 'Base image registry')
string(name: 'BASE_IMAGE_NAMESPACE', defaultValue: 'kiegroup', description: 'Base image namespace')
string(name: 'BASE_IMAGE_NAME_SUFFIX', defaultValue: '', description: 'Base image name suffix')
string(name: 'BASE_IMAGE_TAG', defaultValue: '', description: 'Base image tag')
booleanParam(name: 'PROMOTE_IMAGE_USE_OPENSHIFT_REGISTRY', defaultValue: false, description: 'Set to true if base image should be deployed in Openshift registry.In this case, PROMOTE_IMAGE_REGISTRY_CREDENTIALS, PROMOTE_IMAGE_REGISTRY and PROMOTE_IMAGE_NAMESPACE parameters will be ignored')
string(name: 'PROMOTE_IMAGE_REGISTRY_CREDENTIALS', defaultValue: '', description: 'Promote Image registry credentials to use to deploy images. Will be ignored if no PROMOTE_IMAGE_REGISTRY is given')
string(name: 'PROMOTE_IMAGE_REGISTRY', defaultValue: '', description: 'Promote image registry')
string(name: 'PROMOTE_IMAGE_NAMESPACE', defaultValue: 'kiegroup', description: 'Promote image namespace')
string(name: 'PROMOTE_IMAGE_NAME_SUFFIX', defaultValue: '', description: 'Promote image name suffix')
string(name: 'PROMOTE_IMAGE_TAG', defaultValue: '', description: 'Promote image tag')
booleanParam(name: 'DEPLOY_WITH_LATEST_TAG', defaultValue: false, description: 'Set to true if you want the deployed images to also be with the `latest` tag')
}
environment {
OPENSHIFT_API = credentials("OPENSHIFT_API")
OPENSHIFT_REGISTRY = credentials("OPENSHIFT_REGISTRY")
OPENSHIFT_CREDS_KEY = "OPENSHIFT_CREDS"
}
stages {
stage('Initialization') {
steps {
script {
cleanImages()
if (params.DISPLAY_NAME != "") {
currentBuild.displayName = params.DISPLAY_NAME
}
}
}
}
stage('Pull "old" images'){
steps{
script {
if (isOldImageInOpenshiftRegistry()) {
loginOpenshiftRegistry()
} else if (getOldImageRegistryCredentials() != ''){
loginContainerRegistry(getOldImageRegistry(), getOldImageRegistryCredentials())
}
pullImages()
}
}
}
stage('Tag images'){
steps{
script {
tagImages()
}
}
}
stage('Pushing'){
steps{
script {
if (isNewImageInOpenshiftRegistry()) {
loginOpenshiftRegistry()
} else if (getNewImageRegistryCredentials() != ''){
loginContainerRegistry(getNewImageRegistry(), getNewImageRegistryCredentials())
}
pushImages()
}
}
}
}
post {
always {
script {
cleanImages()
}
}
}
}
void cleanImages(){
sh "podman rm -f \$(podman ps -a -q) || date"
sh "podman rmi -f \$(podman images -q) || date"
}
void pullImages(){
for(String imageName : IMAGES){
sh "podman pull ${getOldImageFullTag(imageName)}"
}
}
void tagImages() {
for(String imageName : IMAGES){
sh "podman tag ${getOldImageFullTag(imageName)} ${getNewImageFullTag(imageName, getNewImageTag())}"
if(isDeployLatestTag()){
sh "podman tag ${getOldImageFullTag(imageName)} ${getNewImageFullTag(imageName, "latest")}"
}
}
}
void pushImages(){
for(String imageName : IMAGES){
sh "podman push ${getNewImageFullTag(imageName, getNewImageTag())}"
if(isDeployLatestTag()){
sh "podman push ${getNewImageFullTag(imageName, "latest")}"
}
}
}
String getOldImageFullTag(String imageName){
return "${getOldImageRegistry()}/${getOldImageNamespace()}/${buildImageNameWithSuffix(imageName, getOldImageNameSuffix())}:${getOldImageTag()}"
}
String getNewImageFullTag(String imageName, String tag){
return "${getNewImageRegistry()}/${getNewImageNamespace()}/${buildImageNameWithSuffix(imageName, getNewImageNameSuffix())}:${tag}"
}
String buildImageNameWithSuffix(String imageName, String suffix) {
String finalImageName = imageName
if(suffix != null && suffix != '') {
finalImageName += "-" + suffix
}
return finalImageName
}
boolean isDeployLatestTag(){
return params.DEPLOY_WITH_LATEST_TAG
}
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 && podman login -u anything -p \$(oc whoami -t) --tls-verify=false ${env.OPENSHIFT_REGISTRY}"
}
void loginContainerRegistry(String registry, String credsId){
withCredentials([usernamePassword(credentialsId: credsId, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PWD')]) {
sh "podman login -u ${REGISTRY_USER} -p ${REGISTRY_PWD} --tls-verify=false ${registry}"
}
}
////////////////////////////////////////////////////////////////////////
// Old image information
////////////////////////////////////////////////////////////////////////
boolean isOldImageInOpenshiftRegistry(){
return params.BASE_IMAGE_USE_OPENSHIFT_REGISTRY
}
String getOldImageRegistryCredentials(){
return isOldImageInOpenshiftRegistry() ? "" : params.BASE_IMAGE_REGISTRY_CREDENTIALS
}
String getOldImageRegistry(){
return isOldImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.BASE_IMAGE_REGISTRY
}
String getOldImageNamespace(){
return isOldImageInOpenshiftRegistry() ? "openshift" : params.BASE_IMAGE_NAMESPACE
}
String getOldImageNameSuffix(){
return params.BASE_IMAGE_NAME_SUFFIX
}
String getOldImageTag(){
return params.BASE_IMAGE_TAG
}
////////////////////////////////////////////////////////////////////////
// New image information
////////////////////////////////////////////////////////////////////////
boolean isNewImageInOpenshiftRegistry(){
return params.PROMOTE_IMAGE_USE_OPENSHIFT_REGISTRY
}
String getNewImageRegistryCredentials(){
return isNewImageInOpenshiftRegistry() ? "" : params.PROMOTE_IMAGE_REGISTRY_CREDENTIALS
}
String getNewImageRegistry(){
return isNewImageInOpenshiftRegistry() ? env.OPENSHIFT_REGISTRY : params.PROMOTE_IMAGE_REGISTRY
}
String getNewImageNamespace(){
return isNewImageInOpenshiftRegistry() ? "openshift" : params.PROMOTE_IMAGE_NAMESPACE
}
String getNewImageNameSuffix(){
return params.PROMOTE_IMAGE_NAME_SUFFIX
}
String getNewImageTag(){
return params.PROMOTE_IMAGE_TAG
}