-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjenkins-pipeline-template.yaml
153 lines (137 loc) · 5.07 KB
/
jenkins-pipeline-template.yaml
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
apiVersion: v1
kind: Template
metadata:
name: jenkins-buildconfig
namespace: openshift
description: |-
BuildConfig for Jenkins based on custom code usng s2i
iconClass: icon-jenkins
openshift.io/display-name: Jenkins BuildConfig
openshift.io/provider-display-name: cloudowski.com
tags: jenkins, build
objects:
- apiVersion: v1
kind: BuildConfig
metadata:
name: jenkins-${GIT_BRANCH}
namespace: ${PROJECT}
spec:
source:
git:
uri: ${GIT_REPO}
ref: ${GIT_BRANCH}
type: Git
strategy:
type: Source
sourceStrategy:
from:
kind: ImageStreamTag
name: jenkins:latest
namespace: openshift
output:
to:
kind: ImageStreamTag
name: jenkins-${GIT_BRANCH}:latest
namespace: ${PROJECT}
triggers: []
- apiVersion: v1
kind: ImageStream
metadata:
labels:
build: jenkins-${GIT_BRANCH}
name: jenkins-${GIT_BRANCH}
namespace: ${PROJECT}
spec:
lookupPolicy:
local: false
referencePolicy:
type: Local
- apiVersion: "v1"
kind: "BuildConfig"
metadata:
name: "build-image-jenkins-${GIT_BRANCH}"
namespace: ${PROJECT}
spec:
source:
git:
uri: ${GIT_REPO}
ref: ${GIT_BRANCH}
type: Git
strategy:
jenkinsPipelineStrategy:
jenkinsfile: |-
def dateFormat = new java.text.SimpleDateFormat("yyyyMMdd'-'HHmmss")
snapshotVersion = dateFormat.format(new Date())
pipeline {
agent any
stages {
stage('Build Image') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${PROJECT}") {
def build = openshift.selector("bc", "jenkins-${GIT_BRANCH}").startBuild()
def build_logs = build.logs("-f")
// sometimes status is 'Running' after it has actually finished
sleep 2
def build_obj = build.object()
echo "Build status: ${build_obj.status.phase}"
if (build_obj.status.phase != 'Complete' || build_obj.status.phase == 'Running') {
error 'Build failed'
}
imageDigest = build_obj.status.output.to.imageDigest
echo "Tagging ${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest} as ${PROJECT}/jenkins-${GIT_BRANCH}:${snapshotVersion} (and ${PROJECT}/jenkins-${GIT_BRANCH}:latest)"
openshift.tag("${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest}", "${PROJECT}/jenkins-${GIT_BRANCH}:latest")
openshift.tag("${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest}", "${PROJECT}/jenkins-${GIT_BRANCH}:${snapshotVersion}")
}
}
}
}
}
stage('Publish?') {
agent any
when {
expression { "${GIT_BRANCH}" == "master" }
}
input {
id "publishImageTag"
message "Publish 'jenkins-${GIT_BRANCH}:${snapshotVersion}' as latest? Enter destination tag"
parameters {
string(defaultValue: 'latest', description: 'Tag for the destination Image ', name: 'IMAGETAG')
}
}
steps {
script {
imageTag = "${IMAGETAG}"
}
echo "Provided imagetag: ${IMAGETAG}"
}
}
stage('Publish Image') {
when {
expression { "${GIT_BRANCH}" == "master" }
}
steps {
script {
openshift.withCluster() {
openshift.withProject("openshift") {
echo "Tagging ${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest} as openshift/jenkins:${imageTag} (and openshift/jenkins:${snapshotVersion})"
openshift.tag("--reference-policy=local","${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest}", "openshift/jenkins:${imageTag}")
openshift.tag("--reference-policy=local","${PROJECT}/jenkins-${GIT_BRANCH}@${imageDigest}", "openshift/jenkins:${snapshotVersion}")
}
}
}
}
}
}
}
parameters:
- name: GIT_BRANCH
description: "Name of branch"
value: master
- name: PROJECT
description: "Name of the destination project"
value: jenkins-builder
- name: GIT_REPO
description: "URL of the repository with Jenkins configuration"
value: https://github.com/cloudowski/jenkins-openshift-pipeline