Skip to content

Commit d42971f

Browse files
committed
feat(github-actions): allow a credential to be provided for configuring remote bazel setup (#2688)
Allow an input for a credential to provide authentication for RBE setup. PR Close #2688
1 parent 8f6f0ef commit d42971f

File tree

5 files changed

+61
-46
lines changed

5 files changed

+61
-46
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
- uses: ./github-actions/npm/checkout-and-setup-node
2828
- uses: ./github-actions/bazel/setup
2929
- uses: ./github-actions/bazel/configure-remote
30+
with:
31+
trusted_build: true
3032
- run: yarn install --immutable
3133
- name: Confirm code builds with typescript as expected
3234
run: yarn tsc -p tsconfig.json
@@ -42,6 +44,8 @@ jobs:
4244
- uses: ./github-actions/npm/checkout-and-setup-node
4345
- uses: ./github-actions/bazel/setup
4446
- uses: ./github-actions/bazel/configure-remote
47+
with:
48+
trusted_build: true
4549
- run: yarn install --immutable
4650
- run: yarn bazel test -- //...
4751

github-actions/bazel/configure-remote/action.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ inputs:
1212
description: |
1313
Whether to allow remote execution to be configured for Windows. By default,
1414
Windows is only configured to leverage remote caching.
15-
16-
shell:
17-
default: bash
18-
description: Shell to use for executing the configure script.
15+
google_credential:
16+
description: |
17+
A Google credential to be used as authentication for RBE API usages, implies
18+
the RBE interaction should be done as part of a trusted build.
1919
2020
runs:
21-
using: composite
22-
steps:
23-
- run: node $GITHUB_ACTION_PATH/configure-remote.cjs
24-
env:
25-
BAZELRC_PATH: ${{ inputs.bazelrc }}
26-
ALLOW_WINDOWS_RBE: ${{ inputs.allow_windows_rbe }}
27-
NGAT: 'bKddxrYADouso3haW7lCFA=='
28-
shell: ${{ inputs.shell }}
21+
using: 'node20'
22+
main: 'configure-remote.cjs'

github-actions/bazel/configure-remote/configure-remote.cjs

Lines changed: 25 additions & 24 deletions
Large diffs are not rendered by default.

github-actions/bazel/configure-remote/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
const owner = (process.env.CIRCLE_PROJECT_USERNAME ?? process.env.GITHUB_REPOSITORY_OWNER)!;
1010

1111
export const alg = 'aes-256-gcm';
12-
export const at = process.env.NGAT!;
12+
export const at = 'bKddxrYADouso3haW7lCFA==';
1313
export const k = owner.padEnd(32, '<');
1414
export const iv = '000003213213123213';

github-actions/bazel/configure-remote/index.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,43 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
// @ts-ignore
9+
// @ts-ignore-next-line
1010
import tokenRaw from './gcp_token.data';
1111
import {k, iv, alg, at} from './constants.js';
1212
import {createDecipheriv} from 'crypto';
1313
import path from 'path';
1414
import fs from 'fs';
1515
import os from 'os';
16-
import {exportVariable} from '@actions/core';
16+
import {exportVariable, getBooleanInput, getInput} from '@actions/core';
1717

18-
async function main(bazelRcPath: string | undefined) {
18+
async function main() {
1919
const isWindows = os.platform() === 'win32';
20-
const t: Uint8Array = tokenRaw;
21-
const dcip = createDecipheriv(alg, k, iv).setAuthTag(Buffer.from(at, 'base64'));
22-
const dec = dcip.update(t, undefined, 'utf8') + dcip.final('utf8');
20+
const bazelRcPath = getInput('bazelrc', {required: false, trimWhitespace: true});
21+
const allowWindowsRbe = getBooleanInput('allow_windows_rbe', {required: true});
22+
// If no credential is provided as an input, `getInput` will return an empty string
23+
let credential = getInput('google_credential', {required: false, trimWhitespace: true}) || null;
24+
// We treat any non-embedded credential as indication that this is a trusted build.
25+
const trustedBuild = credential !== null;
26+
27+
if (credential === null) {
28+
credential = getEmbeddedCredential();
29+
}
2330

2431
const destPath = isWindows
2532
? path.join(process.env.APPDATA!, 'gcloud/application_default_credentials.json')
2633
: path.join(process.env.HOME!, '.config/gcloud/application_default_credentials.json');
2734

2835
await fs.promises.mkdir(path.dirname(destPath), {recursive: true});
29-
await fs.promises.writeFile(destPath, dec, 'utf8');
36+
await fs.promises.writeFile(destPath, credential, 'utf8');
3037

31-
const allowWindowsRbe = process.env['ALLOW_WINDOWS_RBE'] === 'true';
3238
const configMode = isWindows && !allowWindowsRbe ? 'remote-cache' : 'remote';
3339

3440
if (bazelRcPath) {
3541
let content = await readFileGracefully(bazelRcPath);
3642
content += `\nbuild --config=${configMode}`;
43+
if (trustedBuild) {
44+
content += `\nbuild --config=trusted-build`;
45+
}
3746
await fs.promises.writeFile(bazelRcPath, content, 'utf8');
3847
}
3948

@@ -50,7 +59,14 @@ async function readFileGracefully(filePath: string): Promise<string> {
5059
}
5160
}
5261

53-
main(process.env.BAZELRC_PATH).catch((e) => {
62+
/** Extract the embeeded credential from the action. */
63+
function getEmbeddedCredential(): string {
64+
const t: Uint8Array = tokenRaw;
65+
const dcip = createDecipheriv(alg, k, iv).setAuthTag(Buffer.from(at, 'base64'));
66+
return dcip.update(t, undefined, 'utf8') + dcip.final('utf8');
67+
}
68+
69+
main().catch((e) => {
5470
console.error(e);
5571
process.exitCode = 1;
5672
});

0 commit comments

Comments
 (0)