mirror of
https://github.com/Swatinem/rust-cache.git
synced 2025-08-15 05:05:09 +00:00
Accept additional env var filters as input
This commit is contained in:
parent
c4f3eceb2f
commit
db260f722e
@ -26,6 +26,10 @@ An optional key that is added to the automatic cache key.
|
|||||||
: `sharedKey`
|
: `sharedKey`
|
||||||
An additional key that is stable over multiple jobs.
|
An additional key that is stable over multiple jobs.
|
||||||
|
|
||||||
|
: `envVars`
|
||||||
|
A space-separated list of regular expressions that define additional environment variable filters.
|
||||||
|
These are added to an additional cache key that's generated from environment variable contents.
|
||||||
|
|
||||||
: `working-directory`
|
: `working-directory`
|
||||||
The working directory the action operates in, is case the cargo project is not
|
The working directory the action operates in, is case the cargo project is not
|
||||||
located in the repo root.
|
located in the repo root.
|
||||||
|
@ -8,6 +8,9 @@ inputs:
|
|||||||
sharedKey:
|
sharedKey:
|
||||||
description: "An additional cache key that is stable over multiple jobs"
|
description: "An additional cache key that is stable over multiple jobs"
|
||||||
required: false
|
required: false
|
||||||
|
envVars:
|
||||||
|
description: "Additional environment variables to include in the cache key, separeted by spaces"
|
||||||
|
required: false
|
||||||
working-directory:
|
working-directory:
|
||||||
description: "The working directory this action should operate in"
|
description: "The working directory this action should operate in"
|
||||||
required: false
|
required: false
|
||||||
|
@ -71,7 +71,9 @@ export async function getCacheConfig(): Promise<CacheConfig> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
key += `${getEnvKey()}-`;
|
const extraEnvKeys = core.getInput("envVars").split(/\s+/);
|
||||||
|
|
||||||
|
key += `${getEnvKey(extraEnvKeys)}-`;
|
||||||
key += await getRustKey();
|
key += await getRustKey();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -106,9 +108,22 @@ export async function getCargoBins(): Promise<Set<string>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEnvKey(): string {
|
/**
|
||||||
|
* Create a key hash, generated from environment variables.
|
||||||
|
*
|
||||||
|
* The available environment variables are filtered by a set of defaults that are common for Rust
|
||||||
|
* projects and should apply to almost all runs, as they modify the Rustc compiler's, Clippy's and
|
||||||
|
* other tools' behavior.
|
||||||
|
*
|
||||||
|
* @param extraKeys additional user-provided keys that are added to the default list. These are
|
||||||
|
* treated as regular expressions ({@link RegExp}), and will each be surrounded by a `^` and `$`,
|
||||||
|
* to make sure they are matched against the whole env var name.
|
||||||
|
* @returns An SHA-1 hash over all the environment variable values, whose names were not filtered
|
||||||
|
* out. The hash is returned as hex-string, **reduced to half its length**.
|
||||||
|
*/
|
||||||
|
function getEnvKey(extraKeys: string[]): string {
|
||||||
const hasher = crypto.createHash("sha1");
|
const hasher = crypto.createHash("sha1");
|
||||||
const validKeys = [
|
const defaultValidKeys = [
|
||||||
/^CARGO_.+$/,
|
/^CARGO_.+$/,
|
||||||
/^CC_.+$/,
|
/^CC_.+$/,
|
||||||
/^CXX_.+$/,
|
/^CXX_.+$/,
|
||||||
@ -120,8 +135,11 @@ function getEnvKey(): string {
|
|||||||
/^RUSTFMT$/,
|
/^RUSTFMT$/,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Combine default key filters with user-provided ones.
|
||||||
|
const keyFilter = defaultValidKeys.concat(extraKeys.map((key) => new RegExp(`^${key}$`)));
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(process.env)) {
|
for (const [key, value] of Object.entries(process.env)) {
|
||||||
if (validKeys.some((re) => re.test(key)) && value) {
|
if (keyFilter.some((re) => re.test(key)) && value) {
|
||||||
hasher.update(`${key}=${value}`);
|
hasher.update(`${key}=${value}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user