forked from tiiuae/ghaf-jenkins-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghaf-pre-merge-pipeline.groovy
205 lines (190 loc) · 7.21 KB
/
ghaf-pre-merge-pipeline.groovy
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
#!/usr/bin/env groovy
// SPDX-FileCopyrightText: 2022-2024 TII (SSRC) and the Ghaf contributors
// SPDX-License-Identifier: Apache-2.0
////////////////////////////////////////////////////////////////////////////////
def REPO_URL = 'https://github.com/tiiuae/ghaf/'
def WORKDIR = 'ghaf'
// Utils module will be loaded in the first pipeline stage
def utils = null
def targets = [
[ system: "aarch64-linux", target: "doc", ],
[ system: "x86_64-linux", target: "doc", ],
[ system: "x86_64-linux", target: "generic-x86_64-debug", ],
[ system: "x86_64-linux", target: "lenovo-x1-carbon-gen11-debug", ],
[ system: "x86_64-linux", target: "microchip-icicle-kit-debug-from-x86_64", ],
[ system: "aarch64-linux", target: "nvidia-jetson-orin-agx-debug", ],
[ system: "x86_64-linux", target: "nvidia-jetson-orin-agx-debug-from-x86_64", ],
[ system: "aarch64-linux", target: "nvidia-jetson-orin-nx-debug", ],
[ system: "x86_64-linux", target: "nvidia-jetson-orin-nx-debug-from-x86_64", ],
]
target_jobs = [:]
properties([
githubProjectProperty(displayName: '', projectUrlStr: REPO_URL),
// The following options are documented in:
// https://www.jenkins.io/doc/pipeline/steps/params/pipelinetriggers/
// Following config requires having github token configured in:
// 'Manage Jenkins' > 'System' > 'Github' > 'GitHub Server' > 'Credentials'.
// Token needs to be 'classic' with 'repo' scope to be able to both set the
// commit statuses and read the commit author organization.
// 'HEAVY_HOOKS' requires webhooks configured properly in the target
// github repository. If webhooks cannot be used, consider using polling by
// replacing 'HEAVY_HOOKS' with 'CRON' and declaring the poll interval in
// 'spec'.
pipelineTriggers([
githubPullRequests(
spec: '',
triggerMode: 'HEAVY_HOOKS',
// Trigger on PR open, change, or close. Skip if PR is not mergeable.
events: [Open(), commitChanged(), close(), nonMergeable(skip: true)],
abortRunning: true,
cancelQueued: true,
preStatus: true,
skipFirstRun: false,
userRestriction: [users: '', orgs: 'tiiuae'],
repoProviders: [
githubPlugin(
repoPermission: 'PULL'
)
]
)
])
])
////////////////////////////////////////////////////////////////////////////////
pipeline {
agent { label 'built-in' }
options {
disableConcurrentBuilds()
timestamps ()
buildDiscarder(logRotator(numToKeepStr: '100'))
}
stages {
stage('Checkenv') {
steps {
sh 'set | grep -P "(GITHUB_PR_)"'
// Fail if this build was not triggered by a PR
sh 'if [ -z "$GITHUB_PR_NUMBER" ]; then exit 1; fi'
sh 'if [ -z "$GITHUB_PR_TARGET_BRANCH" ]; then exit 1; fi'
sh 'if [ -z "$GITHUB_PR_STATE" ]; then exit 1; fi'
// Fail if environment is otherwise unexpected
sh 'if [ -z "$JOB_BASE_NAME" ]; then exit 1; fi'
sh 'if [ -z "$BUILD_NUMBER" ]; then exit 1; fi'
// Fail if PR was closed (but not merged)
sh 'if [ "$GITHUB_PR_STATE" = "CLOSED" ]; then exit 1; fi'
}
}
stage('Checkout') {
steps {
script { utils = load "utils.groovy" }
dir(WORKDIR) {
// References:
// https://www.jenkins.io/doc/pipeline/steps/params/scmgit/#scmgit
// https://github.com/KostyaSha/github-integration-plugin/blob/master/docs/Configuration.adoc
checkout scmGit(
userRemoteConfigs: [[
url: REPO_URL,
name: 'pr_origin',
// Below, we set two git remotes: 'pr_origin' and 'origin'
// We use '/merge' in pr_origin to build the PR as if it was
// merged to the PR target branch GITHUB_PR_TARGET_BRANCH.
// To build the PR head (without merge) you would replace
// '/merge' with '/head' in the pr_origin remote. We also
// need to set the 'origin' remote to be able to compare
// the PR changes against the correct target.
refspec: '+refs/pull/${GITHUB_PR_NUMBER}/merge:refs/remotes/pr_origin/pull/${GITHUB_PR_NUMBER}/merge +refs/heads/*:refs/remotes/origin/*',
]],
branches: [[name: 'pr_origin/pull/${GITHUB_PR_NUMBER}/merge']],
extensions: [
cleanBeforeCheckout(),
// We use the 'changelogToBranch' extension to correctly
// show the PR changed commits in Jenkins changes.
// References:
// https://issues.jenkins.io/browse/JENKINS-26354
// https://javadoc.jenkins.io/plugin/git/hudson/plugins/git/extensions/impl/ChangelogToBranch.html
changelogToBranch (
options: [
compareRemote: 'origin',
compareTarget: "${GITHUB_PR_TARGET_BRANCH}"
]
)
],
)
script {
env.TARGET_COMMIT = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
}
}
}
}
stage('Set PR status pending') {
steps {
script {
// https://www.jenkins.io/doc/pipeline/steps/github-pullrequest/
setGitHubPullRequestStatus(
state: 'PENDING',
context: "${JOB_BASE_NAME}",
message: "Build #${BUILD_NUMBER} started",
)
}
}
}
stage('Evaluate') {
steps {
dir(WORKDIR) {
script {
// Creates jobs.txt in working directory to use later
utils.nix_eval_jobs(targets)
targets.each {
def target = it.system + "." + it.target
// row that matches this target is grepped from jobs.txt, extracting the pre-evaluated derivation path
def drvPath = sh (script: "cat jobs.txt | grep ${target} | cut -d ' ' -f 2", returnStdout: true).trim()
target_jobs["${it.target} (${it.system})"] = {
stage("Build ${target}") {
try {
if (drvPath) {
sh "nix build --no-link -L ${drvPath}\\^*"
} else {
error("Target \"${target}\" was not found in packages")
}
} catch (InterruptedException e) {
throw e
} catch (Exception e) {
unstable("FAILED: ${target}")
currentBuild.result = "FAILURE"
println "Error: ${e.toString()}"
}
}
}
}
}
}
}
}
stage('Build targets') {
steps {
script {
parallel target_jobs
}
}
}
}
post {
success {
script {
setGitHubPullRequestStatus(
state: 'SUCCESS',
context: "${JOB_BASE_NAME}",
message: "Build #${BUILD_NUMBER} passed in ${currentBuild.durationString}",
)
}
}
unsuccessful {
script {
setGitHubPullRequestStatus(
state: 'FAILURE',
context: "${JOB_BASE_NAME}",
message: "Build #${BUILD_NUMBER} failed in ${currentBuild.durationString}",
)
}
}
}
}
////////////////////////////////////////////////////////////////////////////////