Skip to content

Commit 3eb0119

Browse files
committed
Add support for RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH environment variable
1 parent 6955a80 commit 3eb0119

3 files changed

Lines changed: 64 additions & 7 deletions

File tree

common/reviews/api/rush-lib.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export class EnvironmentConfiguration {
238238
static get buildCacheCredential(): string | undefined;
239239
static get buildCacheEnabled(): boolean | undefined;
240240
static get buildCacheOverrideJson(): string | undefined;
241+
static get buildCacheOverrideJsonFilePath(): string | undefined;
241242
static get buildCacheWriteAllowed(): boolean | undefined;
242243
static get cobuildContextId(): string | undefined;
243244
static get cobuildLeafProjectLogOnlyAllowed(): boolean | undefined;
@@ -276,6 +277,7 @@ export const EnvironmentVariableNames: {
276277
readonly RUSH_BUILD_CACHE_ENABLED: "RUSH_BUILD_CACHE_ENABLED";
277278
readonly RUSH_BUILD_CACHE_WRITE_ALLOWED: "RUSH_BUILD_CACHE_WRITE_ALLOWED";
278279
readonly RUSH_BUILD_CACHE_OVERRIDE_JSON: "RUSH_BUILD_CACHE_OVERRIDE_JSON";
280+
readonly RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH: "RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH";
279281
readonly RUSH_COBUILD_CONTEXT_ID: "RUSH_COBUILD_CONTEXT_ID";
280282
readonly RUSH_COBUILD_RUNNER_ID: "RUSH_COBUILD_RUNNER_ID";
281283
readonly RUSH_COBUILD_LEAF_PROJECT_LOG_ONLY_ALLOWED: "RUSH_COBUILD_LEAF_PROJECT_LOG_ONLY_ALLOWED";

libraries/rush-lib/src/api/BuildCacheConfiguration.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,22 @@ export class BuildCacheConfiguration {
220220
`${EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON} environment variable`
221221
);
222222
} else {
223-
try {
224-
buildCacheJson = await JsonFile.loadAndValidateAsync(jsonFilePath, BUILD_CACHE_JSON_SCHEMA);
225-
} catch (e) {
226-
if (!FileSystem.isNotExistError(e)) {
227-
throw e;
228-
} else {
229-
return undefined;
223+
const buildCacheOverrideJsonFilePath: string | undefined =
224+
EnvironmentConfiguration.buildCacheOverrideJsonFilePath;
225+
if (buildCacheOverrideJsonFilePath) {
226+
buildCacheJson = await JsonFile.loadAndValidateAsync(
227+
buildCacheOverrideJsonFilePath,
228+
BUILD_CACHE_JSON_SCHEMA
229+
);
230+
} else {
231+
try {
232+
buildCacheJson = await JsonFile.loadAndValidateAsync(jsonFilePath, BUILD_CACHE_JSON_SCHEMA);
233+
} catch (e) {
234+
if (!FileSystem.isNotExistError(e)) {
235+
throw e;
236+
} else {
237+
return undefined;
238+
}
230239
}
231240
}
232241
}

libraries/rush-lib/src/api/EnvironmentConfiguration.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,28 @@ export const EnvironmentVariableNames = {
150150
*
151151
* This is useful for testing purposes, or for OSS repos that are have a local-only cache, but can have
152152
* a different cache configuration in CI/CD pipelines.
153+
*
154+
* @remarks
155+
* This is similar to {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH}, but it allows you to specify
156+
* a JSON string instead of a file path. The two environment variables are mutually exclusive, meaning you can
157+
* only use one of them at a time.
153158
*/
154159
RUSH_BUILD_CACHE_OVERRIDE_JSON: 'RUSH_BUILD_CACHE_OVERRIDE_JSON',
155160

161+
/**
162+
* Set this environment variable to the path to a `build-cache.json` file to override the build cache configuration
163+
* that normally lives at `common/config/rush/build-cache.json`.
164+
*
165+
* This is useful for testing purposes, or for OSS repos that are have a local-only cache, but can have
166+
* a different cache configuration in CI/CD pipelines.
167+
*
168+
* @remarks
169+
* This is similar to {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON}, but it allows you to specify
170+
* a file path instead of a JSON string. The two environment variables are mutually exclusive, meaning you can
171+
* only use one of them at a time.
172+
*/
173+
RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH: 'RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH',
174+
156175
/**
157176
* Setting this environment variable opts into running with cobuilds. The context id should be the same across
158177
* multiple VMs, but changed when it is a new round of cobuilds.
@@ -261,6 +280,8 @@ export class EnvironmentConfiguration {
261280

262281
private static _buildCacheOverrideJson: string | undefined;
263282

283+
private static _buildCacheOverrideJsonFilePath: string | undefined;
284+
264285
private static _cobuildContextId: string | undefined;
265286

266287
private static _cobuildRunnerId: string | undefined;
@@ -380,6 +401,15 @@ export class EnvironmentConfiguration {
380401
return EnvironmentConfiguration._buildCacheOverrideJson;
381402
}
382403

404+
/**
405+
* If set, overrides the build cache configuration that normally lives at `common/config/rush/build-cache.json`.
406+
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH}
407+
*/
408+
public static get buildCacheOverrideJsonFilePath(): string | undefined {
409+
EnvironmentConfiguration._ensureValidated();
410+
return EnvironmentConfiguration._buildCacheOverrideJsonFilePath;
411+
}
412+
383413
/**
384414
* Provides a determined cobuild context id if configured
385415
* See {@link EnvironmentVariableNames.RUSH_COBUILD_CONTEXT_ID}
@@ -542,6 +572,11 @@ export class EnvironmentConfiguration {
542572
break;
543573
}
544574

575+
case EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH: {
576+
EnvironmentConfiguration._buildCacheOverrideJsonFilePath = value;
577+
break;
578+
}
579+
545580
case EnvironmentVariableNames.RUSH_COBUILD_CONTEXT_ID: {
546581
EnvironmentConfiguration._cobuildContextId = value;
547582
break;
@@ -603,6 +638,17 @@ export class EnvironmentConfiguration {
603638
);
604639
}
605640

641+
if (
642+
EnvironmentConfiguration._buildCacheOverrideJsonFilePath &&
643+
EnvironmentConfiguration._buildCacheOverrideJson
644+
) {
645+
throw new Error(
646+
`Environment variable ${EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON_FILE_PATH} and ` +
647+
`${EnvironmentVariableNames.RUSH_BUILD_CACHE_OVERRIDE_JSON} are mutually exclusive. ` +
648+
`Only one may be specified.`
649+
);
650+
}
651+
606652
// See doc comment for EnvironmentConfiguration._getRushGlobalFolderOverride().
607653
EnvironmentConfiguration._rushGlobalFolderOverride =
608654
EnvironmentConfiguration._getRushGlobalFolderOverride(process.env);

0 commit comments

Comments
 (0)