-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathPrepare.groovy.j2
More file actions
206 lines (185 loc) · 5.84 KB
/
Prepare.groovy.j2
File metadata and controls
206 lines (185 loc) · 5.84 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def per_exec_ws(folder) {
return "workspace/exec_${env.EXECUTOR_NUMBER}/" + folder
}
// initialize source codes
def init_git() {
checkout scm
// Add more info about job node
sh (
script: './tests/scripts/task_show_node_info.sh',
label: 'Show executor node info',
)
// Determine merge commit to use for all stages
sh (
script: 'git fetch origin main',
label: 'Fetch upstream',
)
if (upstream_revision == null) {
upstream_revision = sh(
script: 'git log -1 FETCH_HEAD --format=\'%H\'',
label: 'Determine upstream revision',
returnStdout: true,
).trim()
}
sh (
script: "git -c user.name=TVM-Jenkins -c user.email=jenkins@tvm.apache.org merge ${upstream_revision}",
label: 'Merge to origin/main'
)
sh(
script: """
set -eux
{{ m.bash_retry() }}
retry 3 timeout 5m git submodule update --init -f --jobs 0
""",
label: 'Update git submodules',
)
checkout_trusted_files()
}
def docker_init(image) {
// Clear out all Docker images that aren't going to be used
sh(
script: """
set -eux
docker image ls --all
IMAGES=\$(docker image ls --all --format {% raw %}'{{.Repository}}:{{.Tag}} {{.ID}}'{% endraw %})
echo -e "Found images:\\n\$IMAGES"
echo "\$IMAGES" | { grep -vE '${image}' || test \$? = 1; } | { xargs docker rmi || test \$? = 123; }
docker image ls --all
""",
label: 'Clean old Docker images',
)
if (image.contains("amazonaws.com")) {
// If this string is in the image name it's from ECR and needs to be pulled
// with the right credentials
ecr_pull(image)
} else {
sh(
script: """
set -eux
{{ m.bash_retry() }}
retry 3 docker pull ${image}
""",
label: 'Pull docker image',
)
}
}
def should_skip_slow_tests(pr_number) {
withCredentials([string(
credentialsId: 'tvm-bot-jenkins-reader',
variable: 'GITHUB_TOKEN',
)]) {
// Exit code of 1 means run slow tests, exit code of 0 means skip slow tests
result = sh (
returnStatus: true,
script: "./ci/scripts/should_run_slow_tests.py --pr '${pr_number}'",
label: 'Check if CI should run slow tests',
)
}
return result == 0
}
def cancel_previous_build() {
// cancel previous build if it is not on main.
if (env.BRANCH_NAME != 'main') {
def buildNumber = env.BUILD_NUMBER as int
// Milestone API allows us to cancel previous build
// with the same milestone number
if (buildNumber > 1) milestone(buildNumber - 1)
milestone(buildNumber)
}
}
def checkout_trusted_files() {
// trust everything from branch builds
if (!env.BRANCH_NAME.startsWith('PR-')) {
return;
}
// trust peoople listed in CONTRIBUTING.md
grep_code = sh(
returnStatus: true,
script: "git show '${upstream_revision}:CONTRIBUTORS.md' | grep '@${env.CHANGE_AUTHOR}'",
label: 'Check if change is from a contributor',
)
if (grep_code == 1) {
// Any scripts that run on the bare host and not inside a Docker container
// (especially those that access secrets) should be checked out here so
// only trusted versions are used in CI
sh(
script: "git checkout ${upstream_revision} ci/scripts/.",
label: 'Check out trusted files',
)
}
}
def should_skip_ci(pr_number) {
if (env.BRANCH_NAME == null || !env.BRANCH_NAME.startsWith('PR-')) {
// never skip CI on build sourced from a branch
return false
}
glob_skip_ci_code = sh (
returnStatus: true,
script: "./ci/scripts/git_skip_ci_globs.py",
label: 'Check if CI should be skipped due to changed files',
)
if (glob_skip_ci_code == 0) {
return true
}
withCredentials([string(
credentialsId: 'tvm-bot-jenkins-reader',
variable: 'TOKEN',
)]) {
// Exit code of 1 means run full CI (or the script had an error, so run
// full CI just in case). Exit code of 0 means skip CI.
git_skip_ci_code = sh (
returnStatus: true,
script: "./ci/scripts/git_skip_ci.py --pr '${pr_number}'",
label: 'Check if CI should be skipped',
)
}
return git_skip_ci_code == 0
}
def prepare() {
stage('Prepare') {
node('CPU-SMALL') {
ws("workspace/exec_${env.EXECUTOR_NUMBER}/tvm/prepare") {
init_git()
if (env.DETERMINE_DOCKER_IMAGES == 'yes') {
sh(
script: "./ci/scripts/determine_docker_images.py {% for image in images %}{{ image.name }}={% raw %}${{% endraw %}{{ image.name }}{% raw %}}{% endraw %} {% endfor %}",
label: 'Decide whether to use tlcpack or tlcpackstaging for Docker images',
)
// Pull image names from the results of should_rebuild_docker.py
{% for image in images %}
{{ image.name }} = sh(
script: "cat .docker-image-names/{{ image.name }}",
label: "Find docker image name for {{ image.name }}",
returnStdout: true,
).trim()
{% endfor %}
}
{% for image in images %}
{{ image.name }} = params.{{ image.name }}_param ?: {{ image.name }}
{% endfor %}
sh (script: """
echo "Docker images being used in this build:"
{% for image in images %}
echo " {{ image.name }} = ${ {{- image.name -}} }"
{% endfor %}
""", label: 'Docker image names')
is_docs_only_build = sh (
returnStatus: true,
script: './ci/scripts/git_change_docs.sh',
label: 'Check for docs only changes',
)
skip_ci = should_skip_ci(env.CHANGE_ID)
skip_slow_tests = should_skip_slow_tests(env.CHANGE_ID)
rebuild_docker_images = sh (
returnStatus: true,
script: './ci/scripts/git_change_docker.sh',
label: 'Check for any docker changes',
)
if (skip_ci) {
// Don't rebuild when skipping CI
rebuild_docker_images = false
}
}
}
}
}