-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
191 lines (177 loc) Β· 8.33 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
pipeline {
agent {
label "master"
}
environment {
// GLobal Vars
PIPELINES_NAMESPACE = "labs-ci-cd"
NAME = "sc-routes"
// Job name contains the branch eg my-app-feature%2Fjenkins-123
JOB_NAME = "${JOB_NAME}".replace("%2F", "-").replace("/", "-")
IMAGE_REPOSITORY= 'image-registry.openshift-image-registry.svc:5000'
GIT_REPO = "https://github.com/eformat/sc-routes.git#${GIT_BRANCH}"
S2I_IMAGE = 'quay.io/quarkus/ubi-quarkus-native-s2i:20.1.0-java11'
GIT_SSL_NO_VERIFY = true
// Credentials bound in OpenShift
GIT_CREDS = credentials("${PIPELINES_NAMESPACE}-git-auth")
NEXUS_CREDS = credentials("${PIPELINES_NAMESPACE}-nexus-password")
ARGOCD_CREDS = credentials("${PIPELINES_NAMESPACE}-argocd-token")
// Nexus Artifact repo
NEXUS_REPO_NAME="labs-static"
NEXUS_REPO_HELM = "helm-charts"
}
options {
buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '1'))
timeout(time: 15, unit: 'MINUTES')
}
stages {
stage('Perpare Environment') {
failFast true
parallel {
stage("Release Build") {
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("master") }
}
steps {
script {
env.TARGET_NAMESPACE = "labs-dev"
env.STAGING_NAMESPACE = "labs-staging"
env.APP_NAME = "${NAME}".replace("/", "-").toLowerCase()
}
}
}
stage("Sandbox Build") {
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("dev") || GIT_BRANCH.startsWith("feature") || GIT_BRANCH.startsWith("fix") || GIT_BRANCH.startsWith("nsfw") }
}
steps {
script {
env.TARGET_NAMESPACE = "labs-dev"
// ammend the name to create 'sandbox' deploys based on current branch
env.APP_NAME = "${GIT_BRANCH}-${NAME}".replace("/", "-").toLowerCase()
}
}
}
stage("Pull Request Build") {
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("PR-") }
}
steps {
script {
env.TARGET_NAMESPACE = "labs-dev"
env.APP_NAME = "${GIT_BRANCH}-${NAME}".replace("/", "-").toLowerCase()
}
}
}
}
}
stage("Build") {
parallel {
stage("Build App") {
agent {
node {
label "jenkins-agent-mvn"
}
}
steps {
script {
env.VERSION = readMavenPom().getVersion()
env.PACKAGE = "${NAME}-${VERSION}-runner.jar"
env.JAVA_HOME = "/usr/lib/jvm/java-11-openjdk"
}
script {
echo '### Running checkstyle ###'
// sh 'mvn checkstyle:check'
echo '### Running tests ###'
// sh 'mvn test'
echo '### Running build ###'
sh '''
mvn package -DskipTests -s ocp/settings.xml
oc get bc ${APP_NAME} || rc=$?
if [ $rc -eq 1 ]; then
echo " π no build - creating one π"
oc new-build --binary --name=${APP_NAME} -l app=${APP_NAME} --strategy=docker --dry-run -o yaml > /tmp/bc.yaml
yq w -i /tmp/bc.yaml items[1].spec.strategy.dockerStrategy.dockerfilePath Dockerfile.jvm
oc apply -f /tmp/bc.yaml
oc patch bc/${APP_NAME} -p '{"spec":{ "runPolicy": "Parallel"}}' --type=strategic
fi
echo " π build found - starting it π"
oc start-build ${APP_NAME} --from-dir=. --follow
'''
}
}
}
stage("Infinispan") {
agent {
node {
label "master"
}
}
steps {
script {
sh '''
oc -n ${TARGET_NAMESPACE} get infinispan infinispan || rc=$?
if [ $rc -eq 1 ]; then
echo " π no infinispan cluster - creating π"
oc -n ${TARGET_NAMESPACE} apply -f ocp/infinispan-subscription.yaml
oc -n ${TARGET_NAMESPACE} apply -f ocp/infinispan-operatorgroup.yaml
sleep 10
oc -n ${TARGET_NAMESPACE} wait pod -l name=infinispan-operator-alm-owned --for=condition=Ready --timeout=300s
oc -n ${TARGET_NAMESPACE} apply -f ocp/infinispan-cr.yaml
sleep 10
oc -n ${TARGET_NAMESPACE} wait pod -l app=infinispan-pod --for=condition=Ready --timeout=300s
fi
echo " π found infinispan cluster - skipping π"
'''
}
}
}
}
}
stage ("Deploy") {
agent {
node {
label "master"
}
}
steps {
script {
sh '''
oc tag ${APP_NAME}:latest ${TARGET_NAMESPACE}/${APP_NAME}:latest
oc -n ${TARGET_NAMESPACE} get dc ${APP_NAME} || rc=$?
if [ $rc -eq 1 ]; then
echo " π no deployment found - creating π"
oc -n ${TARGET_NAMESPACE} new-app ${APP_NAME} --as-deployment-config
oc -n ${TARGET_NAMESPACE} set env --from=secret/${APP_NAME} dc/${APP_NAME}
oc -n ${TARGET_NAMESPACE} set probe dc/${APP_NAME} --liveness --get-url=http://:8080/health --initial-delay-seconds=3 --timeout-seconds=1
oc -n ${TARGET_NAMESPACE} set probe dc/${APP_NAME} --readiness --get-url=http://:8080/health --initial-delay-seconds=5 --timeout-seconds=1
oc -n ${TARGET_NAMESPACE} scale dc/${APP_NAME} --replicas=2
fi
echo " π found pod waiting for deployment π"
oc -n ${TARGET_NAMESPACE} wait dc -l app=${APP_NAME} --for=condition=Available --timeout=300s
oc -n ${TARGET_NAMESPACE} get route ${APP_NAME} || rc=$?
if [ $rc -eq 1 ]; then
oc -n ${TARGET_NAMESPACE} expose svc/${APP_NAME}
oc patch route/${APP_NAME} --type=json -p '[{"op":"add", "path":"/spec/tls", "value":{"termination":"edge","insecureEdgeTerminationPolicy":"Redirect"}}]'
fi
'''
}
}
}
}
}