refactor
This commit is contained in:
parent
319902179c
commit
729d401ac3
12 changed files with 77 additions and 32 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
|
@ -21,6 +21,8 @@ jobs:
|
|||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: ./
|
||||
with:
|
||||
remote-cache: true
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: bazelbuild/examples
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ inputs:
|
|||
description: Google Cloud account key for remote cache
|
||||
required: false
|
||||
default: ""
|
||||
remote-cache:
|
||||
description: Use built-in remote cache server
|
||||
required: false
|
||||
default: "false"
|
||||
repository-cache:
|
||||
description: Cache repositories based on MODULE.bazel/WORKSPACE
|
||||
required: false
|
||||
|
|
|
|||
15
config.js
15
config.js
|
|
@ -46,6 +46,15 @@ if (diskCacheEnabled) {
|
|||
}
|
||||
}
|
||||
|
||||
const remoteCacheServerUrl = 'http://localhost:9889/cache'
|
||||
const remoteCacheEnabled = core.getBooleanInput('remote-cache')
|
||||
if (remoteCacheEnabled) {
|
||||
bazelrc.push(`build --remote_cache=${remoteCacheServerUrl}\n`)
|
||||
if (diskCacheEnabled) {
|
||||
core.error('Disk cache and remote cache cannot be enabled at the same time')
|
||||
}
|
||||
}
|
||||
|
||||
const repositoryCacheConfig = core.getInput('repository-cache')
|
||||
const repositoryCacheEnabled = repositoryCacheConfig !== 'false'
|
||||
let repositoryCacheFiles = [
|
||||
|
|
@ -151,9 +160,9 @@ module.exports = {
|
|||
name: 'repository',
|
||||
paths: [bazelRepository]
|
||||
},
|
||||
remoteCacheServer: {
|
||||
enabled: true,
|
||||
url: 'http://localhost:8080/cache',
|
||||
remoteCache: {
|
||||
enabled: remoteCacheEnabled,
|
||||
logPath: `${os.tmpdir()}/remote-cache-server.log`,
|
||||
url: remoteCacheServerUrl,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
22
dist/main/index.js
vendored
22
dist/main/index.js
vendored
|
|
@ -52,6 +52,15 @@ if (diskCacheEnabled) {
|
|||
}
|
||||
}
|
||||
|
||||
const remoteCacheServerUrl = 'http://localhost:9889/cache'
|
||||
const remoteCacheEnabled = core.getBooleanInput('remote-cache')
|
||||
if (remoteCacheEnabled) {
|
||||
bazelrc.push(`build --remote_cache=${remoteCacheServerUrl}\n`)
|
||||
if (diskCacheEnabled) {
|
||||
core.error('Disk cache and remote cache cannot be enabled at the same time')
|
||||
}
|
||||
}
|
||||
|
||||
const repositoryCacheConfig = core.getInput('repository-cache')
|
||||
const repositoryCacheEnabled = repositoryCacheConfig !== 'false'
|
||||
let repositoryCacheFiles = [
|
||||
|
|
@ -157,10 +166,10 @@ module.exports = {
|
|||
name: 'repository',
|
||||
paths: [bazelRepository]
|
||||
},
|
||||
remoteCacheServer: {
|
||||
enabled: true,
|
||||
url: 'http://localhost:8080/cache',
|
||||
remoteCache: {
|
||||
enabled: remoteCacheEnabled,
|
||||
logPath: `${os.tmpdir()}/remote-cache-server.log`,
|
||||
url: remoteCacheServerUrl,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -96976,7 +96985,6 @@ async function setupBazelrc() {
|
|||
`startup --output_base=${config.paths.bazelOutputBase}\n`
|
||||
)
|
||||
fs.appendFileSync(bazelrcPath, config.bazelrc.join("\n"))
|
||||
fs.appendFileSync(bazelrcPath, `build --remote_cache=${config.remoteCacheServer.url}\n`)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97045,9 +97053,13 @@ async function restoreCache(cacheConfig) {
|
|||
}
|
||||
|
||||
async function startRemoteCacheServer() {
|
||||
core.startGroup("Remote cache server")
|
||||
if (!config.remoteCacheServer.enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
core.startGroup("Remote cache server")
|
||||
core.info(`Remote cache server log file path: ${config.remoteCacheServer.logPath}`)
|
||||
|
||||
const log = fs.openSync(config.remoteCacheServer.logPath, 'a')
|
||||
const remoteCacheServer = path.join(__dirname, '..', 'remote-cache-server', 'index.js')
|
||||
const serverProcess = spawn(process.execPath, [remoteCacheServer], {
|
||||
|
|
|
|||
2
dist/main/index.js.map
vendored
2
dist/main/index.js.map
vendored
File diff suppressed because one or more lines are too long
19
dist/post/index.js
vendored
19
dist/post/index.js
vendored
|
|
@ -52,6 +52,15 @@ if (diskCacheEnabled) {
|
|||
}
|
||||
}
|
||||
|
||||
const remoteCacheServerUrl = 'http://localhost:9889/cache'
|
||||
const remoteCacheEnabled = core.getBooleanInput('remote-cache')
|
||||
if (remoteCacheEnabled) {
|
||||
bazelrc.push(`build --remote_cache=${remoteCacheServerUrl}\n`)
|
||||
if (diskCacheEnabled) {
|
||||
core.error('Disk cache and remote cache cannot be enabled at the same time')
|
||||
}
|
||||
}
|
||||
|
||||
const repositoryCacheConfig = core.getInput('repository-cache')
|
||||
const repositoryCacheEnabled = repositoryCacheConfig !== 'false'
|
||||
let repositoryCacheFiles = [
|
||||
|
|
@ -157,10 +166,10 @@ module.exports = {
|
|||
name: 'repository',
|
||||
paths: [bazelRepository]
|
||||
},
|
||||
remoteCacheServer: {
|
||||
enabled: true,
|
||||
url: 'http://localhost:8080/cache',
|
||||
remoteCache: {
|
||||
enabled: remoteCacheEnabled,
|
||||
logPath: `${os.tmpdir()}/remote-cache-server.log`,
|
||||
url: remoteCacheServerUrl,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95947,7 +95956,9 @@ async function run() {
|
|||
}
|
||||
|
||||
async function stopRemoteCacheServer() {
|
||||
core.info(fs.readFileSync(config.remoteCacheServer.logPath, 'utf8'))
|
||||
if (!config.remoteCacheServer.enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
const pid = core.getState('remote-cache-server-pid')
|
||||
if (pid) {
|
||||
|
|
|
|||
2
dist/post/index.js.map
vendored
2
dist/post/index.js.map
vendored
File diff suppressed because one or more lines are too long
15
dist/remote-cache-server/index.js
vendored
15
dist/remote-cache-server/index.js
vendored
|
|
@ -81894,13 +81894,14 @@ const fs = __nccwpck_require__(7147)
|
|||
const server = http.createServer(async (req, res) => {
|
||||
const { method, url } = req
|
||||
const [, , cacheType, sha] = url.split('/')
|
||||
const cacheKey = `setup-bazel-1-remote-cache-${sha}`
|
||||
const filePath = `/tmp/cache-${cacheType}-${sha}`
|
||||
|
||||
if (method === 'GET') {
|
||||
try {
|
||||
const cacheId = await cache.restoreCache([filePath], sha)
|
||||
const cacheId = await cache.restoreCache([filePath], cacheKey)
|
||||
if (!cacheId) {
|
||||
console.log(`Cache miss for ${sha}`)
|
||||
console.log(`Cache miss for ${cacheKey}`)
|
||||
res.writeHead(404)
|
||||
return res.end('Cache miss')
|
||||
}
|
||||
|
|
@ -81908,7 +81909,7 @@ const server = http.createServer(async (req, res) => {
|
|||
res.writeHead(200, { 'Content-Type': 'application/octet-stream' })
|
||||
res.end(data)
|
||||
} catch (error) {
|
||||
console.error(`Error retrieving cache for ${sha}: ${error}`)
|
||||
console.error(`Error retrieving cache for ${cacheKey}: ${error}`)
|
||||
res.writeHead(500)
|
||||
res.end('Internal Server Error')
|
||||
}
|
||||
|
|
@ -81918,12 +81919,12 @@ const server = http.createServer(async (req, res) => {
|
|||
req.on('end', async () => {
|
||||
try {
|
||||
fs.writeFileSync(filePath, Buffer.concat(data))
|
||||
await cache.saveCache([filePath], sha)
|
||||
console.log(`Cache saved for ${sha}`)
|
||||
await cache.saveCache([filePath], cacheKey)
|
||||
console.log(`Cache saved for ${cacheKey}`)
|
||||
res.writeHead(201)
|
||||
res.end('Cache saved')
|
||||
} catch (error) {
|
||||
console.error(`Error saving cache for ${sha}: ${error}`)
|
||||
console.error(`Error saving cache for ${cacheKey}: ${error}`)
|
||||
res.writeHead(500)
|
||||
res.end('Internal Server Error')
|
||||
}
|
||||
|
|
@ -81934,7 +81935,7 @@ const server = http.createServer(async (req, res) => {
|
|||
}
|
||||
})
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
const PORT = process.env.PORT || 9889
|
||||
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`))
|
||||
|
||||
})();
|
||||
|
|
|
|||
2
dist/remote-cache-server/index.js.map
vendored
2
dist/remote-cache-server/index.js.map
vendored
File diff suppressed because one or more lines are too long
7
index.js
7
index.js
|
|
@ -110,7 +110,6 @@ async function setupBazelrc() {
|
|||
`startup --output_base=${config.paths.bazelOutputBase}\n`
|
||||
)
|
||||
fs.appendFileSync(bazelrcPath, config.bazelrc.join("\n"))
|
||||
fs.appendFileSync(bazelrcPath, `build --remote_cache=${config.remoteCacheServer.url}\n`)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -179,9 +178,13 @@ async function restoreCache(cacheConfig) {
|
|||
}
|
||||
|
||||
async function startRemoteCacheServer() {
|
||||
core.startGroup("Remote cache server")
|
||||
if (!config.remoteCacheServer.enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
core.startGroup("Remote cache server")
|
||||
core.info(`Remote cache server log file path: ${config.remoteCacheServer.logPath}`)
|
||||
|
||||
const log = fs.openSync(config.remoteCacheServer.logPath, 'a')
|
||||
const remoteCacheServer = path.join(__dirname, '..', 'remote-cache-server', 'index.js')
|
||||
const serverProcess = spawn(process.execPath, [remoteCacheServer], {
|
||||
|
|
|
|||
4
post.js
4
post.js
|
|
@ -12,7 +12,9 @@ async function run() {
|
|||
}
|
||||
|
||||
async function stopRemoteCacheServer() {
|
||||
core.info(fs.readFileSync(config.remoteCacheServer.logPath, 'utf8'))
|
||||
if (!config.remoteCacheServer.enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
const pid = core.getState('remote-cache-server-pid')
|
||||
if (pid) {
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@ const fs = require('fs')
|
|||
const server = http.createServer(async (req, res) => {
|
||||
const { method, url } = req
|
||||
const [, , cacheType, sha] = url.split('/')
|
||||
const cacheKey = `setup-bazel-1-remote-cache-${sha}`
|
||||
const filePath = `/tmp/cache-${cacheType}-${sha}`
|
||||
|
||||
if (method === 'GET') {
|
||||
try {
|
||||
const cacheId = await cache.restoreCache([filePath], sha)
|
||||
const cacheId = await cache.restoreCache([filePath], cacheKey)
|
||||
if (!cacheId) {
|
||||
console.log(`Cache miss for ${sha}`)
|
||||
console.log(`Cache miss for ${cacheKey}`)
|
||||
res.writeHead(404)
|
||||
return res.end('Cache miss')
|
||||
}
|
||||
|
|
@ -20,7 +21,7 @@ const server = http.createServer(async (req, res) => {
|
|||
res.writeHead(200, { 'Content-Type': 'application/octet-stream' })
|
||||
res.end(data)
|
||||
} catch (error) {
|
||||
console.error(`Error retrieving cache for ${sha}: ${error}`)
|
||||
console.error(`Error retrieving cache for ${cacheKey}: ${error}`)
|
||||
res.writeHead(500)
|
||||
res.end('Internal Server Error')
|
||||
}
|
||||
|
|
@ -30,12 +31,12 @@ const server = http.createServer(async (req, res) => {
|
|||
req.on('end', async () => {
|
||||
try {
|
||||
fs.writeFileSync(filePath, Buffer.concat(data))
|
||||
await cache.saveCache([filePath], sha)
|
||||
console.log(`Cache saved for ${sha}`)
|
||||
await cache.saveCache([filePath], cacheKey)
|
||||
console.log(`Cache saved for ${cacheKey}`)
|
||||
res.writeHead(201)
|
||||
res.end('Cache saved')
|
||||
} catch (error) {
|
||||
console.error(`Error saving cache for ${sha}: ${error}`)
|
||||
console.error(`Error saving cache for ${cacheKey}: ${error}`)
|
||||
res.writeHead(500)
|
||||
res.end('Internal Server Error')
|
||||
}
|
||||
|
|
@ -46,5 +47,5 @@ const server = http.createServer(async (req, res) => {
|
|||
}
|
||||
})
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
const PORT = process.env.PORT || 9889
|
||||
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue