-
Notifications
You must be signed in to change notification settings - Fork 14
/
Jenkinsfile-infra
206 lines (188 loc) · 9.72 KB
/
Jenkinsfile-infra
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
/*
* This file uses CSS styles defined in
* https://github.com/networkupstools/jenkins-dynamatrix
* with addBadge() method from Jenkins Badge plugin v2.x
* (supporting cssStyle argument instead of a legacy series
* of colors and line thicknesses that end up hard-coded
* into messages).
*/
@Library('jenkins-dynamatrix@master') _
import org.nut.dynamatrix.dynamatrixGlobalState;
import org.nut.dynamatrix.*;
reportBuildCause()
pipeline {
// Note: fixed label set from NUT CI farm since the overall
// checkout workspace is huge and so persistent, as well as
// lots of heavier tools are used and vetted to work well:
agent {
//label "jenkins-nut-doc-deb11"
label "nut-website-builder && nut-builder && doc-builder && NUT_BUILD_CAPS=docs:all"
}
options {
disableConcurrentBuilds()
}
parameters {
booleanParam(
name: 'CI_AUTOCOMMIT',
defaultValue: true,
description: 'Create the commits for site source and public?'
)
booleanParam(
name: 'CI_AUTOPUSH',
defaultValue: true,
description: 'Push the commits for site source and public?'
)
booleanParam(
name: 'CI_AVOID_RESPIN',
defaultValue: true,
description: 'Avoid needless work (site date changes etc) if nut-website and source components did not change in git'
)
booleanParam(
name: 'CI_AVOID_SPELLCHECK',
defaultValue: true,
description: 'If "true", the spellcheck of site files would be a separate action with a non-fatal diagnosis after a site build and push (best-effort FYI); if "false" then spelling issues vs. master nut/docs/nut.dict would be fatal before site build.'
)
choice(
name: 'CI_TRY_HTMLPROOFER',
// FIXME: Initially "false" to boot-strap development on
// https://github.com/networkupstools/nut-website/issues/52
// and then should become "true" or "require" depending on
// achievements.
choices: ['false', 'true', 'require'],
description: 'If "true", the sanity-check of generated site files (cross-page links etc.) would be an action with a non-fatal diagnosis after a site build before a push (best-effort FYI); if "require" its faults would be fatal; if "false" then we would not waste several minutes on it.'
)
booleanParam(
name: 'NUT_DDL_PEDANTIC_DECLARATIONS',
defaultValue: true,
description: 'If "true", the "nut-ddl.py" script should fail if there are concerns about DDL file markup (so something is ambiguous, duplicate where should be unique, missing where required...) so these would be fatal before site build; if "false" then such issues are only printed as warnings.'
)
}
stages {
stage("Build and optionally push") {
steps {
script {
/*
// TODO: Allow admin users to run the job and others not?
// For now rely on Jenkins folder/job security matrix
def causeUser = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')?.userId[0]
def adminGroup = 'networkupstools'
// May be null/not of wanted type if Role Strategy Plugin is missing
def authStrategy = null
try {
authStrategy = Jenkins.instance.getAuthorizationStrategy()
if (! (authStrategy instanceof RoleBasedAuthorizationStrategy) ) authStrategy=null
} catch (Throwable t) {}
def roleMaps = null
def roleSids = null
if (authStrategy) {
try {
roleMaps = authStrategy.getRoleMap(com.synopsys.arc.jenkins.plugins.rolestrategy.RoleType.Global)
roleSids = roleMaps.getSidsForRole(adminGroup.trim())
} catch (Throwable t) {}
}
*/
if (env?.BRANCH_NAME == null) {
if (env?.GIT_BRANCH) {
env.BRANCH_NAME = env.GIT_BRANCH - ~/^origin\//
} else {
env.BRANCH_NAME = sh (
script: 'git rev-parse --abbrev-ref HEAD',
returnStdout: true
).trim()
}
}
if (env.BRANCH_NAME != 'master'
&& !(env.BRANCH_NAME ==~ /^.*\/master$/ )
) {
echo "WARNING: Not running for a master branch (${env.BRANCH_NAME}), disabling Git actions"
env.CI_AUTOCOMMIT = false
env.CI_AUTOPUSH = false
} else {
// Be ready to push later
sh 'git checkout master && git rebase origin/master'
}
if (currentBuild.changeSets?.size() > 0) {
echo "WARNING: Running with source changes known to CI, so not avoiding work"
env.CI_AVOID_RESPIN = false
} else
for (cause in currentBuild.getBuildCauses()) {
echo "Checking for code-change detecting build cause in: ${cause}"
if (cause._class.toString().contains('GitHubPushCause')
|| cause._class.toString().contains('BranchEventCause')
|| cause._class.toString().contains('BranchIndexingCause')
) {
echo "WARNING: Running due to an automated source change trigger, not avoiding work"
env.CI_AVOID_RESPIN = false
}
}
def shRes
def msg
echo "Starting website build: CI_AUTOCOMMIT=${env.CI_AUTOCOMMIT} CI_AUTOPUSH=${env.CI_AUTOPUSH} CI_AVOID_RESPIN=${env.CI_AVOID_RESPIN} CI_AVOID_SPELLCHECK=${env.CI_AVOID_SPELLCHECK} CI_TRY_HTMLPROOFER=${env.CI_TRY_HTMLPROOFER} BRANCH_NAME=${env.BRANCH_NAME}"
withCredentials([gitUsernamePassword(credentialsId: scm.getUserRemoteConfigs()[0].getCredentialsId())]) {
// for git push in shell, using https://www.jenkins.io/blog/2021/07/27/git-credentials-binding-phase-1/
shRes = sh (returnStatus: true, script: "./ci_build.sh")
}
def cssAdd = ""
switch (shRes) {
case 0:
if ("${env.CI_AUTOPUSH}" == "true") {
msg = "Site updated"
cssAdd = "badge-jenkins-dynamatrix-SlowBuild-SUCCESS"
} else {
msg = "Site regenerated successfully, but not pushed"
cssAdd = "badge-jenkins-dynamatrix-SlowBuild-NOT_BUILT"
}
break
case 42:
msg = "Site already up to date, skipped"
cssAdd = "badge-jenkins-dynamatrix-SlowBuild-NOT_BUILT"
break
default:
msg = "FAILED to process site update"
cssAdd = "badge-jenkins-dynamatrix-SlowBuild-FAILURE"
break
}
try {
// Badge v2.x API, with style
addBadge(text: msg, cssClass: "badge-jenkins-dynamatrix-Baseline ${cssAdd}")
} catch (Throwable ignored) {
manager.addShortText(msg)
}
echo msg
if ( !(shRes in [0, 42]) ) {
error msg
}
// NOTE: If there would be support for historic site publishing,
// spellcheck for those snapshots can not be impacted so should
// be skipped.
if ("${env.CI_AVOID_SPELLCHECK}" == "true") {
shRes = sh (returnStatus: true, script: "make -k -s -j 8 spellcheck 2>/dev/null >/dev/null; make -k spellcheck")
switch (shRes) {
case 0:
msg = "Spellcheck passed"
try {
// Badge v2.x API, with style
addBadge(text: msg, cssClass: "badge-jenkins-dynamatrix-Baseline badge-jenkins-dynamatrix-SlowBuild-SUCCESS")
} catch (Throwable ignored) {
manager.addShortText(msg)
}
echo msg
break
default:
msg = "Spellchecker had concerns (code ${shRes})"
try {
// Badge v2.x API, with style
addBadge(text: msg, cssClass: "badge-jenkins-dynamatrix-Baseline badge-jenkins-dynamatrix-SlowBuild-UNSTABLE")
} catch (Throwable ignored) {
manager.addShortText(msg)
}
echo msg
unstable(msg)
break
}
}
}
}
}
}
}