-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
148 lines (120 loc) · 4.37 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
node('maven-appdev') {
def stashedArtifactName = 'supplier-api-archive'
def groupId;
def artifactId;
def version;
def devTag;
def testTag;
def prodTag;
stage('Checkout Source') {
echo 'Checkout Source'
git url: 'https://github.com/briangallagher/supplier-api.git'
}
stage('Set version') {
groupId = getGroupIdFromPom("pom.xml")
artifactId = getArtifactIdFromPom("pom.xml")
version = getVersionFromPom("pom.xml")
// Set the tag for the development image: version + build number
devTag = "${version}-${BUILD_NUMBER}"
testTag = "${version}-${BUILD_NUMBER}"
// Set the tag for the production image: version
prodTag = "${version}"
echo " Dev TAG: ${devTag}"
echo " Test TAG: ${testTag}"
echo " Prod TAG: ${prodTag}"
}
stage('Build Source') {
echo 'Build Source'
sh "mvn clean package -DskipTests"
stash name: stashedArtifactName, includes: "target/*"
}
stage('Unit Test Source') {
sh "mvn test"
}
stage('Build Image') {
echo 'Build Image'
unstash name: stashedArtifactName
openshift.withCluster() {
openshift.withProject('arcadia-dev') {
openshift.selector("bc/supplier-api").startBuild("--from-archive=./target/supplier-api-"+prodTag+".jar", "--wait")
openshift.tag("arcadia-dev/supplier-api:latest", "arcadia-dev/supplier-api:" + devTag)
openshift.tag("arcadia-dev/supplier-api:latest", "arcadia-test/supplier-api:" + testTag)
openshift.tag("arcadia-test/supplier-api:" + testTag, "arcadia-test/supplier-api:latest")
}
}
}
stage('Deploy Image to Dev') {
echo 'Deploy Image to Dev'
deployToEnvironment(project: 'arcadia', env: 'dev', appName: 'supplier-api')
}
stage('Integration Testing') {
echo 'Integration Testing'
}
stage('Deploy Image to Test') {
echo 'Deploy Image to Test'
deployToEnvironment(project: 'arcadia', env: 'test', appName: 'supplier-api')
}
stage('UAT Testing') {
echo 'UAT Testing'
}
stage('Deploy Image to Production') {
echo 'Deploy Image to Production'
// TODO: Blue / Green
}
}
// Convenience Functions to read variables from the pom.xml.
// Do not change anything below this line.
def getVersionFromPom(pom) {
def matcher = readFile(pom) =~ '<version>(.+)</version>'
matcher ? matcher[0][1] : null
}
def getGroupIdFromPom(pom) {
def matcher = readFile(pom) =~ '<groupId>(.+)</groupId>'
matcher ? matcher[0][1] : null
}
def getArtifactIdFromPom(pom) {
def matcher = readFile(pom) =~ '<artifactId>(.+)</artifactId>'
matcher ? matcher[0][1] : null
}
def deployToEnvironment(Map params) {
echo "Deploying application ${params.appName} (project '${params.project}') to ${params.env}"
def namespace = "${params.project}-${params.env}"
setConfiguration(appName: params.appName, dcName: params.appName, env: params.env, namespace: namespace)
openshift.withCluster() {
openshift.withProject(namespace) {
echo "Rolling out a new application deployment"
def dc = openshift.selector( "dc/${params.appName}")
dc.rollout().latest()
// Wait for the deployment to complete
dc.rollout().status()
}
}
}
// This module does the following:
// 1. In the root of the source code finds the files `.env.<ENV>`, where
// <ENV> is the name of the environment (e.g. .env.dev, .env.ppte etc.)
//
// 2. Creates a ConfigMap from the file
//
// 3. Applies the ConfigMap to the DeploymentConfig
def setConfiguration(Map params) {
echo "Updating envrionment variables ConfigMap in namespace ${params.namespace}"
openshift.withCluster() {
openshift.withProject() {
// openshift.withProject(params.namespace) {
echo "Setting Configuration"
// def envvarFile = ".env.${params.env}"
// def configmapName = "${params.appName}-envvars"
// def configMapEnv = openshift.selector("configmap/${configmapName}")
// if (configMapEnv.exists()) {
// configMapEnv.delete()
// }
// if (fileExists(envvarFile)) {
// openshift.create("configmap", configmapName, "--from-env-file=${envvarFile}")
// openshift.set("env", "dc/${params.dcName}", "--from=configmap/${configmapName}")
// } else {
// echo "The environment variables file ${envvarFile} doesn't exist"
// }
}
}
}