Skip to content

Commit

Permalink
refactor: allow list of restore keys
Browse files Browse the repository at this point in the history
  • Loading branch information
penandlim committed Feb 22, 2024
1 parent 1e74412 commit 042cfbf
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ jobs:
### Inputs
| **Name** | **Required** | **Default** | **Description** | **Type** |
| --------- | ------------ | ----------- | ------------------------------------------------------------------------------------------------------------ | -------- |
| `cache` | No | `true` | Whether to cache RPC responses or not. | bool |
| `version` | No | `nightly` | Version to install, e.g. `nightly` or `1.0.0`. **Note:** Foundry only has nightly builds for the time being. | string |
| **Name** | **Required** | **Default** | **Description** | **Type** |
| -------------------- | ------------ | ------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -------- |
| `cache` | No | `true` | Whether to cache RPC responses or not. | bool |
| `version` | No | `nightly` | Version to install, e.g. `nightly` or `1.0.0`. **Note:** Foundry only has nightly builds for the time being. | string |
| `cache-key` | No | `${{ github.job }}-${{ github.sha }}` | The cache key to use for caching. | string |
| `cache-restore-keys` | No | `${{ github.job }}-` | The cache key to use for restoring the cache. | string[] |

### RPC Caching

Expand All @@ -58,6 +60,36 @@ the `cache` input to `false`, like this:
cache: false
```

### Custom Cache Keys

You can also set custom cache keys using the `cache-key` and `cache-restore-keys` inputs. This is useful if you want to
customize how the cache may be shared between jobs. Note that cache-key must be unique for each run to successfully save
without conflicts.

For example, to share the cache across two different jobs, you could do something like this:

```yml
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
cache-key: custom-seed-test-${{ github.sha }}
cache-restore-keys: |-
custom-seed-test
custom-seed-
---
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
cache-key: custom-seed-coverage-${{ github.sha }}
cache-restore-keys: |-
custom-seed-coverage
custom-seed-
```

This will create two different caches, one for each job, but they will share the same cache key prefix. This means that
when restoring the cache in the second job, it will look for caches with keys that start with `custom-seed-`. This
allows you to share the cache across different jobs, or across different workflows if desired.

#### Deleting Caches

You can delete caches via the GitHub Actions user interface. Just go to your repo's "Actions" page:
Expand Down
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ inputs:
Caching is activated by default.
required: false
cache-key:
default: "${{ github.job }}-${{ github.sha }}"
description: |
A custom cache key to use.
This key is used to identify the cache. If not provided, a default key consisting of the job id and the commit hash is used.
required: false
cache-restore-keys:
default: |-
${{ github.job }}-
description: |
Custom cache restore keys to use.
This key is used to identify the cache to restore. If not provided, a default key consisting of the job id is used.
required: false
version:
default: "nightly"
description: |
Expand Down
11 changes: 8 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86185,12 +86185,17 @@ const PLATFORM = os.platform();
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];

async function restoreRPCCache() {
const customKeyInput = core.getInput("cache-key");
const customRestoreKeyInput = core.getInput("cache-restore-key");
const prefix = `${PLATFORM}-foundry-chain-fork-`;
const customKeyInput = core.getInput("cache-key");
const primaryKey = `${prefix}${customKeyInput}`;
const restoreKeys = [`${prefix}${customRestoreKeyInput}`, prefix];
core.saveState(State.CachePrimaryKey, primaryKey);
const defaultRestoreKeys = [prefix];
const customRestoreKeyInput = core
.getInput("cache-restore-keys")
.split(/[\r\n]/)
.map((input) => input.trim())
.filter((input) => input !== "");
const restoreKeys = customRestoreKeyInput.concat(defaultRestoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84678,12 +84678,17 @@ const PLATFORM = os.platform();
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];

async function restoreRPCCache() {
const customKeyInput = core.getInput("cache-key");
const customRestoreKeyInput = core.getInput("cache-restore-key");
const prefix = `${PLATFORM}-foundry-chain-fork-`;
const customKeyInput = core.getInput("cache-key");
const primaryKey = `${prefix}${customKeyInput}`;
const restoreKeys = [`${prefix}${customRestoreKeyInput}`, prefix];
core.saveState(State.CachePrimaryKey, primaryKey);
const defaultRestoreKeys = [prefix];
const customRestoreKeyInput = core
.getInput("cache-restore-keys")
.split(/[\r\n]/)
.map((input) => input.trim())
.filter((input) => input !== "");
const restoreKeys = customRestoreKeyInput.concat(defaultRestoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
Expand Down
2 changes: 1 addition & 1 deletion dist/save/index.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ const PLATFORM = os.platform();
const CACHE_PATHS = [path.join(HOME, ".foundry/cache/rpc")];

async function restoreRPCCache() {
const customKeyInput = core.getInput("cache-key");
const customRestoreKeyInput = core.getInput("cache-restore-key");
const prefix = `${PLATFORM}-foundry-chain-fork-`;
const customKeyInput = core.getInput("cache-key");
const primaryKey = `${prefix}${customKeyInput}`;
const restoreKeys = [`${prefix}${customRestoreKeyInput}`, prefix];
core.saveState(State.CachePrimaryKey, primaryKey);
const defaultRestoreKeys = [prefix];
const customRestoreKeyInput = core
.getInput("cache-restore-keys")
.split(/[\r\n]/)
.map((input) => input.trim())
.filter((input) => input !== "");
const restoreKeys = customRestoreKeyInput.concat(defaultRestoreKeys);
const cacheKey = await cache.restoreCache(CACHE_PATHS, primaryKey, restoreKeys);
if (!cacheKey) {
core.info("Cache not found");
Expand Down

0 comments on commit 042cfbf

Please sign in to comment.