-
Notifications
You must be signed in to change notification settings - Fork 890
/
Jenkinsfile
199 lines (182 loc) · 7.95 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
pipeline {
agent {
node { label "master" }
}
options {
timeout(time: 6, unit: "HOURS")
timestamps()
}
parameters {
choice(name: "BUILD_TYPE", choices: ["Release", "Debug"], description: "")
choice(name: "CHANNEL", choices: ["nightly", "dev", "beta", "release"], description: "")
booleanParam(name: "OFFICIAL_BUILD", defaultValue: true, description: "")
booleanParam(name: "SKIP_SIGNING", defaultValue: false, description: "")
booleanParam(name: "WIPE_WORKSPACE", defaultValue: false, description: "")
booleanParam(name: "SKIP_INIT", defaultValue: false, description: "")
booleanParam(name: "DISABLE_SCCACHE", defaultValue: false, description: "")
booleanParam(name: "DEBUG", defaultValue: false, description: "")
}
environment {
GITHUB_API = "https://api.github.com/repos/brave"
GITHUB_CREDENTIAL_ID = "brave-builds-github-token-for-pr-builder"
GITHUB_AUTH = credentials("${GITHUB_CREDENTIAL_ID}")
BB_REPO = "https://${GITHUB_AUTH}@github.com/brave/brave-browser"
}
stages {
stage("env") {
steps {
script {
BUILD_TYPE = params.BUILD_TYPE
CHANNEL = params.CHANNEL
OFFICIAL_BUILD = params.OFFICIAL_BUILD
SKIP_SIGNING = params.SKIP_SIGNING
WIPE_WORKSPACE = params.WIPE_WORKSPACE
SKIP_INIT = params.SKIP_INIT
DISABLE_SCCACHE = params.DISABLE_SCCACHE
DEBUG = params.DEBUG
SKIP = false
BRANCH = env.BRANCH_NAME
TARGET_BRANCH = "master"
if (env.CHANGE_BRANCH) {
BRANCH = env.CHANGE_BRANCH
TARGET_BRANCH = env.CHANGE_TARGET
def prNumber = readJSON(text: httpRequest(url: GITHUB_API + "/brave-core/pulls?head=brave:" + BRANCH, authentication: GITHUB_CREDENTIAL_ID, quiet: !DEBUG).content)[0].number
def prDetails = readJSON(text: httpRequest(url: GITHUB_API + "/brave-core/pulls/" + prNumber, authentication: GITHUB_CREDENTIAL_ID, quiet: !DEBUG).content)
SKIP = prDetails.mergeable_state.equals("draft") or prDetails.labels.count { label -> label.name.equalsIgnoreCase("CI/skip") }.equals(1)
}
BRANCH_EXISTS_IN_BB = httpRequest(url: GITHUB_API + "/brave-browser/branches/" + BRANCH, validResponseCodes: "100:499", authentication: GITHUB_CREDENTIAL_ID, quiet: !DEBUG).status.equals(200)
}
}
}
stage("abort") {
steps {
script {
if (SKIP) {
echo "Aborting build as PR is in draft or has \"CI/skip\" label"
stopCurrentBuild()
}
for (build in getBuilds()) {
if (build.isBuilding() && build.getNumber() < env.BUILD_NUMBER.toInteger()) {
echo "Aborting older running build " + build
build.doStop()
// build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"))
}
}
}
}
}
stage("checkout") {
when {
expression { !SKIP }
}
steps {
sh """
set -e
if [ -d brave-browser ]; then
rm -rf brave-browser
fi
git clone --branch ${TARGET_BRANCH} ${BB_REPO} || git clone ${BB_REPO}
cd brave-browser
git config user.name brave-builds
git config user.email [email protected]
git config push.default simple
"""
}
}
stage("branch-create") {
when {
allOf {
expression { !SKIP }
expression { !BRANCH_EXISTS_IN_BB }
}
}
steps {
sh """
set -e
cd brave-browser
git checkout -b ${BRANCH}
echo "Creating CI branch"
git commit --allow-empty -m "created CI branch"
echo "Pushing"
git push ${BB_REPO}
"""
}
}
stage("branch-rebase") {
when {
allOf {
expression { !SKIP }
expression { BRANCH_EXISTS_IN_BB }
}
}
steps {
sh """
set -e
cd brave-browser
git checkout ${BRANCH}
if [ "`jq -r .version package.json`" != "`jq -r .version ../package.json`" ]; then
set +e
echo "Version mismatch between brave-browser and brave-core in package.json, attempting rebase on brave-browser"
echo "Fetching latest changes and pruning refs"
git fetch --prune
echo "Rebasing ${BRANCH} branch on brave-browser against ${TARGET_BRANCH}"
git rebase origin/${TARGET_BRANCH}
if [ \$? -ne 0 ]; then
echo "Failed to rebase (conflicts), will need to be manually rebased"
git rebase --abort
else
echo "Rebased, force pushing to brave-browser"
git push --force ${BB_REPO}
fi
if [ "`jq -r .version package.json`" != "`jq -r .version ../package.json`" ]; then
echo "Version mismatch between brave-browser and brave-core in package.json, please try rebasing this branch in brave-core as well"
exit 1
fi
set -e
fi
"""
}
}
stage("build") {
when {
expression { !SKIP }
}
steps {
script {
try {
startBraveBrowserBuild()
}
catch (hudson.AbortException ex) {
echo "Sleeping 6m so new branch is discovered or associated PR created in brave-browser"
sleep(time: 6, unit: "MINUTES")
startBraveBrowserBuild()
}
}
}
}
}
}
@NonCPS
def stopCurrentBuild() {
Jenkins.instance.getItemByFullName(env.JOB_NAME).getLastBuild().doStop()
}
@NonCPS
def getBuilds() {
return Jenkins.instance.getItemByFullName(env.JOB_NAME).builds
}
def startBraveBrowserBuild() {
def prDetails = readJSON(text: httpRequest(url: GITHUB_API + "/brave-browser/pulls?head=brave:" + BRANCH, authentication: GITHUB_CREDENTIAL_ID, quiet: !DEBUG).content)[0]
def prNumber = prDetails ? prDetails.number : ""
def refToBuild = prNumber ? "PR-" + prNumber : URLEncoder.encode(BRANCH, "UTF-8")
params = [
string(name: "BUILD_TYPE", value: BUILD_TYPE),
string(name: "CHANNEL", value: CHANNEL),
booleanParam(name: "OFFICIAL_BUILD", value: OFFICIAL_BUILD),
booleanParam(name: "SKIP_SIGNING", value: SKIP_SIGNING),
booleanParam(name: "WIPE_WORKSPACE", value: WIPE_WORKSPACE),
booleanParam(name: "SKIP_INIT", value: SKIP_INIT),
booleanParam(name: "DISABLE_SCCACHE", value: DISABLE_SCCACHE),
booleanParam(name: "DEBUG", value: DEBUG)
]
currentBuild.result = build(job: "brave-browser-build-pr/" + refToBuild, parameters: params, propagate: false).result
}