Skip to content

Commit ace7744

Browse files
authored
new: Add caching inputs/settings. (#8)
1 parent 4b6d928 commit ace7744

File tree

7 files changed

+34
-4
lines changed

7 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.2.0
2+
3+
- Added a `cache` input to toggle caching of the toolchain. Defaults to true.
4+
- Added a `cache-base` input. When provided, will only save cache on this branch/ref, but will
5+
restore cache on all branches/refs.
6+
17
# 0.1.2
28

39
- Improve cache key checks.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
## Inputs
3131
3232
- `auto-install` - Auto-install tools on setup. Defaults to `false`.
33+
- `cache` - Toggle caching of the toolchain directory. Defaults to `true`.
34+
- `cache-base` - Base branch/ref to save a warmup cache on. Other branches/refs will restore from
35+
this base.
3336
- `moon-version` - Version of moon to explicitly install (if repository is using moon). Defaults to
3437
"latest".
3538
- `proto-version` - Version of proto to explicitly install. Defaults to "latest".

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ inputs:
55
auto-install:
66
default: false
77
description: 'Auto-install tools on setup.'
8+
cache:
9+
description: 'Toggle caching of the toolchain directory.'
10+
default: true
11+
cache-base:
12+
description:
13+
'Base branch/ref to save a warmup cache on. Other branches/refs will restore from this base.'
814
moon-version:
915
default: 'latest'
1016
description: 'Version of moon to install (if repository is using moon).'

helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import os from 'node:os';
33
import path from 'node:path';
44
import execa from 'execa';
5+
import * as cache from '@actions/cache';
56
import * as core from '@actions/core';
67
import * as glob from '@actions/glob';
78
import * as tc from '@actions/tool-cache';
@@ -40,6 +41,10 @@ export function getWorkingDir() {
4041
return process.env.GITHUB_WORKSPACE ?? process.cwd();
4142
}
4243

44+
export function isCacheEnabled(): boolean {
45+
return core.getBooleanInput('cache') && cache.isFeatureAvailable();
46+
}
47+
4348
export function isUsingMoon() {
4449
return fs.existsSync(path.join(getWorkingDir(), core.getInput('workspace-root'), '.moon'));
4550
}

index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import {
99
getToolchainCacheKey,
1010
getToolsDir,
1111
installBin,
12+
isCacheEnabled,
1213
isUsingMoon,
1314
} from './helpers';
1415

1516
async function restoreCache() {
16-
if (!cache.isFeatureAvailable()) {
17+
if (!isCacheEnabled()) {
1718
return;
1819
}
1920

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonrepo/setup-toolchain",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "A GitHub action to setup and cache the proto and moon toolchains.",
55
"main": "dist/index.js",
66
"scripts": {

post.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import execa from 'execa';
33
import * as cache from '@actions/cache';
44
import * as core from '@actions/core';
5-
import { getPluginsDir, getToolchainCacheKey, getToolsDir } from './helpers';
5+
import { getPluginsDir, getToolchainCacheKey, getToolsDir, isCacheEnabled } from './helpers';
66

77
async function cleanToolchain() {
88
try {
@@ -14,8 +14,17 @@ async function cleanToolchain() {
1414
}
1515
}
1616

17+
function shouldSaveCache() {
18+
const base = core.getInput('cache-base');
19+
20+
// Only save the cache for the following 2 scenarios:
21+
// - If not using the base warmup strategy.
22+
// - If using the base warmup strategy, and the current ref matches.
23+
return !base || !!(base && !!(process.env.GITHUB_REF_NAME ?? '').match(base));
24+
}
25+
1726
async function saveCache() {
18-
if (!cache.isFeatureAvailable()) {
27+
if (!isCacheEnabled() || !shouldSaveCache()) {
1928
return;
2029
}
2130

0 commit comments

Comments
 (0)