-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathJenkinsfile
254 lines (215 loc) · 8.86 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
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','/');
def split_job_name = env.JOB_NAME.split(/\/{1}/);
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 != "") {
discordSend description: "in favor of #${buildnum}, ignore following failed builds for ${killnums}", footer: "", link: env.BUILD_URL, result: "ABORTED", title: "[${split_job_name[0]}] Killing task(s) ${fixed_job_name} ${killnums}", webhookURL: env.GS2EMU_WEBHOOK;
}
echo "Done killing"
}
def buildStepDocker(config) {
def DOCKER_ROOT = config.DockerRoot;
def DOCKERIMAGE = config.DockerImage;
def DOCKERTAG = config.Tag;
def DOCKERFILE = config.Dockerfile;
def BUILD_NEXT = config.BuildIfSuccessful;
def BUILDENV = config.BuildEnv;
def RUN_TESTS = config.RunTests;
def split_job_name = env.JOB_NAME.split(/\/{1}/);
def fixed_job_name = split_job_name[1].replace('%2F',' ');
try {
checkout scm;
def buildenv = "${DOCKERTAG}";
def tag = '';
def EXTRA_VER = '';
def PUSH_IMAGE = BUILD_NEXT.equals('image');
def PUSH_ARTIFACT = BUILD_NEXT.equals('artifact');
if (BUILD_NEXT.equals('both')) {
PUSH_IMAGE = BUILD_NEXT.equals('both');
PUSH_ARTIFACT = BUILD_NEXT.equals('both');
}
if(env.TAG_NAME) {
sh(returnStdout: true, script: "echo '```' > RELEASE_DESCRIPTION.txt");
env.RELEASE_DESCRIPTION = sh(returnStdout: true, script: "git tag -l --format='%(contents)' ${env.TAG_NAME} >> RELEASE_DESCRIPTION.txt");
sh(returnStdout: true, script: "echo '```' >> RELEASE_DESCRIPTION.txt");
}
if (env.BRANCH_NAME.equals('master')) {
tag = "latest${DOCKERTAG}";
} else {
tag = "${env.BRANCH_NAME.replace('/','-')}${DOCKERTAG}";
}
if (env.TAG_NAME) {
EXTRA_VER = "--build-arg VER_EXTRA=${DOCKERTAG}";
} else if (env.BRANCH_NAME.equals('master')) {
EXTRA_VER = "--build-arg VER_EXTRA=-beta${DOCKERTAG}"
} else {
EXTRA_VER = "--build-arg VER_EXTRA=-${tag}";
}
docker.withRegistry("https://index.docker.io/v1/", "dockergraal") {
def release_name = env.JOB_NAME.replace('%2F','/');
def release_type = ("${release_name}").replace('/','-').replace('GServer-v2-','').replace('master','').replace('dev','');
def customImage
stage("Building ${DOCKERIMAGE}:${tag}...") {
customImage = docker.build("${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}", "--build-arg BUILDENV=${buildenv} ${EXTRA_VER} ${BUILDENV} --network=host --pull -f ${DOCKERFILE} .");
}
def archive_date = sh (
script: 'date +"-%Y%m%d-%H%M"',
returnStdout: true
).trim();
if (env.TAG_NAME) {
archive_date = '';
}
if (PUSH_IMAGE) {
stage("Pushing to docker hub registry...") {
customImage.push();
discordSend description: "Docker Image: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}", footer: "", link: env.BUILD_URL, result: currentBuild.currentResult, title: "[${split_job_name[0]}] Image Successful: ${fixed_job_name} #${env.BUILD_NUMBER}", webhookURL: env.GS2EMU_WEBHOOK;
}
}
if (RUN_TESTS) {
stage("Run tests...") {
customImage.inside("") {
try{
sh "cd /tmp/gserver/build && ctest -T test --no-compress-output --output-on-failure"
} catch(err) {
currentBuild.result = 'FAILURE'
discordSend description: "Testing Failed: ${fixed_job_name} #${env.BUILD_NUMBER} DockerImage: ${DOCKERIMAGE} (<${env.BUILD_URL}|Open>)", footer: "", link: env.BUILD_URL, result: currentBuild.currentResult, title: "[${split_job_name[0]}] Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER}", webhookURL: env.GS2EMU_WEBHOOK
notify('Build failed')
}
sh "mkdir -p ./test && cp -fvr /tmp/gserver/build/Testing ./test/Testing"
dir("./test") {
archiveArtifacts (
artifacts: 'Testing/**/*.xml',
fingerprint: true
)
stage("Xunit") {
// Process the CTest xml output with the xUnit plugin
xunit (
testTimeMargin: '3000',
thresholdMode: 1,
thresholds: [
skipped(failureThreshold: '0'),
failed(failureThreshold: '0')
],
tools: [CTest(
pattern: 'Testing/**/*.xml',
deleteOutputFiles: true,
failIfNotNew: false,
skipNoTestFiles: true,
stopProcessingIfError: true
)],
skipPublishingChecks: false
);
}
}
}
}
}
if (PUSH_ARTIFACT) {
stage("Archiving artifacts...") {
customImage.inside("") {
sh "mkdir -p ./dist && cp -fvr /dist/* ./dist"
dir("./dist") {
archiveArtifacts artifacts: '*.zip,*.tar.gz,*.tgz', allowEmptyArchive: true
discordSend description: "Docker Image: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}", footer: "", link: env.BUILD_URL, result: currentBuild.currentResult, title: "[${split_job_name[0]}] Artifact Successful: ${fixed_job_name} #${env.BUILD_NUMBER}", webhookURL: env.GS2EMU_WEBHOOK;
}
}
def dockerImageRef = docker.image("amigadev/docker-base:latest");
dockerImageRef.pull();
dockerImageRef.inside("") {
stage("Github Release") {
withCredentials([string(credentialsId: 'PREAGONAL_GITHUB_TOKEN', variable: 'GITHUB_TOKEN')]) {
dir("./dist") {
if (!env.CHANGE_ID) { // Don't run on PR's
def release_type_tag = 'develop';
def pre_release = '--pre-release';
if (env.TAG_NAME) {
pre_release = '';
release_type_tag = env.TAG_NAME;
} else if (env.BRANCH_NAME.equals('master')) {
release_type_tag = 'nightly';
}
if (!env.TAG_NAME) {
sh(returnStdout: true, script: "echo -e '${release_type_tag} releases' > ../RELEASE_DESCRIPTION.txt");
}
def files = sh(returnStdout: true, script: 'find . -name "*.zip" -o -name "*.tar.gz"');
files = sh (script: "basename ${files}",returnStdout:true).trim()
try {
sh "cat ../RELEASE_DESCRIPTION.txt | github-release release --user xtjoeytx --repo GServer-v2 --tag ${release_type_tag} --name \"GS2Emu ${release_type_tag}\" ${pre_release} --description -"
} catch(err) {
}
sh "github-release upload --user xtjoeytx --repo GServer-v2 --tag ${release_type_tag} --name \"${files}\" --file ${files} --replace"
}
}
}
}
}
}
} else {
// Do nothing
}
}
} catch(err) {
currentBuild.result = 'FAILURE'
discordSend description: "", footer: "", link: env.BUILD_URL, result: currentBuild.currentResult, title: "[${split_job_name[0]}] Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER}", webhookURL: env.GS2EMU_WEBHOOK
notify("Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER}")
throw err
}
}
node('master') {
killall_jobs();
def split_job_name = env.JOB_NAME.split(/\/{1}/);
def fixed_job_name = split_job_name[1].replace('%2F',' ');
checkout(scm);
env.COMMIT_MSG = sh (
script: 'git log -1 --pretty=%B ${GIT_COMMIT}',
returnStdout: true
).trim();
discordSend description: "${env.COMMIT_MSG}", footer: "", link: env.BUILD_URL, result: currentBuild.currentResult, title: "[${split_job_name[0]}] Build Started: ${fixed_job_name} #${env.BUILD_NUMBER}", webhookURL: env.GS2EMU_WEBHOOK
def branches = [:]
def project = readJSON file: "JenkinsEnv.json";
if (env.TAG_NAME) {
sh(returnStdout: true, script: "echo '```' > RELEASE_DESCRIPTION.txt");
env.RELEASE_DESCRIPTION = sh(returnStdout: true, script: "git tag -l --format='%(contents)' ${env.TAG_NAME} >> RELEASE_DESCRIPTION.txt");
sh(returnStdout: true, script: "echo '```' >> RELEASE_DESCRIPTION.txt");
}
project.builds.each { v ->
branches["Build ${v.Title}"] = {
node(v.OS) {
if ("${v.Type}" == "docker") {
buildStepDocker(v.Config);
}
}
}
}
parallel branches;
if (env.TAG_NAME) {
def DESC = sh(returnStdout: true, script: 'cat RELEASE_DESCRIPTION.txt');
discordSend description: "${DESC}", customUsername: "OpenGraal", customAvatarUrl: "https://pbs.twimg.com/profile_images/1895028712/13460_106738052711614_100001262603030_51047_4149060_n_400x400.jpg", footer: "OpenGraal Team", link: "https://github.com/xtjoeytx/GServer-v2/releases/tag/${env.TAG_NAME}", result: "SUCCESS", title: "GS2Emu v${env.TAG_NAME}", webhookURL: env.GS2EMU_RELEASE_WEBHOOK;
}
sh "rm -rf ./*"
}