forked from stephbu/csharp-dns-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
188 lines (162 loc) · 5.34 KB
/
Copy pathJenkinsfile
File metadata and controls
188 lines (162 loc) · 5.34 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
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
def notify(status){
emailext (
body: '$DEFAULT_CONTENT',
recipientProviders: [
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']
],
replyTo: '$DEFAULT_REPLYTO',
subject: '$DEFAULT_SUBJECT',
to: '$DEFAULT_RECIPIENTS'
)
}
@NonCPS
def killall_jobs() {
def jobname = env.JOB_NAME
def buildnum = env.BUILD_NUMBER.toInteger()
def killnums = ""
def job = Jenkins.instance.getItemByFullName(jobname)
def fixed_job_name = env.JOB_NAME.replace('%2F','/')
for (build in job.builds) {
if (!build.isBuilding()) { continue; }
if (buildnum == build.getNumber().toInteger()) { continue; println "equals" }
if (buildnum < build.getNumber().toInteger()) { continue; println "newer" }
echo "Kill task = ${build}"
killnums += "#" + build.getNumber().toInteger() + ", "
build.doStop();
}
if (killnums != "") {
notify("Killing task(s) ${fixed_job_name} ${killnums} in favor of #${buildnum}, ignore following failed builds for ${killnums}");
}
echo "Done killing"
}
def buildStep(DOCKER_ROOT, DOCKERIMAGE, DOCKERTAG, DOCKERFILE, BUILD_NEXT) {
def fixed_job_name = env.JOB_NAME.replace('%2F','/')
def tag = ''
try {
checkout scm;
def buildenv = '';
def publish = false;
if (env.BRANCH_NAME.equals('master')) {
buildenv = 'production';
tag = "${DOCKERTAG}";
publish = true;
} else if (env.BRANCH_NAME.equals('dev')) {
buildenv = 'development';
tag = "${DOCKERTAG}-dev";
publish = true;
} else {
tag = "${env.BRANCH_NAME.replace('/','-')}";
}
docker.withRegistry("https://index.docker.io/v1/", "dockerhub") {
def customImage
stage("Building ${DOCKERIMAGE}:${tag}...") {
customImage = docker.build("${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}", "--build-arg BUILDENV=${buildenv} --network=host --pull -f ${DOCKERFILE} .");
}
stage("Testing ${DOCKERIMAGE}:${tag}...") {
def testImage = docker.build("${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}_test", "--build-arg BUILDENV=${buildenv} --network=host --pull --target setup -f ${DOCKERFILE} .");
testImage.inside("-u 0") {
try{
sh("dotnet test --logger \"trx;LogFileName=../../Testing/unit_tests.xml\"");
sh('dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile="**/obj/**%3B**/Migrations/**%3B**/*generated.cs%3B**/Startup.cs%3B**/Program.cs"');
sh("chmod 777 -R .");
} catch(err) {
currentBuild.result = 'FAILURE'
sh("chmod 777 -R .");
notify('Testing failed')
}
archiveArtifacts (
artifacts: 'Testing/**.xml',
fingerprint: true
)
stage("Xunit") {
xunit (
testTimeMargin: '3000',
thresholdMode: 1,
thresholds: [
skipped(failureThreshold: '1000'),
failed(failureThreshold: '0')
],
tools: [MSTest(
pattern: 'Testing/**.xml',
deleteOutputFiles: true,
failIfNotNew: false,
skipNoTestFiles: true,
stopProcessingIfError: true
)],
skipPublishingChecks: false
);
}
}
def spaTestImage = docker.build("${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}_spa_test", "--build-arg BUILDENV=${buildenv} --network=host --pull --target spa-setup -f ${DOCKERFILE} .");
spaTestImage.inside("-u 0") {
try {
sh("cd Dns.Spa && npm ci --include=dev");
sh("cd Dns.Spa && npm run test:coverage");
sh("mkdir -p Testing");
sh("cp Dns.Spa/coverage/lcov.info Testing/spa_lcov.info");
sh("chmod 777 -R .");
} catch(err) {
currentBuild.result = 'FAILURE'
sh("chmod 777 -R .");
notify('SPA testing failed')
}
}
archiveArtifacts (
artifacts: 'Testing/spa_lcov.info',
fingerprint: true
)
withCredentials([string(credentialsId: 'MBEIJER_CSHARP_DNS_SERVER_CODECOV_TOKEN', variable: 'CODECOV_TOKEN')]) {
sh('''#!/usr/bin/env bash
set -euo pipefail
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
# Jenkins workspaces may be owned by a different uid when running in Docker.
git config --global --add safe.directory "$PWD" || true
./codecov \
--token "$CODECOV_TOKEN" \
--file "Testing/unit_tests.xml" \
--file "Dns.UnitTests/coverage.opencover.xml" \
--file "Testing/spa_lcov.info" \
--sha "${GIT_COMMIT:-$(git rev-parse HEAD)}" \
--branch "${BRANCH_NAME:-${GIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}}" \
--slug "mbeijer/csharp-dns-server" \
--build "$BUILD_NUMBER" \
--build-url "${BUILD_URL:-}" \
--name "jenkins-${JOB_NAME}-${BUILD_NUMBER}" \
--disable-search
''')
}
}
if (publish) {
stage("Pushing to docker hub registry...") {
customImage.push();
}
}
}
if (!BUILD_NEXT.equals('')) {
build "${BUILD_NEXT}/${env.BRANCH_NAME}";
}
} catch(err) {
currentBuild.result = 'FAILURE'
notify("Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER} Target: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}")
throw err
}
}
node('master') {
killall_jobs();
def fixed_job_name = env.JOB_NAME.replace('%2F','/');
checkout scm;
def branches = [:]
def project = readJSON file: "JenkinsEnv.json";
project.builds.each { v ->
branches["Build ${v.DockerRoot}/${v.DockerImage}:${v.DockerTag}"] = {
node("amd64") {
buildStep(v.DockerRoot, v.DockerImage, v.DockerTag, v.Dockerfile, v.BuildIfSuccessful)
}
}
}
sh "rm -rf ./*"
parallel branches;
}