-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJenkinsfile
260 lines (224 loc) · 8.36 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!groovy
import groovy.json.JsonSlurperClassic
node {
def configuration
def buildStatus = BuildStatus.Ok
timestamps {
stage('Checkout') {
cleanDir(env.WORKSPACE)
checkoutComponents(env.COMPONENTS)
configuration = getConfiguration('BuildConfiguration.json')
}
try {
stage('Build') {
for(def component : configuration.components ) {
def solution = "${component.name}\\${component.solution}"
bat "\"${tool 'nuget'}\" restore $solution"
bat "\"${tool 'msbuild'}\" $solution ${component.properties} /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"
}
}
if(configuration.build.tests) {
stage('Tests') {
dir(env.WORKSPACE){
bat """${tool 'nunit'} ${getFilePaths(configuration.tests.wildcards).join(' ')} --work=${configuration.reports}"""
nunit testResultsPattern: "${configuration.reports}/TestResult.xml"
}
}
}
if(configuration.build.codeQuality) {
stage('CodeQuality') {
def assemblies = getFilePaths(configuration.codeQuality.fxcop.wildcards)
dir(env.WORKSPACE){
for(def assembly : assemblies ) {
try{
bat """"${tool 'fxcop'}" /f:$assembly /o:${configuration.reports}\\${new File(assembly).name}.fxcop.xml"""
} catch(Exception ex) {
echo ex.getMessage()
}
}
}
}
}
if(configuration.build.archive) {
stage('Archive') {
dir(env.WORKSPACE){
for(def archive : configuration.archive ) {
archiveArtifacts artifacts: archive, onlyIfSuccessful: true
}
}
}
}
} catch (ex) {
buildStatus = BuildStatus.Error;
echo ex
exit 1
} finally {
if(configuration.build.notifications) {
stage('Notifications') {
def subject = "Build $buildStatus - $JOB_NAME ($BUILD_DISPLAY_NAME)"
def nunitTestBody = configuration.build.tests
? renderTemplete(
configuration.reportsTemplates + 'nunitTestResult.template.html',
getTestReportModel(configuration.reports + '\\TestResult.xml'))
: ""
def fxCopTestBody = configuration.build.codeQuality
? renderTemplete(
configuration.reportsTemplates + 'fxCopTestResult.template.html',
getFxCopReporModel(configuration.codeQuality.fxcop.reports))
: ""
def emailBody = renderTemplete(
configuration.reportsTemplates + 'buildresult.template.html',
getBuildCompleteModel(nunitTestBody, fxCopTestBody, buildStatus))
emailext body: emailBody, subject: subject, to: 'khdevnet@gmail.com'
}
}
}
}
}
def checkoutComponents(components){
for(def gitUrl : readJsonFromText(components) ) {
dir(getComponentFolder(gitUrl)) {
git url: gitUrl
}
}
}
def getConfiguration(configurationFileName) {
def buildConfigurationJsonFile = findFiles(glob: "**/**/$configurationFileName").first()
readJsonFromFile(buildConfigurationJsonFile.path)
}
def getComponentFolder(giturl) {
giturl.replace('.git','').tokenize( '/' ).last()
}
def readJsonFromText(def text) {
return new JsonSlurperClassic().parseText(text)
}
def readJsonFromFile(def path) {
def configurationFile = new File(env.WORKSPACE, path)
return new JsonSlurperClassic().parseText(configurationFile.text)
}
// parse fx cop
def getFxCopReporModel(fxCopReportFileWildCards){
def reportMap = [:]
for(def fxCopReportFilePath : getFilePaths(fxCopReportFileWildCards) ) {
def fxCopReportFile = new File(env.WORKSPACE, fxCopReportFilePath)
def dllName = fxCopReportFile.name.replace(".fxcop.xml", "");
def statistic = parseFxCopReportXmlFile(fxCopReportFile)
echo dllName
echo statistic
reportMap.put(dllName, statistic)
}
def statisticHtml = '';
for(def model : reportMap ) {
statisticHtml+="<li>${model.key}: ${model.value}</li>"
}
return ["statistic": statisticHtml]
}
def parseFxCopReportXmlFile(fxCopReportFile){
def errorsCount = 0
def warningsCount = 0
def fxCopRootNode = new XmlParser().parse(fxCopReportFile)
def namespacesNode = getFirstNodeByName(fxCopRootNode.children(), 'Namespaces')
def namespaceNodes = getAllNodesByName(namespacesNode.children(), 'Namespace');
for(def node : namespaceNodes ) {
def messagesNode = getFirstNodeByName(node.children(), 'Messages')
def messageNodes = getAllNodesByName(messagesNode.children(), 'Message')
for(def messageNode : messageNodes ) {
def issueNode = getFirstNodeByName(messageNode.children(), 'Issue')
def issueNodeAttributes = issueNode.attributes()
def levelAttribute = issueNodeAttributes.get('Level')
if(levelAttribute != null) {
if(levelAttribute == 'Warning'){
warningsCount++
}
if(levelAttribute == 'Error'){
errorsCount++
}
}
}
}
return "Warnings: ${warningsCount}, Errors: ${errorsCount}"
}
def getFirstNodeByName(nodes, nodeName){
for(def node : nodes ) {
if(node.name() == nodeName){
return node
}
}
}
def getAllNodesByName(nodes, nodeName){
def list = []
for(def node : nodes ) {
if(node.name() == nodeName){
list << node
}
}
return list
}
def getBuildCompleteModel(nunitResultBody, fxCopResultBody, buildStatus){
return ["buildResultUrl": "$BUILD_URL", "buildStatus": buildStatus,
"buildNumber": "$BUILD_DISPLAY_NAME", "applicationName": "$JOB_NAME",
"nunitResultBody" : "$nunitResultBody", "fxCopResultBody": "$fxCopResultBody"]
}
def mergeMap(target, map){
for(def result : map ) { target.put(map.key, map.value) }
return target
}
def renderTemplete(templateFilePath, model){
def templateBody = new File(env.WORKSPACE, templateFilePath).text
def engine = new groovy.text.SimpleTemplateEngine()
engine.createTemplate(templateBody).make(model).toString()
}
def getTestReportModel(nunitTestReportXmlFilePath){
def testXmlRootNode = new XmlParser().parse(new File(env.WORKSPACE, nunitTestReportXmlFilePath))
def resultNode = findlastNode(testXmlRootNode.children(),'test-suite')
def result = resultNode.attributes();
result.put('testResultsUrl', env.JOB_URL + env.BUILD_ID + '/testReport')
return result
}
def findlastNode(list, nodeName){
for(def element : list.reverse() ) {
if(element.name()==nodeName){
return element
}
}
}
def getFilePaths(wildcards){
def files = []
for(def wildcard : wildcards ) {
files.addAll(findFiles(glob: wildcard))
}
def filePaths = []
for(def file : files ) { filePaths << file.path }
return filePaths
}
def getFiles(wildcards, rootDir=''){
def files = []
for(def wildcard : wildcards ) {
files.addAll(findFiles(glob: wildcard))
}
def names = []
def prefix = rootDir == '' ? '' : rootDir + '\\'
for(def file : files ) { names << prefix + file.name }
return names
}
def cleanDir(dirPath) {
def dir = new File(dirPath)
if (dir.exists()) dir.deleteDir()
if (!dir.exists()) dir.mkdirs()
}
def makeDir(dirPath) {
def dir = new File(dirPath)
if (!dir.exists()) dir.mkdirs()
}
def removeDir(dirPath) {
def dir = new File(dirPath)
if (dir.exists()) dir.deleteDir()
}
def log(message){
println message
}
class BuildStatus {
static String Ok = 'Ok'
static String Error = 'Error'
static String Warning = 'Warning'
}