Skip to content

Commit b29b93a

Browse files
authored
Merge pull request openshift#3380 from csrwng/add-jenkinsfile
Add jenkinsfile and openshift template
2 parents 343e505 + 987899a commit b29b93a

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed

Jenkinsfile

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env groovy
2+
3+
// Pipeline variables
4+
def isPR=false // true if the branch being tested belongs to a PR
5+
def project="" // project where build and deploy will occur
6+
def projectCreated=false // true if a project was created by this build and needs to be cleaned up
7+
def repoUrl="" // the URL of this project's repository
8+
def appName="openshift-docs" // name of application to create
9+
def approved=false // true if the preview was approved
10+
11+
// uniqueName returns a name with a 16-character random character suffix
12+
def uniqueName = { String prefix ->
13+
sh "cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1 > suffix"
14+
suffix = readFile("suffix").trim()
15+
return prefix + suffix
16+
}
17+
18+
// setBuildStatus sets a status item on a GitHub commit
19+
def setBuildStatus = { String url, String context, String message, String state, String backref ->
20+
step([
21+
$class: "GitHubCommitStatusSetter",
22+
reposSource: [$class: "ManuallyEnteredRepositorySource", url: url ],
23+
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context ],
24+
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
25+
statusBackrefSource: [ $class: "ManuallyEnteredBackrefSource", backref: backref ],
26+
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [
27+
[$class: "AnyBuildResult", message: message, state: state]] ]
28+
]);
29+
}
30+
31+
// getRepoURL retrieves the origin URL of the current source repository
32+
def getRepoURL = {
33+
sh "git config --get remote.origin.url > originurl"
34+
return readFile("originurl").trim()
35+
}
36+
37+
// getRouteHostname retrieves the host name from the given route in an
38+
// OpenShift namespace
39+
def getRouteHostname = { String routeName, String projectName ->
40+
sh "oc get route ${routeName} -n ${projectName} -o jsonpath='{ .spec.host }' > apphost"
41+
return readFile("apphost").trim()
42+
}
43+
44+
// setPreviewStatus sets a status item for each openshift-docs release
45+
def setPreviewStatus = { String url, String message, String state, String host, boolean includeLink ->
46+
setBuildStatus(url, "ci/app-preview/origin", message, state, includeLink ? "http://${host}/openshift-origin/latest/welcome/index.html" : "")
47+
setBuildStatus(url, "ci/app-preview/enterprise", message, state, includeLink ? "http://${host}/openshift-enterprise/master/welcome/index.html" : "")
48+
setBuildStatus(url, "ci/app-preview/online", message, state, includeLink ? "http://${host}/openshift-online/master/welcome/index.html" : "")
49+
setBuildStatus(url, "ci/app-preview/dedicated", message, state, includeLink ? "http://${host}/openshift-dedicated/master/welcome/index.html" : "")
50+
setBuildStatus(url, "ci/app-preview/registry", message, state, includeLink ? "http://${host}/atomic-registry/latest/registry_quickstart/index.html" : "")
51+
}
52+
53+
try { // Use a try block to perform cleanup in a finally block when the build fails
54+
55+
node {
56+
// Initialize variables in default node context
57+
isPR = env.BRANCH_NAME ? env.BRANCH_NAME.startsWith("PR") : false
58+
baseProject = env.PROJECT_NAME
59+
project = env.PROJECT_NAME
60+
61+
stage ('Checkout') {
62+
checkout scm
63+
repoUrl = getRepoURL()
64+
}
65+
66+
// When testing a PR, create a new project to perform the build
67+
// and deploy artifacts.
68+
if (isPR) {
69+
stage ('Create PR Project') {
70+
setPreviewStatus(repoUrl, "Building application", "PENDING", "", false)
71+
setBuildStatus(repoUrl, "ci/approve", "Aprove after testing", "PENDING", "")
72+
project = uniqueName("${appName}-")
73+
sh "oc new-project ${project}"
74+
projectCreated=true
75+
sh "oc policy add-role-to-group view system:authenticated -n ${project}"
76+
}
77+
}
78+
79+
stage ('Apply object configurations') {
80+
sh "oc process -f _openshift/docs-template.yaml -n ${project} | oc apply -f - -n ${project}"
81+
}
82+
83+
stage ('Build') {
84+
sh "oc start-build ${appName} -n ${project} --from-repo=. --follow"
85+
}
86+
87+
88+
if (isPR) {
89+
stage ('Verify Service') {
90+
openshiftVerifyService serviceName: appName, namespace: project
91+
}
92+
def appHostName = getRouteHostname(appName, project)
93+
setPreviewStatus(repoUrl, "The application is available", "SUCCESS", "${appHostName}", true)
94+
setBuildStatus(repoUrl, "ci/approve", "Approve after testing", "PENDING", "${env.BUILD_URL}input/")
95+
stage ('Manual Test') {
96+
timeout(time:2, unit:'DAYS') {
97+
input "Is everything OK?"
98+
}
99+
}
100+
approved = true
101+
setPreviewStatus(repoUrl, "Application previewed", "SUCCESS", "", false)
102+
setBuildStatus(repoUrl, "ci/approve", "Manually approved", "SUCCESS", "")
103+
}
104+
}
105+
}
106+
finally {
107+
if (projectCreated) {
108+
node {
109+
stage('Delete PR Project') {
110+
if (!approved) {
111+
setPreviewStatus(repoUrl, "Application previewed", "FAILURE", "", false)
112+
setBuildStatus(repoUrl, "ci/approve", "Rejected", "FAILURE", "")
113+
}
114+
sh "oc delete project ${project}"
115+
}
116+
}
117+
}
118+
}

