Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to disable dependency caching on demand #34

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ jobs:
with:
scarb-lock: ./subdir/Scarb.lock

- name: "Setup Scarb with caching disabled"
uses: ./
with:
cache: false

- name: "Create .tool-versions file"
run: echo "scarb 0.7.0" >> .tool-versions
- name: "Setup Scarb using `.tool-versions` file"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- `scarb-lock` - **Optional**. String.
- Stating a relative or absolute path to the `Scarb.lock` file used for caching dependencies.
- Empty/not specified: `Scarb.lock` in the working directory will be used.
- `cache` - **Optional**. Boolean.
- Enables caching Scarb dependencies.
- Empty/not specified: `true`.

## Outputs

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
scarb-lock:
description: Path to Scarb.lock file
required: false
cache:
description: Enable dependency caching
required: false
outputs:
scarb-prefix:
description: The prefix of the installed Scarb
Expand Down
9 changes: 8 additions & 1 deletion lib/cache-save.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import * as core from "@actions/core";
import * as cache from "@actions/cache";

import { getCacheDirectory, State } from "./cache-utils";
import { getCacheDirectory, isCacheEnabled, State } from "./cache-utils";

async function saveCache() {
const cacheInput = core.getInput("cache");

if (!isCacheEnabled(cacheInput)) {
core.info(`Caching disabled, not saving cache.`);
return;
}

try {
const primaryKey = core.getState(State.CachePrimaryKey);
const matchedKey = core.getState(State.CacheMatchedKey);
Expand Down
5 changes: 5 additions & 0 deletions lib/cache-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const State = {
CacheMatchedKey: "matched_key",
};

export function isCacheEnabled(cacheInput) {
cacheInput = cacheInput || "true";
return cacheInput === "true";
}

export async function getCacheDirectory() {
// NOTE: The `cache path` command was introduced in Scarb 0.7.0. We do not want to break compatibility with older
// versions yet, so we fall back to a well-known cache path if this command is not available.
Expand Down
20 changes: 13 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
import { downloadScarb } from "./download";
import { getOsTriplet } from "./platform";
import { restoreCache } from "./cache-restore";
import { isCacheEnabled } from "./cache-utils";

export default async function main() {
try {
const scarbVersionInput = core.getInput("scarb-version");
const toolVersionsPathInput = core.getInput("tool-versions");
const scarbLockPathInput = core.getInput("scarb-lock");
const cacheInput = core.getInput("cache");

const { repo: scarbRepo, version: scarbVersion } = await determineVersion(
scarbVersionInput,
Expand Down Expand Up @@ -48,13 +50,17 @@ export default async function main() {

core.setOutput("scarb-version", await getFullVersionFromScarb());

await restoreCache(scarbLockPathInput).catch((e) => {
core.error(
`There was an error when restoring cache: ${
e instanceof Error ? e.message : e
}`,
);
});
if (isCacheEnabled(cacheInput)) {
await restoreCache(scarbLockPathInput).catch((e) => {
core.error(
`There was an error when restoring cache: ${
e instanceof Error ? e.message : e
}`,
);
});
} else {
core.info(`Caching disabled, not restoring cache.`);
}
} catch (e) {
core.setFailed(e);
}
Expand Down
Loading