forked from bazel-contrib/setup-bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
301 lines (250 loc) · 8.48 KB
/
index.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const fs = require('fs')
const { setTimeout } = require('timers/promises')
const core = require('@actions/core')
const github = require('@actions/github')
const glob = require('@actions/glob')
const tc = require('@actions/tool-cache')
const config = require('./config')
const { mountStickyDisk } = require('./stickydisk');
const crypto = require('crypto')
const cache = require('@actions/cache')
async function run() {
try {
await setupBazel()
} catch (error) {
core.saveState('action-failed', 'true')
core.setFailed(error.stack)
}
}
async function setupBazel() {
core.startGroup('Configure Bazel')
core.info('Configuration:')
core.info(JSON.stringify(config, null, 2))
await setupBazelrc()
core.endGroup()
await setupBazelisk()
const bazeliskMounts = await loadStickyDisk(config.bazeliskCache)
const diskMounts = await loadStickyDisk(config.diskCache)
const repoMounts = await loadStickyDisk(config.repositoryCache)
await restoreExternalCaches(config.externalCache)
const allMounts = {
...bazeliskMounts,
...diskMounts,
...repoMounts,
};
// Save the combined mounts from this run
core.saveState('sticky-disk-mounts', JSON.stringify(allMounts));
return allMounts;
}
async function restoreExternalCaches(cacheConfig) {
if (!cacheConfig.enabled) {
return
}
// First fetch the manifest of external caches used.
const path = cacheConfig.manifest.path
await restoreCache({
enabled: true,
files: cacheConfig.manifest.files,
name: cacheConfig.manifest.name,
paths: [path]
})
// Now restore all external caches defined in manifest
if (fs.existsSync(path)) {
const manifest = fs.readFileSync(path, { encoding: 'utf8' })
for (const name of manifest.split('\n').filter(s => s)) {
await restoreCache({
enabled: cacheConfig[name]?.enabled ?? cacheConfig.default.enabled,
files: cacheConfig[name]?.files || cacheConfig.default.files,
name: cacheConfig.default.name(name),
paths: cacheConfig.default.paths(name)
})
}
}
}
async function restoreCache(cacheConfig) {
if (!cacheConfig.enabled) {
return
}
const delay = Math.random() * 1000 // timeout <= 1 sec to reduce 429 errors
await setTimeout(delay, async function () {
core.startGroup(`Restore cache for ${cacheConfig.name}`)
const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
const name = cacheConfig.name
const paths = cacheConfig.paths
const restoreKey = `${config.baseCacheKey}-${name}-`
const key = `${restoreKey}${hash}`
core.debug(`Attempting to restore ${name} cache from ${key}`)
const restoredKey = await cache.restoreCache(
paths, key, [restoreKey],
{ segmentTimeoutInMs: 300000 } // 5 minutes
)
if (restoredKey) {
core.info(`Successfully restored cache from ${restoredKey}`)
if (restoredKey === key) {
core.saveState(`${name}-cache-hit`, 'true')
}
} else {
core.info(`Failed to restore ${name} cache`)
}
core.endGroup()
}())
}
async function setupBazelisk() {
if (config.bazeliskVersion.length == 0) {
return
}
core.startGroup('Setup Bazelisk')
let toolPath = tc.find('bazelisk', config.bazeliskVersion)
if (toolPath) {
core.debug(`Found in cache @ ${toolPath}`)
} else {
toolPath = await downloadBazelisk()
}
core.addPath(toolPath)
core.endGroup()
}
async function downloadBazelisk() {
const version = config.bazeliskVersion
core.debug(`Attempting to download ${version}`)
// Possible values are 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x' and 'x64'.
// Bazelisk filenames use 'amd64' and 'arm64'.
let arch = config.os.arch
if (arch == 'x64') {
arch = 'amd64'
}
// Possible values are 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos' and 'win32'.
// Bazelisk filenames use 'darwin', 'linux' and 'windows'.
let platform = config.os.platform
if (platform == "win32") {
platform = "windows"
}
let filename = `bazelisk-${platform}-${arch}`
if (platform == 'windows') {
filename = `${filename}.exe`
}
const token = core.getInput('token')
const octokit = github.getOctokit(token, {
baseUrl: 'https://api.github.com'
})
const { data: releases } = await octokit.rest.repos.listReleases({
owner: 'bazelbuild',
repo: 'bazelisk'
})
// Find version matching semver specification.
const tagName = tc.evaluateVersions(releases.map((r) => r.tag_name), version)
const release = releases.find((r) => r.tag_name === tagName)
if (!release) {
throw new Error(`Unable to find Bazelisk version ${version}`)
}
const asset = release.assets.find((a) => a.name == filename)
if (!asset) {
throw new Error(`Unable to find Bazelisk version ${version} for platform ${platform}/${arch}`)
}
const url = asset.browser_download_url
core.debug(`Downloading from ${url}`)
const downloadPath = await tc.downloadTool(url, undefined, `token ${token}`)
core.debug('Adding to the cache...');
fs.chmodSync(downloadPath, '755');
const cachePath = await tc.cacheFile(downloadPath, 'bazel', 'bazelisk', version)
core.debug(`Successfully cached bazelisk to ${cachePath}`)
return cachePath
}
async function setupBazelrc() {
for (const bazelrcPath of config.paths.bazelrc) {
fs.writeFileSync(
bazelrcPath,
`startup --output_base=${config.paths.bazelOutputBase}\n`
)
fs.appendFileSync(bazelrcPath, config.bazelrc.join("\n"))
}
}
async function loadExternalStickyDisks(cacheConfig) {
if (!cacheConfig.enabled) {
return {}
}
// First fetch the manifest of external caches used.
const path = cacheConfig.manifest.path
const manifestMounts = await loadStickyDisk({
enabled: true,
files: cacheConfig.manifest.files,
name: cacheConfig.manifest.name,
paths: [path]
})
let allMounts = { ...manifestMounts }
// Now restore all external caches defined in manifest
if (fs.existsSync(path)) {
process.stderr.write(`Restoring external caches from ${path}\n`)
const manifest = fs.readFileSync(path, { encoding: 'utf8' })
for (const name of manifest.split('\n').filter(s => s)) {
const mounts = await loadStickyDisk({
enabled: cacheConfig[name]?.enabled ?? cacheConfig.default.enabled,
files: cacheConfig[name]?.files || cacheConfig.default.files,
name: cacheConfig.default.name(name),
paths: cacheConfig.default.paths(name)
})
allMounts = { ...allMounts, ...mounts }
}
}
return allMounts
}
async function loadStickyDisk(cacheConfig) {
if (!cacheConfig.enabled) {
return {};
}
const delay = Math.random() * 1000 // timeout <= 1 sec to reduce contention
const mounts = await setTimeout(delay, async function () {
core.startGroup(`Setting up sticky disk for ${cacheConfig.name}`)
const hash = await glob.hashFiles(cacheConfig.files.join('\n'))
const name = cacheConfig.name
const paths = cacheConfig.paths
const baseKey = `${config.baseCacheKey}-${name}-${hash}`
const newMounts = {};
try {
const controller = new AbortController();
// Mount sticky disk for each path in the config and collect results
const mountResults = await Promise.all(paths.map(async (path) => {
try {
// Create a unique key for each path by including a hash of the path
const pathHash = crypto
.createHash('sha256')
.update(path)
.digest('hex')
.slice(0, 8); // Take first 8 chars of hash for brevity
const pathKey = `${baseKey}-${pathHash}`;
const { device, exposeId } = await mountStickyDisk(
pathKey,
path,
controller.signal,
controller
);
core.debug(`Mounted device ${device} at ${path} with expose ID ${exposeId}`);
return {
path,
mount: { device, exposeId, stickyDiskKey: pathKey }
};
} catch (error) {
core.warning(`Failed to mount sticky disk for ${path}: ${error}`);
return null;
}
}));
// Add successful mounts to the collection
for (const result of mountResults) {
if (result) {
newMounts[result.path] = result.mount;
}
}
core.info('Successfully mounted sticky disks');
} catch (error) {
core.warning(`Failed to setup sticky disks for ${name}: ${error}`);
}
core.endGroup()
return newMounts;
}())
return mounts;
}
run()
module.exports = {
loadStickyDisk,
loadExternalStickyDisks,
setupBazel
}