-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathconfig.js
More file actions
179 lines (164 loc) · 5.37 KB
/
config.js
File metadata and controls
179 lines (164 loc) · 5.37 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
import fs from 'fs'
import os from 'os'
import yaml from 'yaml'
import * as core from '@actions/core'
import * as github from '@actions/github'
const bazeliskVersion = core.getInput('bazelisk-version')
const cacheSave = core.getBooleanInput('cache-save')
const cacheVersion = core.getInput('cache-version')
const moduleRoot = core.getInput('module-root')
const homeDir = os.homedir()
const arch = os.arch()
const platform = os.platform()
let bazelOutputBase = core.getInput('output-base')
if (!bazelOutputBase) {
if (platform === 'win32') {
// check if GITHUB_WORKSPACE starts with D:
if (process.env.GITHUB_WORKSPACE?.toLowerCase()?.startsWith('d:')) {
bazelOutputBase = 'D:/_bazel'
} else {
bazelOutputBase = `C:/_bazel`
}
} else {
bazelOutputBase = `${homeDir}/.bazel`
}
}
let bazelDisk = core.toPosixPath(`${homeDir}/.cache/bazel-disk`)
let bazelRepository = core.toPosixPath(`${homeDir}/.cache/bazel-repo`)
let bazelrcPaths = [core.toPosixPath(`${homeDir}/.bazelrc`)]
let userCacheDir = `${homeDir}/.cache`
switch (platform) {
case 'darwin':
userCacheDir = `${homeDir}/Library/Caches`
break
case 'win32':
bazelDisk = `${bazelOutputBase}-disk`
bazelRepository = `${bazelOutputBase}-repo`
userCacheDir = `${homeDir}/AppData/Local`
if (process.env.HOME) {
bazelrcPaths.push(core.toPosixPath(`${process.env.HOME}/.bazelrc`))
}
break
}
const baseCacheKey = `setup-bazel-${cacheVersion}-${platform}-${arch}`
const bazelrc = core.getMultilineInput('bazelrc')
const diskCacheConfig = core.getInput('disk-cache')
const diskCacheEnabled = diskCacheConfig !== 'false'
let diskCacheName = 'disk'
if (diskCacheEnabled) {
// Before Bazel 6.3, providing --disk_cache to common is an error,
// with Bazel 6.3 and onwards, common accepts any legal Bazel option
// https://github.com/bazelbuild/bazel/issues/3054
bazelrc.push(`build --disk_cache=${bazelDisk}`)
if (diskCacheName !== 'true') {
diskCacheName = `${diskCacheName}-${diskCacheConfig}`
}
}
const repositoryCacheConfig = yaml.parse(core.getInput('repository-cache'))
const repositoryCacheEnabled = repositoryCacheConfig !== false
let repositoryCacheFiles = [
`${moduleRoot}/MODULE.bazel`,
`${moduleRoot}/WORKSPACE.bazel`,
`${moduleRoot}/WORKSPACE.bzlmod`,
`${moduleRoot}/WORKSPACE`
]
if (repositoryCacheEnabled) {
bazelrc.push(`common --repository_cache=${bazelRepository}`)
if (repositoryCacheConfig !== true) {
repositoryCacheFiles = Array(repositoryCacheConfig).flat()
}
}
const googleCredentials = core.getInput('google-credentials')
const googleCredentialsSaved = (core.getState('google-credentials-path').length > 0)
if (googleCredentials.length > 0 && !googleCredentialsSaved) {
const tmpDir = core.toPosixPath(fs.mkdtempSync(process.env.RUNNER_TEMP))
const googleCredentialsPath = `${tmpDir}/key.json`
fs.writeFileSync(googleCredentialsPath, googleCredentials)
bazelrc.push(`build --google_credentials=${googleCredentialsPath}`)
core.saveState('google-credentials-path', googleCredentialsPath)
}
const externalCacheConfig = yaml.parse(core.getInput('external-cache'))
const bazelExternal = core.toPosixPath(`${bazelOutputBase}/external`)
const externalCache = {}
if (externalCacheConfig) {
const { workflow, job } = github.context
const manifestName = externalCacheConfig.name ||
`${workflow.toLowerCase().replaceAll(/[ /]/g, '-')}-${job}`
externalCache.enabled = true
externalCache.minSize = 10 // MB
externalCache.baseCacheKey = `${baseCacheKey}-external-`
externalCache.manifest = {
files: [
`${moduleRoot}/MODULE.bazel`,
`${moduleRoot}/WORKSPACE.bazel`,
`${moduleRoot}/WORKSPACE.bzlmod`,
`${moduleRoot}/WORKSPACE`
],
name: `external-${manifestName}-manifest`,
path: `${os.tmpdir()}/external-cache-manifest.txt`
}
externalCache.default = {
enabled: true,
files: [
`${moduleRoot}/MODULE.bazel`,
`${moduleRoot}/WORKSPACE.bazel`,
`${moduleRoot}/WORKSPACE.bzlmod`,
`${moduleRoot}/WORKSPACE`
],
name: (name) => { return `external-${name}` },
paths: (name) => {
return [
`${bazelExternal}/@${name}.marker`,
`${bazelExternal}/${name}`
]
}
}
for (const name in externalCacheConfig.manifest) {
externalCache[name] = {
enabled: externalCacheConfig.manifest[name] != false,
files: Array(externalCacheConfig.manifest[name]).flat()
}
}
}
const cacheRestoreTimeoutMs = parseInt(core.getInput('cache-restore-timeout')) || 0
const token = core.getInput('token')
core.exportVariable('BAZELISK_GITHUB_TOKEN', token)
export default {
baseCacheKey,
cacheSave,
cacheRestoreTimeoutMs,
bazeliskCache: {
enabled: core.getBooleanInput('bazelisk-cache'),
files: [`${moduleRoot}/.bazelversion`],
name: 'bazelisk',
paths: [core.toPosixPath(`${userCacheDir}/bazelisk`)]
},
bazeliskVersion,
bazelrc,
diskCache: {
enabled: diskCacheEnabled,
files: [
...repositoryCacheFiles,
`${moduleRoot}/**/BUILD.bazel`,
`${moduleRoot}/**/BUILD`
],
name: diskCacheName,
paths: [bazelDisk]
},
externalCache,
paths: {
bazelExternal,
bazelOutputBase: core.toPosixPath(bazelOutputBase),
bazelrc: bazelrcPaths
},
os: {
arch,
platform,
},
repositoryCache: {
enabled: repositoryCacheEnabled,
files: repositoryCacheFiles,
name: 'repository',
paths: [bazelRepository]
},
}