-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
179 lines (170 loc) · 7.62 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
def applicationName = 'my-php-app'
def baseImage = "php";
def buildImage = "${baseImage}:latest";
def secretName = "git-repo-secret";
def createProject(openshift, project) {
try {
openshift.newProject(project)
} catch (Exception e) {
if( !e.getMessage().contains("AlreadyExists")) throw e;
}
}
pipeline {
agent none
options {
timeout(time: 20, unit: 'MINUTES')
}
stages {
stage('initialisation') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
echo "Using project: ${openshift.project()}"
}
}
}
}
}
stage('create environments') {
/*agent {
label "maven"
}*/
steps {
script {
openshift.withCluster() {
createProject(openshift, "${applicationName}-dev")
createProject(openshift, "${applicationName}-staging")
createProject(openshift, "${applicationName}-uat")
createProject(openshift, "${applicationName}-prod")
}
}
}
}
stage('sub-builds creation') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
def bc = openshift.selector("bc", applicationName);
if (!bc.exists()) {
echo "BC does not exist...creating it"
def scmUrl = scm.getUserRemoteConfigs()[0].getUrl();
openshift.newBuild("--strategy=source",
"${buildImage}~${scmUrl}",
"--context-dir=${applicationName}", "--name=${applicationName}", "--source-secret=${secretName}",
"--to=${applicationName}", "--env=GIT_SSL_NO_VERIFY=true")
} else {
echo "BC does exist...updating it if necessary"
def bcObject = bc.object();
def currentBuildImage = bcObject.spec.strategy.sourceStrategy.from.name;
if (currentBuildImage != buildImage ) {
bcObject.spec.strategy.sourceStrategy.from.name = buildImage;
openshift.apply(bcObject);
}
}
}
}
}
}
}
stage('build') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
def build = openshift.selector("bc", applicationName);
build.startBuild();
def builds = build.related('builds')
timeout(5) {
builds.untilEach(1) {
def status = it.object().status.phase;
echo "Build in status: ${status}"
return (status == "Complete" || status == "Cancelled")
}
}
}
}
}
}
}
stage('tag') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
def now = new Date();
def tag = now.format("yyyMMdd-HHmmss", TimeZone.getTimeZone('UTC'));
openshift.tag("${applicationName}:latest", "${applicationName}-dev/${applicationName}:latest-dev")
openshift.tag("${applicationName}:latest", "${applicationName}-staging/${applicationName}:latest-staging")
openshift.tag("${applicationName}:latest", "${applicationName}-uat/${applicationName}:latest-uat")
openshift.tag("${applicationName}:latest", "${applicationName}-prod/${applicationName}:latest-prod")
//openshift.tag("${applicationName}:latest", "${applicationName}-staging:latest")
//openshift.tag("${applicationName}:latest", "${applicationName}:${baseImage}-${tag}")
}
}
}
}
}
stage('create application') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${applicationName}-dev") {
def dc = openshift.selector("dc", applicationName);
if (!dc.exists()) {
openshift.newApp(applicationName).narrow('svc').expose();
} else {
def route = openshift.selector("route", applicationName);
if (!route.exists()) {
def svc = openshift.selector("svc", applicationName);
svc.expose();
}
}
}
}
}
}
}
stage('deploy') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${applicationName}-dev") {
def rm = openshift.selector("dc", applicationName).rollout()
timeout(5) {
openshift.selector("dc", applicationName).related('pods').untilEach(1) {
return (it.object().status.phase == "Running")
}
}
}
}
}
}
}
stage('template generation') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${applicationName}-dev") {
def dc = openshift.selector("dc").objects(exportable:true)
def svc = openshift.selector("svc").objects(exportable:true)
def cm = openshift.selector("cm").objects(exportable:true)
def is = openshift.selector("is").objects(exportable:true)
def routes = openshift.selector("routes").objects(exportable:true)
def templateObject = openshift.selector( "template", applicationName)
if( templateObject.exists() ){
templateObject.delete()
}
def objects = dc + svc + cm + is + routes;
def template = [[ "kind":"Template", "apiVersion":"v1", "objects": objects,
"metadata":[ "name":"${applicationName}", "annotations": [ "iconClass": "icon-php"],
"labels":[ "template":"${applicationName}" ]]]]
openshift.create(template);
}
}
}
}
}
}
}