-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
106 lines (95 loc) · 4.32 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
#!groovy
pipeline {
agent any
parameters {
string(name: 'Branch', defaultValue: 'master', description: 'The branch/commit/tag to get the Jenkinsfile from')
choice(name: 'target', choices: ['dev', 'test', 'prod'], description: 'The environment to deploy to')
choice(name: 'stage', choices: ['default','dev', 'test', 'prod'], description: 'The environment to deploy to')
string(name: 'version', defaultValue: 'latest', description: 'The image version to release', trim: true)
booleanParam(name: 'full', defaultValue: false, description: 'True if all Openshift resource shall eb dropped and recreated')
}
environment {
MEGA_PROJECT = "57-mega-"
TARGET_PROJECT = "${env.MEGA_PROJECT + params.target}"
OCP_ROUTE_SUFFIX = "${env.TARGET_PROJECT}.cloud.itandtel.at"
}
options {
disableConcurrentBuilds()
}
stages {
stage('Prepare') {
steps {
script {
sh "oc project ${env.TARGET_PROJECT}"
}
}
}
stage('Recreate') {
when {
expression { params.full }
}
steps {
script {
dir('apps/mega-zep') {
openshift.withCluster() {
// Frontend
deleteServiceResources("mega-zep-frontend", params.target)
createServiceResources("mega-zep-frontend", params.target)
// Backend
deleteServiceResources("mega-zep-backend", params.target)
def frontendProps = readServiceProperties("mega-zep-frontend", ".", params.target)
def service = "https://${frontendProps.NAME}-${env.OCP_ROUTE_SUFFIX}"
def stage = (params.stage == "default" ? params.target : params.stage) + "stage"
createServiceResources("mega-zep-backend",
params.target,
"--param=JAVA_OPTIONS='-Dquarkus.profile=${stage} -Djava.net.preferIPv4Stack=true -Dquarkus.http.cors.origins=${service}'")
}
}
}
}
}
stage('Tag-Image') {
when {
expression { !params.full }
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
openshift.withCluster() {
openshift.tag("docker.io/${USERNAME}/mega-zep-frontend:${params.version}", "${env.TARGET_PROJECT}/mega-zep-frontend:${params.target}")
openshift.tag("docker.io/${USERNAME}/mega-zep-backend:${params.version}", "${env.TARGET_PROJECT}/mega-zep-backend:${params.target}")
}
}
}
}
}
}
}
def deleteServiceResources(String service, String stage = "") {
if (stage != "") {
stage = "." + stage
}
sh "oc process --filename ${service}.yaml \
--param-file=${service}${stage}.properties \
--param=IMAGE_TAG=${params.target} \
| oc delete --ignore-not-found --filename -"
sh "oc delete is/${service} --ignore-not-found=true"
}
def createServiceResources(String service, String stage = "", String additionalParams = '') {
if (stage != "") {
stage = "." + stage
}
sh "oc process --filename ${service}.yaml \
--param-file=${service}${stage}.properties --param=IMAGE_TAG=${params.target} ${additionalParams} \
| oc create --filename -"
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh "oc create is ${service} && oc import-image ${service}:${params.target} --from=docker.io/${USERNAME}/${service}:${params.version}"
}
}
def readServiceProperties(String service, String path = ".", String stage = "") {
if (stage != "") {
stage = "." + stage
}
def props = readProperties file: "${path}/${service}${stage}.properties"
return props
}