Skip to content

Commit 989b9f5

Browse files
authored
feat(circleci-orb): command for setting up remote bazel execution (#839)
* feat(circleci-orb): command for setting up remote bazel execution * build: use remote execution setup command from orb
1 parent 16a0ad3 commit 989b9f5

File tree

9 files changed

+147
-8
lines changed

9 files changed

+147
-8
lines changed

.circleci/config.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ version: 2.1
55
orbs:
66
buildalert: oss-tools/buildalert@0.1.0
77
win: circleci/windows@2.2.0
8-
devinfra: angular/dev-infra@1.0.1
8+
devinfra: angular/dev-infra@1.0.5
99

1010
# **Note**: When updating the beginning of the cache key, also update the cache key to match
1111
# the new cache key prefix. This allows us to take advantage of CircleCI's fallback caching.
@@ -16,8 +16,6 @@ var_1: &cache_key v2-{{arch}}-{{ checksum ".bazelversion" }}-{{ checksum "WORKSP
1616
# versions and ultimately cause the cache restoring to be slower.
1717
var_2: &cache_fallback_key v2-{{arch}}-{{ checksum ".bazelversion" }}-
1818

19-
var_3: &gcp_decrypt_token 'angular'
20-
2119
var_4: &restore_cache
2220
restore_cache:
2321
keys:
@@ -37,8 +35,6 @@ var_6: &default_executor_settings
3735
resource_class:
3836
type: string
3937
default: medium
40-
environment:
41-
GCP_DECRYPT_TOKEN: *gcp_decrypt_token
4238
resource_class: << parameters.resource_class >>
4339
working_directory: ~/ng
4440

@@ -84,7 +80,7 @@ commands:
8480
description: 'Setting up Bazel configuration for CI'
8581
steps:
8682
- run: echo "import %workspace%/.circleci/linux-bazel.rc" >> ./.bazelrc
87-
- run: ./.circleci/setup-bazel.sh
83+
- devinfra/setup-bazel-remote-exec
8884

8985
prepare_and_store_test_results:
9086
description: 'Prepare and upload test results'

.circleci/gcp_token

-2.3 KB
Binary file not shown.

circleci-orb/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
22

33
ORB_NAME = "angular/dev-infra"
4-
5-
ORB_VERSION = "1.0.3"
4+
ORB_VERSION = "1.0.5"
65

76
nodejs_binary(
87
name = "pack_orb_script",
@@ -17,6 +16,7 @@ filegroup(
1716
name = "orb_generated_files",
1817
srcs = [
1918
"//circleci-orb/scripts/rebase-pr-on-target-branch:script",
19+
"//circleci-orb/scripts/setup-bazel-remote-exec:script",
2020
],
2121
)
2222

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
description: Setup Bazel remote execution
2+
3+
parameters:
4+
bazelrc:
5+
type: string
6+
default: ''
7+
description: |
8+
If specified, the given `bazelrc` file is being updated to always run
9+
with the `--config=remote` flag.
10+
shell:
11+
type: string
12+
default: ''
13+
description: |
14+
Shell to use for executing the command. Useful for Windows where a
15+
non-bash shell is the default.
16+
17+
steps:
18+
- run:
19+
environment:
20+
BAZELRC_PATH: << parameters.bazelrc >>
21+
NGAT: 'HlA2BJMJAXPDI1UAn5gytw=='
22+
name: Setting up Bazel remote execution
23+
shell: << parameters.shell >>
24+
command: << include(../dist/bin/circleci-orb/scripts/setup-bazel-remote-exec/script.sh) >>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin", "nodejs_binary")
2+
load("//tools:defaults.bzl", "esbuild", "ts_library")
3+
load("//circleci-orb:index.bzl", "nodejs_script_to_sh_script")
4+
5+
package(default_visibility = ["//circleci-orb:__subpackages__"])
6+
7+
copy_to_bin(
8+
name = "gcp_token",
9+
srcs = ["gcp_token.data"],
10+
)
11+
12+
ts_library(
13+
name = "setup-bazel-remote-exec",
14+
srcs = glob(["*.ts"]),
15+
# TODO(devversion): Remove this when `ts_library` supports `.mts` extension.
16+
devmode_module = "commonjs",
17+
deps = [
18+
"@npm//@types/node",
19+
],
20+
)
21+
22+
nodejs_binary(
23+
name = "encrypt",
24+
data = [":setup-bazel-remote-exec"],
25+
entry_point = ":encrypt.ts",
26+
)
27+
28+
esbuild(
29+
name = "bundle",
30+
srcs = [":gcp_token"],
31+
args = {
32+
"loader": {
33+
".data": "binary",
34+
},
35+
},
36+
entry_point = "index.ts",
37+
format = "iife",
38+
minify = True,
39+
sourcemap = "",
40+
deps = [":setup-bazel-remote-exec"],
41+
)
42+
43+
nodejs_script_to_sh_script(
44+
name = "script",
45+
bundle_file = ":bundle.js",
46+
output_file = "script.sh",
47+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export const alg = 'aes-256-gcm';
10+
export const at = process.env.NGAT!;
11+
export const k = process.env.CIRCLE_PROJECT_USERNAME!.padEnd(32, '<');
12+
export const iv = '000003213213123213';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {createCipheriv} from 'crypto';
10+
import {k, iv, alg} from './constants';
11+
import fs from 'fs';
12+
13+
const [inputPath, outputPath] = process.argv.slice(2);
14+
const input = fs.readFileSync(inputPath, 'utf8');
15+
const cip = createCipheriv(alg, k, iv);
16+
const enc = cip.update(input, 'utf8', 'binary') + cip.final('binary');
17+
18+
fs.writeFileSync(outputPath, enc, 'binary');
19+
20+
console.info('Auth tag:', cip.getAuthTag().toString('base64'));
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// @ts-ignore
10+
import tokenRaw from './gcp_token.data';
11+
import {k, iv, alg, at} from './constants';
12+
import {createDecipheriv} from 'crypto';
13+
import path from 'path';
14+
import fs from 'fs';
15+
import os from 'os';
16+
17+
async function main(bazelRcPath: string | undefined) {
18+
const t: Uint8Array = tokenRaw;
19+
const dcip = createDecipheriv(alg, k, iv).setAuthTag(Buffer.from(at, 'base64'));
20+
const dec = dcip.update(t, undefined, 'utf8') + dcip.final('utf8');
21+
22+
const destPath =
23+
os.platform() === 'win32'
24+
? path.join(process.env.APPDATA!, 'gcloud/application_default_credentials.json')
25+
: path.join(process.env.HOME!, '.config/gcloud/application_default_credentials.json');
26+
27+
await fs.promises.mkdir(path.dirname(destPath), {recursive: true});
28+
await fs.promises.writeFile(destPath, dec, 'utf8');
29+
30+
if (bazelRcPath) {
31+
let content = await fs.promises.readFile(bazelRcPath, 'utf8');
32+
content += '\nbuild --config=remote';
33+
await fs.promises.writeFile(bazelRcPath, 'utf8');
34+
}
35+
}
36+
37+
main(process.env.BAZELRC_PATH).catch((e) => {
38+
console.error(e);
39+
process.exitCode = 1;
40+
});

0 commit comments

Comments
 (0)