_openshift/docs-template.yaml

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
apiVersion: v1
2+
kind: Template
3+
metadata:
4+
name: openshift-docs
5+
parameters:
6+
- name: GIT_URI
7+
displayName: Git Uri
8+
value: https://github.com/openshift/openshift-docs.git
9+
- name: GIT_REF
10+
displayName: Git Reference
11+
value: master
12+
objects:
13+
- apiVersion: v1
14+
kind: ImageStream
15+
metadata:
16+
labels:
17+
app: openshift-docs
18+
name: openshift-docs
19+
spec: {}
20+
- apiVersion: v1
21+
kind: ImageStream
22+
metadata:
23+
labels:
24+
build: openshift-docs-s2i
25+
name: openshift-docs-s2i
26+
spec:
27+
tags:
28+
- from:
29+
kind: DockerImage
30+
name: docker.io/cewong/asciibinder-018-centos7:latest
31+
name: latest
32+
- apiVersion: v1
33+
kind: BuildConfig
34+
metadata:
35+
annotations:
36+
labels:
37+
app: openshift-docs
38+
name: openshift-docs
39+
spec:
40+
output:
41+
to:
42+
kind: ImageStreamTag
43+
name: openshift-docs:latest
44+
runPolicy: Serial
45+
source:
46+
git:
47+
uri: ${GIT_URI}
48+
ref: ${GIT_REF}
49+
type: Git
50+
strategy:
51+
sourceStrategy:
52+
from:
53+
kind: ImageStreamTag
54+
name: openshift-docs-s2i:latest
55+
type: Source
56+
triggers: {}
57+
- apiVersion: v1
58+
kind: DeploymentConfig
59+
metadata:
60+
labels:
61+
app: openshift-docs
62+
name: openshift-docs
63+
spec:
64+
replicas: 1
65+
selector:
66+
app: openshift-docs
67+
deploymentconfig: openshift-docs
68+
strategy:
69+
resources: {}
70+
recreateParams: {}
71+
type: Recreate
72+
template:
73+
metadata:
74+
labels:
75+
app: openshift-docs
76+
deploymentconfig: openshift-docs
77+
spec:
78+
containers:
79+
- image: " "
80+
imagePullPolicy: IfNotPresent
81+
name: openshift-docs
82+
ports:
83+
- containerPort: 8080
84+
protocol: TCP
85+
resources: {}
86+
livenessProbe:
87+
httpGet:
88+
path: /
89+
port: 8080
90+
scheme: HTTP
91+
initialDelaySeconds: 5
92+
timeoutSeconds: 1
93+
periodSeconds: 5
94+
successThreshold: 1
95+
failureThreshold: 3
96+
readinessProbe:
97+
tcpSocket:
98+
port: 8080
99+
initialDelaySeconds: 3
100+
timeoutSeconds: 1
101+
periodSeconds: 10
102+
successThreshold: 1
103+
failureThreshold: 3
104+
terminationMessagePath: /dev/termination-log
105+
dnsPolicy: ClusterFirst
106+
restartPolicy: Always
107+
securityContext: {}
108+
terminationGracePeriodSeconds: 30
109+
test: false
110+
triggers:
111+
- type: ConfigChange
112+
- imageChangeParams:
113+
automatic: true
114+
containerNames:
115+
- openshift-docs
116+
from:
117+
kind: ImageStreamTag
118+
name: openshift-docs:latest
119+
type: ImageChange
120+
status: {}
121+
- apiVersion: v1
122+
kind: Service
123+
metadata:
124+
labels:
125+
app: openshift-docs
126+
name: openshift-docs
127+
spec:
128+
ports:
129+
- name: 8080-tcp
130+
port: 8080
131+
protocol: TCP
132+
targetPort: 8080
133+
selector:
134+
app: openshift-docs
135+
deploymentconfig: openshift-docs
136+
sessionAffinity: None
137+
type: ClusterIP
138+
- apiVersion: v1
139+
kind: Route
140+
metadata:
141+
labels:
142+
app: openshift-docs
143+
name: openshift-docs
144+
spec:
145+
port:
146+
targetPort: 8080-tcp
147+
to:
148+
kind: Service
149+
name: openshift-docs
150+
weight: 100
151+
wildcardPolicy: None

0 commit comments

Comments
 (0)