forked from bparees/openshift-jenkins-s2i-config
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Jenkinsfile
197 lines (171 loc) · 6.74 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
#!/usr/bin/groovy
@Library('github.com/fabric8io/fabric8-pipeline-library@master')
def repo = 'openshift-jenkins-s2i-config'
def org = 'fabric8io'
def project = org + '/' + repo
def flow = new io.fabric8.Fabric8Commands()
def baseImageVerion = "v0072490"
def deploySnapshot = false
def pipeline
def snapshotImageName
dockerTemplate{
s2iNode{
checkout scm
if (env.BRANCH_NAME.startsWith('PR-')) {
echo 'Running CI pipeline'
snapshot = true
snapshotImageName = "fabric8/jenkins-openshift:SNAPSHOT-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
stage ('build'){
container('s2i') {
sh "s2i build . fabric8/jenkins-openshift-base:${baseImageVerion} ${snapshotImageName} --copy"
}
}
stage ('push snapshot to dockerhub'){
container('docker') {
sh "docker push ${snapshotImageName}"
}
}
stage('notify'){
def changeAuthor = env.CHANGE_AUTHOR
if (!changeAuthor){
error "no commit author found so cannot comment on PR"
}
def pr = env.CHANGE_ID
if (!pr){
error "no pull request number found so cannot comment on PR"
}
def message = "@${changeAuthor} snapshot Jenkins image is available. `docker pull ${snapshotImageName}`"
container('docker'){
flow.addCommentToPullRequest(message, pr, project)
}
}
// diabled the godog test for now lets revisit and check if required.
deploySnapshot = false
} else if (env.BRANCH_NAME.equals('master')) {
echo 'Running CD pipeline'
def newVersion = getNewVersion {}
stage ('s2i build'){
container('s2i') {
sh "s2i build . fabric8/jenkins-openshift-base:${baseImageVerion} fabric8/jenkins-openshift:${newVersion} --copy"
}
}
stage ('push to dockerhub'){
container('docker') {
sh "docker push fabric8/jenkins-openshift:${newVersion}"
sh "docker tag fabric8/jenkins-openshift:${newVersion} fabric8/jenkins-openshift:latest"
sh 'docker push fabric8/jenkins-openshift:latest'
}
}
pushPomPropertyChangePR {
propertyName = 'jenkins-openshift.version'
projects = [
'fabric8-services/fabric8-tenant-jenkins'
]
version = newVersion
containerName = 's2i'
}
}
}
}
if (deploySnapshot){
stage ('deploy and test snapshot'){
def namespace = 'jenkins-'+ env.BRANCH_NAME
namespace = namespace.toLowerCase()
parallel openshift: {
def containerName = 'os'
deployRemoteClusterNode(configSecretName: 'fabric8-intcluster-config', containerName: containerName, label: "jen_os_${env.CHANGE_ID}_${env.BUILD_NUMBER}"){
cleanWs()
deployRemoteOpenShift()
}
}, kubernetes: {
def containerName = 'k8s'
deployRemoteClusterNode(configSecretName: 'tiger-config', containerName: containerName, label: "jen_k8s_${env.CHANGE_ID}_${env.BUILD_NUMBER}"){
cleanWs()
deployRemoteKubernetes(snapshotImageName, namespace)
}
},
failFast: false
}
}
def deployRemoteOpenShift(){
container('os'){
echo 'TODO run BDD tests on OpenShift'
sh 'oc version'
}
}
def deployRemoteKubernetes(snapshotImageName, deployNamespace){
withCredentials([usernamePassword(credentialsId: 'test-user', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
withCredentials([usernamePassword(credentialsId: 'cd-github', passwordVariable: 'GH_PASS', usernameVariable: 'GH_USER')]) {
def nexusServiceLink = """
apiVersion: v1
kind: List
items:
- kind: Service
apiVersion: v1
metadata:
name: content-repository
spec:
type: ExternalName
externalName: artifact-repository.shared
ports:
- port: 80
"""
def addCredsScript = """
{
\"credentials\": {
\"scope\": \"GLOBAL\",
\"id\": \"cd-github\",
\"username\": \"${GH_USER}\",
\"password\": \"${GH_PASS}\",
\"\$class\": \"com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl\"
}
}
"""
def map = [:]
map["TEST_ADMIN_PASSWORD"] = PASS
map["EXTERNAL_DOCKER_REGISTRY_URL"] = '10.7.240.40:80' // this is nexus in the shared ns on test cluster, find a nice way to look this up so it's not hardcoded.
//swizzle the image name and deploy snashot
ing = deployKubernetesSnapshot{
mavenRepo = 'http://central.maven.org/maven2/io/fabric8/apps/jenkins'
githubRepo = 'openshift-jenkins-s2i-config'
originalImageName = 'fabric8/jenkins-openshift'
newImageName = snapshotImageName
namespace = deployNamespace
appToDeploy = 'jenkins'
project = 'fabric8io/openshift-jenkins-s2i-config'
clusterName = 'tiger'
clusterZone = 'europe-west1-b'
extraYAML = nexusServiceLink
templateParameters = map
}
map = null
sh "curl -X POST 'http://${USER}:${PASS}@${ing}/credentials/store/system/domain/_/createCredentials' --data-urlencode 'json=${addCredsScript}'"
try {
def buildPath = '/go/src/github.com/fabric8-jenkins/godog-jenkins'
container('k8s'){
sh "mkdir -p ${buildPath}"
sh "git clone https://github.com/fabric8-jenkins/godog-jenkins ${buildPath}"
sh 'ls -al'
sh "cd ${buildPath}/jenkins && GITHUB_USER=${GH_USER} GITHUB_PASSWORD=${GH_PASS} BDD_JENKINS_URL=http://${ing} BDD_JENKINS_USERNAME=admin BDD_JENKINS_TOKEN=${env.PASS} godog"
}
} finally {
notify('k8s', ing)
}
}
}
}
def notify(containerName, url){
def flow = new io.fabric8.Fabric8Commands()
def changeAuthor = env.CHANGE_AUTHOR
if (!changeAuthor){
error "no commit author found so cannot comment on PR"
}
def pr = env.CHANGE_ID
if (!pr){
error "no pull request number found so cannot comment on PR"
}
def message = "Snapshot Jenkins is deployed and running [HERE](http://${url})"
container(containerName){
flow.addCommentToPullRequest(message, pr, 'fabric8io/openshift-jenkins-s2i-config')
}
}