[bazel] Setup remote cache settings on bootstrap (#121445) (#123303)

(cherry picked from commit e39faea65d)

# Conflicts:
#	.gitignore
#	packages/kbn-pm/dist/index.js
This commit is contained in:
Brian Seeders 2022-01-18 17:17:11 -05:00 committed by GitHub
parent dbecdeda85
commit f30a3897d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 924 additions and 717 deletions

View file

@ -132,6 +132,9 @@ coverage --instrument_test_targets
# Metadata settings
build --workspace_status_command="node ./src/dev/bazel_workspace_status.js"
# Load remote cache settings, if they exist
try-import %workspace%/.bazelrc.cache
# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
# This needs to be last statement in this

1
.gitignore vendored
View file

@ -90,6 +90,7 @@ report.asciidoc
/bazel
/bazel-*
.bazelrc.user
.bazelrc.cache
# Exclude renovate config file (we only need it on master)
renovate.json5

File diff suppressed because one or more lines are too long

View file

@ -23,6 +23,7 @@ import {
runBazel,
yarnIntegrityFileExists,
} from '../utils/bazel';
import { setupRemoteCache } from '../utils/bazel/setup_remote_cache';
export const BootstrapCommand: ICommand = {
description: 'Install dependencies and crosslink projects',
@ -55,6 +56,9 @@ export const BootstrapCommand: ICommand = {
// Install bazel machinery tools if needed
await installBazelTools(rootPath);
// Setup remote cache settings in .bazelrc.cache if needed
await setupRemoteCache(rootPath);
// Bootstrap process for Bazel packages
// Bazel is now managing dependencies so yarn install
// will happen as part of this

View file

@ -0,0 +1,92 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import dedent from 'dedent';
import { existsSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { spawn } from '../child_process';
import { log } from '../log';
async function isVaultAvailable() {
try {
await spawn('vault', ['--version'], { stdio: 'pipe' });
return true;
} catch {
return false;
}
}
async function isElasticCommitter() {
try {
const { stdout: email } = await spawn('git', ['config', 'user.email'], {
stdio: 'pipe',
});
return email.trim().endsWith('@elastic.co');
} catch {
return false;
}
}
export async function setupRemoteCache(repoRootPath: string) {
// The remote cache is only for Elastic employees working locally (CI cache settings are handled elsewhere)
if (process.env.CI || !(await isElasticCommitter())) {
return;
}
log.debug(`[bazel_tools] setting up remote cache settings if necessary`);
const settingsPath = resolve(repoRootPath, '.bazelrc.cache');
if (existsSync(settingsPath)) {
log.debug(`[bazel_tools] remote cache settings already exist, skipping`);
return;
}
if (!(await isVaultAvailable())) {
log.info('[bazel_tools] vault is not available, unable to setup remote cache settings.');
log.info('[bazel_tools] building packages will work, but will be slower in many cases.');
log.info('[bazel_tools] reach out to Operations if you need assistance with this.');
return;
}
let apiKey = '';
try {
const { stdout } = await spawn(
'vault',
['read', '-field=readonly-key', 'secret/kibana-issues/dev/bazel-remote-cache'],
{
stdio: 'pipe',
}
);
apiKey = stdout.trim();
} catch (ex: unknown) {
log.info(
'[bazel_tools] unable to read bazel remote cache key from vault, are you authenticated?'
);
log.info('[bazel_tools] building packages will work, but will be slower in many cases.');
log.info('[bazel_tools] reach out to Operations if you need assistance with this.');
log.info(`[bazel_tools] ${ex}`);
return;
}
const contents = dedent`
# V1 - This file is automatically generated by 'yarn kbn bootstrap'
# To regenerate this file, delete it and run 'yarn kbn bootstrap' again.
build --bes_results_url=https://app.buildbuddy.io/invocation/
build --bes_backend=grpcs://cloud.buildbuddy.io
build --remote_cache=grpcs://cloud.buildbuddy.io
build --remote_timeout=3600
build --remote_header=${apiKey}
`;
writeFileSync(settingsPath, contents);
log.info(`[bazel_tools] remote cache settings written to ${settingsPath}`);
}