forked from redhat-cop/uncontained.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
221 lines (204 loc) · 7.66 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
216
217
218
219
220
221
@Library('cop-library') _
openshift.withCluster() {
env.localToken = readFile('/var/run/secrets/kubernetes.io/serviceaccount/token').trim()
env.NAMESPACE = openshift.project()
}
//n.notifyBuild('STARTED', rocketchat_url)
pipeline {
agent {
label 'hugo-builder'
}
stages {
stage ('Fetch Source Code') {
steps {
script {
hygieiaBuildPublishStep buildStatus: 'InProgress'
git url: "${APPLICATION_SOURCE_REPO}", branch: "${APPLICATION_SOURCE_REF}"
}
}
}
stage ('Build Site from Source') {
steps {
container('builder') {
sh '''
source /opt/rh/rh-git29/enable
npm install
npm run build
'''
}
}
post {
failure {
hygieiaBuildPublishStep buildStatus: 'Failure'
}
aborted {
hygieiaBuildPublishStep buildStatus: 'Aborted'
}
success {
hygieiaBuildPublishStep buildStatus: 'Success'
}
}
}
stage ('Build and Test in Parallel') {
parallel {
stage ('Run Automated Tests') {
steps {
container('builder') {
sh 'npm test'
}
}
}
stage ('Build Container Image') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
openshift.selector("bc", "${APP_NAME}").startBuild("--from-dir=dist").logs("-f")
}
}
}
}
}
}
}
stage ('Deploy to Dev'){
steps {
script {
openshift.withCluster() {
openshift.withProject() {
echo "Promoting via tag from ${NAMESPACE} to ${DEV_NAMESPACE}/${APP_NAME}"
tagImage(sourceImagePath: "${NAMESPACE}", sourceImageName: "${APP_NAME}", toImagePath: "${DEV_NAMESPACE}", toImageName: "${APP_NAME}", toImageTag: "latest")
}
}
}
}
}
stage ('Verify Deployment to Dev') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${DEV_NAMESPACE}"){
// Verify all pods come up healthy
def dcObj = openshift.selector('dc', APP_NAME).object()
def podSelector = openshift.selector('pod', [deployment: "${APP_NAME}-${dcObj.status.latestVersion}"])
podSelector.untilEach {
echo "pod: ${it.name()}"
return it.object().status.containerStatuses[0].ready
}
// Smoke Test
def routeObj = openshift.selector('route', APP_NAME).object()
def proto = 'http://'
if (routeObj.spec.tls != null) {
proto = 'https://'
}
env.TEST_URL = "${proto}${routeObj.spec.host}"
}
}
}
container('builder') {
sh "export TEST_URL=${env.TEST_URL} && npm run smoke"
}
}
post {
success {
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: 'dist/', artifactGroup: 'uncontained.io', artifactName: 'index.html', artifactVersion: "${BUILD_NUMBER}-${DEV_NAMESPACE}", buildStatus: 'Success', environmentName: "${DEV_NAMESPACE}"
}
}
}
stage('Promotions') {
agent {
kubernetes {
label 'promotion-slave'
cloud 'openshift'
serviceAccount 'jenkins'
containerTemplate {
name 'jnlp'
image "docker-registry.default.svc:5000/${NAMESPACE}/jenkins-slave-image-mgmt"
alwaysPullImage true
workingDir '/tmp'
args '${computer.jnlpmac} ${computer.name}'
ttyEnabled false
}
}
}
stages {
stage('Promote to Test') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
echo "Promoting via tag from ${NAMESPACE} to ${TEST_NAMESPACE}/${APP_NAME}"
tagImage(sourceImagePath: "${NAMESPACE}", sourceImageName: "${APP_NAME}", toImagePath: "${TEST_NAMESPACE}", toImageName: "${APP_NAME}", toImageTag: "latest")
}
}
sh 'mkdir -p dist/ && touch dist/index.html'
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: 'dist/', artifactGroup: 'uncontained.io', artifactName: 'index.html', artifactVersion: "${BUILD_NUMBER}-${TEST_NAMESPACE}", buildStatus: 'Success', environmentName: "${TEST_NAMESPACE}"
}
}
}
stage('Promote to Stage') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
echo "Promoting via tag from ${NAMESPACE} to ${STAGE_NAMESPACE}/${APP_NAME}"
tagImage(sourceImagePath: "${NAMESPACE}", sourceImageName: "${APP_NAME}", toImagePath: "${STAGE_NAMESPACE}", toImageName: "${APP_NAME}", toImageTag: "latest")
}
}
sh 'mkdir -p dist/ && touch dist/index.html'
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: 'dist/', artifactGroup: 'uncontained.io', artifactName: 'index.html', artifactVersion: "${BUILD_NUMBER}-${STAGE_NAMESPACE}", buildStatus: 'Success', environmentName: "${STAGE_NAMESPACE}"
}
}
}
stage('Promote to Prod') {
when {
beforeAgent true
allOf{
environment name: 'APPLICATION_SOURCE_REF', value: 'master';
environment name: 'APPLICATION_SOURCE_REPO', value: 'https://github.com/redhat-cop/uncontained.io.git'
}
}
steps {
script {
openshift.withCluster() {
def secretData = openshift.selector('secret/other-cluster-credentials').object().data
def encodedRegistry = secretData.registry
def encodedToken = secretData.token
def encodedAPI = secretData.api
env.registry = sh(script:"set +x; echo ${encodedRegistry} | base64 --decode", returnStdout: true)
env.token = sh(script:"set +x; echo ${encodedToken} | base64 --decode", returnStdout: true)
env.api = sh(script:"set +x; echo ${encodedAPI} | base64 --decode", returnStdout: true)
openshift.withProject() {
def imageRegistry = openshift.selector( 'is', "${APP_NAME}").object().status.dockerImageRepository
echo "Promoting ${imageRegistry} -> ${registry}/${PROD_NAMESPACE}/${APP_NAME}"
sh """
set +x
skopeo copy --remove-signatures \
--src-creds openshift:${localToken} --src-cert-dir=/run/secrets/kubernetes.io/serviceaccount/ \
--dest-creds openshift:${token} --dest-tls-verify=false \
docker://${imageRegistry} docker://${registry}/${PROD_NAMESPACE}/${APP_NAME}
"""
sh 'mkdir -p dist/ && touch dist/index.html'
hygieiaDeployPublishStep applicationName: "${APP_NAME}", artifactDirectory: 'dist/', artifactGroup: 'uncontained.io', artifactName: 'index.html', artifactVersion: "${BUILD_NUMBER}-${PROD_NAMESPACE}", buildStatus: 'Success', environmentName: "${PROD_NAMESPACE}"
}
}
}
}
}
}
}
}
/*
post {
success {
script {
//n.notifyBuild('SUCCESSFUL', rocketchat_url)
}
}
failure {
script {
//n.notifyBuild('FAILED', rocketchat_url)
}
}
}*/
}