forked from merken/netCoreBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
154 lines (124 loc) · 4.45 KB
/
Jenkinsfile
File metadata and controls
154 lines (124 loc) · 4.45 KB
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
import groovy.json.JsonSlurper
VERSION_NUMBER = ""
/** Pipeline **/
node {
ws('netcore') {
try{
stage("scm pull") {
deleteDir();
cloneRepo();
VERSION_NUMBER = getVersionNumber();
currentBuild.displayName = "$VERSION_NUMBER";
}
stage ("dotnet build") {
dotnet_build();
}
stage ("dotnet publish to IIS") {
dotnet_publish_iis();
}
}
catch (InterruptedException x) {
currentBuild.result = 'ABORTED';
throw x;
}
catch (e) {
currentBuild.result = 'FAILURE';
throw e;
}
}
}
def cloneRepo() {
checkout scm;
}
def dotnet_build(){
dir('Merken.NetCoreBuild.App') {
sh(script: 'dotnet build Merken.NetCoreBuild.App.csproj', returnStdout: true);
}
}
def dotnet_publish_iis(){
dir('Merken.NetCoreBuild.App') {
sh(script: 'set -x', returnStdout: true);
sh(script: 'dotnet build Merken.NetCoreBuild.App.csproj /p:DeployOnBuild=true /p:PublishProfile="Web11112 - Web Deploy" /p:Username="$Web11112" /p:Password="<password removed>"', returnStdout: true).trim();
}
}
def dotnet_test(){
dir('Merken.NetCoreBuild.Test') {
sh(script: 'dotnet restore', returnStdout: true);
sh(script: 'dotnet xunit -xml xunit-results.xml', returnStdout: true);
}
dir('Merken.NetCoreBuild.Transform') {
sh(script: 'dotnet run ../Merken.NetCoreBuild.Test/xunit-results.xml xunitdotnet-2.0-to-junit-2.xsl junit-results.xml', returnStdout: true);
step([$class: 'XUnitBuilder',
thresholds: [[$class: 'FailedThreshold', unstableThreshold: '1']],
tools: [[$class: 'JUnitType', pattern: '*.*']]])
}
}
def dotnet_publish(){
dir('Merken.NetCoreBuild.App') {
sh(script: 'dotnet publish Merken.NetCoreBuild.App.csproj -o ./obj/Docker/publish', returnStdout: true);
sh(script: 'cp Dockerfile ./obj/Docker/publish', returnStdout: true);
sh(script: 'tar zcf netcoreapp.tar.gz -C ./obj/Docker/publish .', returnStdout: true);
}
}
def docker_build(){
dir('Merken.NetCoreBuild.App') {
dockerApiRequest('containers/netcoreapp/stop', 'POST');
dockerApiRequest('containers/prune', 'POST');
dockerApiRequest('images/netcoreapp', 'DELETE');
dockerApiRequest('build?t=netcoreapp:' + VERSION_NUMBER + '&nocache=1&rm=1', 'POST', 'tar','', '@netcoreapp.tar.gz', true);
}
}
def docker_run(){
dir('Merken.NetCoreBuild.App') {
def containerId = createContainer();
renameContainer(containerId);
startContainer();
}
}
def createContainer(){
sh('echo \'{ "Image": "netcoreapp:' + VERSION_NUMBER + '", "ExposedPorts": { "5000/tcp" : {} }, "HostConfig": { "PortBindings": { "5000/tcp": [{ "HostPort": "5000" }] } } }\' > imageconf');
def createResponse = dockerApiRequest('containers/create', 'POST', 'json', 'json', '@imageconf');
def containerId = createResponse.Id;
return containerId;
}
def renameContainer(containerId){
def request = 'containers/' + containerId + '/rename?name=netcoreapp';
dockerApiRequest(request, 'POST');
}
def startContainer(){
dockerApiRequest('containers/netcoreapp/start', 'POST');
}
//Generates a version number
def getVersionNumber() {
def out = sh(script: 'git rev-list --count HEAD', returnStdout: true);
def array = out.split("\\r?\\n");
def count = array[array.length - 1];
def commitCount = count.trim();
return commitCount;
}
def dockerApiRequest(request, method, contenttype = 'json', accept = '', data = '', isDataBinary = false){
def requestBuilder = 'curl -v -X ' + method + ' --unix-socket /var/run/docker.sock "http://0.0.0.0:2375/' + request + '"';
if(contenttype == 'json'){
requestBuilder += ' -H "Content-Type:application/json"';
}
if(contenttype == 'tar'){
requestBuilder += ' -H "Content-Type:application/x-tar"';
}
if(accept == 'json'){
requestBuilder += ' -H "Accept: application/json"';
}
if(data.trim()){
if(isDataBinary){
requestBuilder += ' --data-binary ' + data + ' --dump-header - --no-buffer';
}else{
requestBuilder += ' -d ' + data;
}
}
def response = sh(script: requestBuilder, returnStdout:true);
if(accept == 'json'){
def jsonSlurper = new JsonSlurper();
def json = jsonSlurper.parseText(response);
return json;
}
return null;
}