mirror of
https://github.com/Swatinem/rust-cache
synced 2025-04-07 13:45:20 +00:00
small code style improvements, README and CHANGELOG updates
This commit is contained in:
parent
ccdddcc049
commit
e78327dd9e
|
@ -1,8 +1,13 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2.1.0
|
||||||
|
|
||||||
|
- Only hash `Cargo.{lock,toml}` files in the configured workspace directories.
|
||||||
|
|
||||||
## 2.0.2
|
## 2.0.2
|
||||||
|
|
||||||
- Avoid calling `cargo metadata` on pre-cleanup.
|
- Avoid calling `cargo metadata` on pre-cleanup.
|
||||||
|
- Added `prefix-key`, `cache-directories` and `cache-targets` options.
|
||||||
|
|
||||||
## 2.0.1
|
## 2.0.1
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,12 @@ sensible defaults.
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
with:
|
with:
|
||||||
# The prefix cache key, this can be changed to start a new cache manually
|
# The prefix cache key, this can be changed to start a new cache manually.
|
||||||
# default: "v0-rust"
|
# default: "v0-rust"
|
||||||
prefix-key: ""
|
prefix-key: ""
|
||||||
|
|
||||||
# An additional cache key that is stable over multiple jobs
|
# A cache key that is used instead of the automatic `job`-based key,
|
||||||
|
# and is stable over multiple jobs.
|
||||||
# default: empty
|
# default: empty
|
||||||
shared-key: ""
|
shared-key: ""
|
||||||
|
|
||||||
|
@ -41,10 +42,11 @@ sensible defaults.
|
||||||
# default: ". -> target"
|
# default: ". -> target"
|
||||||
workspaces: ""
|
workspaces: ""
|
||||||
|
|
||||||
# Additional non workspace directories, separated by newlines
|
# Additional non workspace directories to be cached, separated by newlines.
|
||||||
cache-directories: ""
|
cache-directories: ""
|
||||||
|
|
||||||
# Determines whether workspace `target` directories are cached.
|
# Determines whether workspace `target` directories are cached.
|
||||||
|
# If `false`, only the cargo registry will be cached.
|
||||||
# default: "true"
|
# default: "true"
|
||||||
cache-targets: ""
|
cache-targets: ""
|
||||||
|
|
||||||
|
|
18
action.yml
18
action.yml
|
@ -3,34 +3,34 @@ description: "A GitHub Action that implements smart caching for rust/cargo proje
|
||||||
author: "Arpad Borsos <swatinem@swatinem.de>"
|
author: "Arpad Borsos <swatinem@swatinem.de>"
|
||||||
inputs:
|
inputs:
|
||||||
prefix-key:
|
prefix-key:
|
||||||
description: "The prefix cache key, this can be changed to start a new cache manually"
|
description: "The prefix cache key, this can be changed to start a new cache manually."
|
||||||
required: false
|
required: false
|
||||||
default: "v0-rust"
|
default: "v0-rust"
|
||||||
shared-key:
|
shared-key:
|
||||||
description: "An additional cache key that is stable over multiple jobs"
|
description: "A cache key that is used instead of the automatic `job`-based key, and is stable over multiple jobs."
|
||||||
required: false
|
required: false
|
||||||
key:
|
key:
|
||||||
description: "An additional key for the cache"
|
description: "An additional cache key that is added alongside the automatic `job`-based cache key and can be used to further differentiate jobs."
|
||||||
required: false
|
required: false
|
||||||
env-vars:
|
env-vars:
|
||||||
description: "Additional environment variables to include in the cache key, separated by spaces"
|
description: "Additional environment variables to include in the cache key, separated by spaces."
|
||||||
required: false
|
required: false
|
||||||
workspaces:
|
workspaces:
|
||||||
description: "Paths to multiple Cargo workspaces and their target directories, separated by newlines"
|
description: "Paths to multiple Cargo workspaces and their target directories, separated by newlines."
|
||||||
required: false
|
required: false
|
||||||
cache-directories:
|
cache-directories:
|
||||||
description: "Additional non workspace directories, separated by newlines"
|
description: "Additional non workspace directories to be cached, separated by newlines."
|
||||||
required: false
|
required: false
|
||||||
cache-targets:
|
cache-targets:
|
||||||
description: "Determines whether workspace targets are cached"
|
description: "Determines whether workspace targets are cached. If `false`, only the cargo registry will be cached."
|
||||||
required: false
|
required: false
|
||||||
default: "true"
|
default: "true"
|
||||||
cache-on-failure:
|
cache-on-failure:
|
||||||
description: "Cache even if the build fails. Defaults to false"
|
description: "Cache even if the build fails. Defaults to false."
|
||||||
required: false
|
required: false
|
||||||
outputs:
|
outputs:
|
||||||
cache-hit:
|
cache-hit:
|
||||||
description: "A boolean value that indicates an exact match was found"
|
description: "A boolean value that indicates an exact match was found."
|
||||||
runs:
|
runs:
|
||||||
using: "node16"
|
using: "node16"
|
||||||
main: "dist/restore/index.js"
|
main: "dist/restore/index.js"
|
||||||
|
|
43
dist/restore/index.js
vendored
43
dist/restore/index.js
vendored
|
@ -64548,7 +64548,7 @@ class CacheConfig {
|
||||||
// Construct key prefix:
|
// Construct key prefix:
|
||||||
// This uses either the `shared-key` input,
|
// This uses either the `shared-key` input,
|
||||||
// or the `key` input combined with the `job` key.
|
// or the `key` input combined with the `job` key.
|
||||||
let key = lib_core.getInput("prefix-key");
|
let key = lib_core.getInput("prefix-key") || "v0-rust";
|
||||||
const sharedKey = lib_core.getInput("shared-key");
|
const sharedKey = lib_core.getInput("shared-key");
|
||||||
if (sharedKey) {
|
if (sharedKey) {
|
||||||
key += `-${sharedKey}`;
|
key += `-${sharedKey}`;
|
||||||
|
@ -64603,11 +64603,23 @@ class CacheConfig {
|
||||||
// might create/overwrite lockfiles.
|
// might create/overwrite lockfiles.
|
||||||
let lockHash = lib_core.getState(STATE_LOCKFILE_HASH);
|
let lockHash = lib_core.getState(STATE_LOCKFILE_HASH);
|
||||||
let keyFiles = JSON.parse(lib_core.getState(STATE_LOCKFILES) || "[]");
|
let keyFiles = JSON.parse(lib_core.getState(STATE_LOCKFILES) || "[]");
|
||||||
|
// Constructs the workspace config and paths to restore:
|
||||||
|
// The workspaces are given using a `$workspace -> $target` syntax.
|
||||||
|
const workspaces = [];
|
||||||
|
const workspacesInput = lib_core.getInput("workspaces") || ".";
|
||||||
|
for (const workspace of workspacesInput.trim().split("\n")) {
|
||||||
|
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
|
||||||
|
root = external_path_default().resolve(root);
|
||||||
|
target = external_path_default().join(root, target);
|
||||||
|
workspaces.push(new Workspace(root, target));
|
||||||
|
}
|
||||||
|
self.workspaces = workspaces;
|
||||||
if (!lockHash) {
|
if (!lockHash) {
|
||||||
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", {
|
keyFiles = keyFiles.concat(await globFiles("rust-toolchain\nrust-toolchain.toml"));
|
||||||
followSymbolicLinks: false,
|
for (const workspace of workspaces) {
|
||||||
});
|
const root = workspace.root;
|
||||||
keyFiles = await globber.glob();
|
keyFiles.push(...(await globFiles(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
|
||||||
|
}
|
||||||
keyFiles.sort((a, b) => a.localeCompare(b));
|
keyFiles.sort((a, b) => a.localeCompare(b));
|
||||||
hasher = external_crypto_default().createHash("sha1");
|
hasher = external_crypto_default().createHash("sha1");
|
||||||
for (const file of keyFiles) {
|
for (const file of keyFiles) {
|
||||||
|
@ -64622,24 +64634,13 @@ class CacheConfig {
|
||||||
self.keyFiles = keyFiles;
|
self.keyFiles = keyFiles;
|
||||||
key += `-${lockHash}`;
|
key += `-${lockHash}`;
|
||||||
self.cacheKey = key;
|
self.cacheKey = key;
|
||||||
// Constructs the workspace config and paths to restore:
|
|
||||||
// The workspaces are given using a `$workspace -> $target` syntax.
|
|
||||||
const workspaces = [];
|
|
||||||
const workspacesInput = lib_core.getInput("workspaces") || ".";
|
|
||||||
for (const workspace of workspacesInput.trim().split("\n")) {
|
|
||||||
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
|
|
||||||
root = external_path_default().resolve(root);
|
|
||||||
target = external_path_default().join(root, target);
|
|
||||||
workspaces.push(new Workspace(root, target));
|
|
||||||
}
|
|
||||||
self.workspaces = workspaces;
|
|
||||||
self.cachePaths = [config_CARGO_HOME];
|
self.cachePaths = [config_CARGO_HOME];
|
||||||
const cacheTargets = lib_core.getInput("cache-targets").toLowerCase();
|
const cacheTargets = lib_core.getInput("cache-targets").toLowerCase() || "true";
|
||||||
if (cacheTargets === "true") {
|
if (cacheTargets === "true") {
|
||||||
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
||||||
}
|
}
|
||||||
const cacheDirectories = lib_core.getInput("cache-directories");
|
const cacheDirectories = lib_core.getInput("cache-directories");
|
||||||
for (const dir of cacheDirectories.trim().split("\n")) {
|
for (const dir of cacheDirectories.trim().split(/\s+/).filter(Boolean)) {
|
||||||
self.cachePaths.push(dir);
|
self.cachePaths.push(dir);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
@ -64681,6 +64682,12 @@ async function getRustVersion() {
|
||||||
.filter((s) => s.length === 2);
|
.filter((s) => s.length === 2);
|
||||||
return Object.fromEntries(splits);
|
return Object.fromEntries(splits);
|
||||||
}
|
}
|
||||||
|
async function globFiles(pattern) {
|
||||||
|
const globber = await glob.create(pattern, {
|
||||||
|
followSymbolicLinks: false,
|
||||||
|
});
|
||||||
|
return await globber.glob();
|
||||||
|
}
|
||||||
|
|
||||||
;// CONCATENATED MODULE: ./src/cleanup.ts
|
;// CONCATENATED MODULE: ./src/cleanup.ts
|
||||||
|
|
||||||
|
|
43
dist/save/index.js
vendored
43
dist/save/index.js
vendored
|
@ -64548,7 +64548,7 @@ class CacheConfig {
|
||||||
// Construct key prefix:
|
// Construct key prefix:
|
||||||
// This uses either the `shared-key` input,
|
// This uses either the `shared-key` input,
|
||||||
// or the `key` input combined with the `job` key.
|
// or the `key` input combined with the `job` key.
|
||||||
let key = core.getInput("prefix-key");
|
let key = core.getInput("prefix-key") || "v0-rust";
|
||||||
const sharedKey = core.getInput("shared-key");
|
const sharedKey = core.getInput("shared-key");
|
||||||
if (sharedKey) {
|
if (sharedKey) {
|
||||||
key += `-${sharedKey}`;
|
key += `-${sharedKey}`;
|
||||||
|
@ -64603,11 +64603,23 @@ class CacheConfig {
|
||||||
// might create/overwrite lockfiles.
|
// might create/overwrite lockfiles.
|
||||||
let lockHash = core.getState(STATE_LOCKFILE_HASH);
|
let lockHash = core.getState(STATE_LOCKFILE_HASH);
|
||||||
let keyFiles = JSON.parse(core.getState(STATE_LOCKFILES) || "[]");
|
let keyFiles = JSON.parse(core.getState(STATE_LOCKFILES) || "[]");
|
||||||
|
// Constructs the workspace config and paths to restore:
|
||||||
|
// The workspaces are given using a `$workspace -> $target` syntax.
|
||||||
|
const workspaces = [];
|
||||||
|
const workspacesInput = core.getInput("workspaces") || ".";
|
||||||
|
for (const workspace of workspacesInput.trim().split("\n")) {
|
||||||
|
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
|
||||||
|
root = external_path_default().resolve(root);
|
||||||
|
target = external_path_default().join(root, target);
|
||||||
|
workspaces.push(new Workspace(root, target));
|
||||||
|
}
|
||||||
|
self.workspaces = workspaces;
|
||||||
if (!lockHash) {
|
if (!lockHash) {
|
||||||
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", {
|
keyFiles = keyFiles.concat(await globFiles("rust-toolchain\nrust-toolchain.toml"));
|
||||||
followSymbolicLinks: false,
|
for (const workspace of workspaces) {
|
||||||
});
|
const root = workspace.root;
|
||||||
keyFiles = await globber.glob();
|
keyFiles.push(...(await globFiles(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
|
||||||
|
}
|
||||||
keyFiles.sort((a, b) => a.localeCompare(b));
|
keyFiles.sort((a, b) => a.localeCompare(b));
|
||||||
hasher = external_crypto_default().createHash("sha1");
|
hasher = external_crypto_default().createHash("sha1");
|
||||||
for (const file of keyFiles) {
|
for (const file of keyFiles) {
|
||||||
|
@ -64622,24 +64634,13 @@ class CacheConfig {
|
||||||
self.keyFiles = keyFiles;
|
self.keyFiles = keyFiles;
|
||||||
key += `-${lockHash}`;
|
key += `-${lockHash}`;
|
||||||
self.cacheKey = key;
|
self.cacheKey = key;
|
||||||
// Constructs the workspace config and paths to restore:
|
|
||||||
// The workspaces are given using a `$workspace -> $target` syntax.
|
|
||||||
const workspaces = [];
|
|
||||||
const workspacesInput = core.getInput("workspaces") || ".";
|
|
||||||
for (const workspace of workspacesInput.trim().split("\n")) {
|
|
||||||
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
|
|
||||||
root = external_path_default().resolve(root);
|
|
||||||
target = external_path_default().join(root, target);
|
|
||||||
workspaces.push(new Workspace(root, target));
|
|
||||||
}
|
|
||||||
self.workspaces = workspaces;
|
|
||||||
self.cachePaths = [CARGO_HOME];
|
self.cachePaths = [CARGO_HOME];
|
||||||
const cacheTargets = core.getInput("cache-targets").toLowerCase();
|
const cacheTargets = core.getInput("cache-targets").toLowerCase() || "true";
|
||||||
if (cacheTargets === "true") {
|
if (cacheTargets === "true") {
|
||||||
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
||||||
}
|
}
|
||||||
const cacheDirectories = core.getInput("cache-directories");
|
const cacheDirectories = core.getInput("cache-directories");
|
||||||
for (const dir of cacheDirectories.trim().split("\n")) {
|
for (const dir of cacheDirectories.trim().split(/\s+/).filter(Boolean)) {
|
||||||
self.cachePaths.push(dir);
|
self.cachePaths.push(dir);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
@ -64681,6 +64682,12 @@ async function getRustVersion() {
|
||||||
.filter((s) => s.length === 2);
|
.filter((s) => s.length === 2);
|
||||||
return Object.fromEntries(splits);
|
return Object.fromEntries(splits);
|
||||||
}
|
}
|
||||||
|
async function globFiles(pattern) {
|
||||||
|
const globber = await glob.create(pattern, {
|
||||||
|
followSymbolicLinks: false,
|
||||||
|
});
|
||||||
|
return await globber.glob();
|
||||||
|
}
|
||||||
|
|
||||||
;// CONCATENATED MODULE: ./src/cleanup.ts
|
;// CONCATENATED MODULE: ./src/cleanup.ts
|
||||||
|
|
||||||
|
|
1
package-lock.json
generated
1
package-lock.json
generated
|
@ -5,7 +5,6 @@
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "rust-cache",
|
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
"license": "LGPL-3.0",
|
"license": "LGPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -50,7 +50,7 @@ export class CacheConfig {
|
||||||
// This uses either the `shared-key` input,
|
// This uses either the `shared-key` input,
|
||||||
// or the `key` input combined with the `job` key.
|
// or the `key` input combined with the `job` key.
|
||||||
|
|
||||||
let key = core.getInput("prefix-key");
|
let key = core.getInput("prefix-key") || "v0-rust";
|
||||||
|
|
||||||
const sharedKey = core.getInput("shared-key");
|
const sharedKey = core.getInput("shared-key");
|
||||||
if (sharedKey) {
|
if (sharedKey) {
|
||||||
|
@ -132,29 +132,23 @@ export class CacheConfig {
|
||||||
self.workspaces = workspaces;
|
self.workspaces = workspaces;
|
||||||
|
|
||||||
if (!lockHash) {
|
if (!lockHash) {
|
||||||
hasher = crypto.createHash("sha1");
|
keyFiles = keyFiles.concat(await globFiles("rust-toolchain\nrust-toolchain.toml"));
|
||||||
|
|
||||||
async function globHash(pattern: string): Promise<string[]> {
|
|
||||||
const globber = await glob.create(pattern, {
|
|
||||||
followSymbolicLinks: false,
|
|
||||||
});
|
|
||||||
return await globber.glob();
|
|
||||||
}
|
|
||||||
|
|
||||||
keyFiles = keyFiles.concat(await globHash("rust-toolchain\nrust-toolchain.toml"));
|
|
||||||
for (const workspace of workspaces) {
|
for (const workspace of workspaces) {
|
||||||
const root = workspace.root;
|
const root = workspace.root;
|
||||||
keyFiles = keyFiles.concat(await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`));
|
keyFiles.push(
|
||||||
|
...(await globFiles(
|
||||||
|
`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`,
|
||||||
|
)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
keyFiles.sort((a, b) => a.localeCompare(b));
|
keyFiles.sort((a, b) => a.localeCompare(b));
|
||||||
|
|
||||||
|
hasher = crypto.createHash("sha1");
|
||||||
for (const file of keyFiles) {
|
for (const file of keyFiles) {
|
||||||
for await (const chunk of fs.createReadStream(file)) {
|
for await (const chunk of fs.createReadStream(file)) {
|
||||||
hasher.update(chunk);
|
hasher.update(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lockHash = hasher.digest("hex");
|
lockHash = hasher.digest("hex");
|
||||||
|
|
||||||
core.saveState(STATE_LOCKFILE_HASH, lockHash);
|
core.saveState(STATE_LOCKFILE_HASH, lockHash);
|
||||||
|
@ -167,13 +161,13 @@ export class CacheConfig {
|
||||||
self.cacheKey = key;
|
self.cacheKey = key;
|
||||||
|
|
||||||
self.cachePaths = [CARGO_HOME];
|
self.cachePaths = [CARGO_HOME];
|
||||||
const cacheTargets = core.getInput("cache-targets").toLowerCase();
|
const cacheTargets = core.getInput("cache-targets").toLowerCase() || "true";
|
||||||
if (cacheTargets === "true") {
|
if (cacheTargets === "true") {
|
||||||
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
self.cachePaths.push(...workspaces.map((ws) => ws.target));
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheDirectories = core.getInput("cache-directories");
|
const cacheDirectories = core.getInput("cache-directories");
|
||||||
for (const dir of cacheDirectories.trim().split("\n")) {
|
for (const dir of cacheDirectories.trim().split(/\s+/).filter(Boolean)) {
|
||||||
self.cachePaths.push(dir);
|
self.cachePaths.push(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,3 +218,10 @@ async function getRustVersion(): Promise<RustVersion> {
|
||||||
.filter((s) => s.length === 2);
|
.filter((s) => s.length === 2);
|
||||||
return Object.fromEntries(splits);
|
return Object.fromEntries(splits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function globFiles(pattern: string): Promise<string[]> {
|
||||||
|
const globber = await glob.create(pattern, {
|
||||||
|
followSymbolicLinks: false,
|
||||||
|
});
|
||||||
|
return await globber.glob();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue