Skip to content

Commit

Permalink
new: Add caching inputs/settings. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 30, 2023
1 parent 4b6d928 commit ace7744
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.2.0

- Added a `cache` input to toggle caching of the toolchain. Defaults to true.
- Added a `cache-base` input. When provided, will only save cache on this branch/ref, but will
restore cache on all branches/refs.

# 0.1.2

- Improve cache key checks.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
## Inputs
- `auto-install` - Auto-install tools on setup. Defaults to `false`.
- `cache` - Toggle caching of the toolchain directory. Defaults to `true`.
- `cache-base` - Base branch/ref to save a warmup cache on. Other branches/refs will restore from
this base.
- `moon-version` - Version of moon to explicitly install (if repository is using moon). Defaults to
"latest".
- `proto-version` - Version of proto to explicitly install. Defaults to "latest".
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ inputs:
auto-install:
default: false
description: 'Auto-install tools on setup.'
cache:
description: 'Toggle caching of the toolchain directory.'
default: true
cache-base:
description:
'Base branch/ref to save a warmup cache on. Other branches/refs will restore from this base.'
moon-version:
default: 'latest'
description: 'Version of moon to install (if repository is using moon).'
Expand Down
5 changes: 5 additions & 0 deletions helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import execa from 'execa';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as glob from '@actions/glob';
import * as tc from '@actions/tool-cache';
Expand Down Expand Up @@ -40,6 +41,10 @@ export function getWorkingDir() {
return process.env.GITHUB_WORKSPACE ?? process.cwd();
}

export function isCacheEnabled(): boolean {
return core.getBooleanInput('cache') && cache.isFeatureAvailable();
}

export function isUsingMoon() {
return fs.existsSync(path.join(getWorkingDir(), core.getInput('workspace-root'), '.moon'));
}
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
getToolchainCacheKey,
getToolsDir,
installBin,
isCacheEnabled,
isUsingMoon,
} from './helpers';

async function restoreCache() {
if (!cache.isFeatureAvailable()) {
if (!isCacheEnabled()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonrepo/setup-toolchain",
"version": "0.1.2",
"version": "0.2.0",
"description": "A GitHub action to setup and cache the proto and moon toolchains.",
"main": "dist/index.js",
"scripts": {
Expand Down
13 changes: 11 additions & 2 deletions post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import execa from 'execa';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import { getPluginsDir, getToolchainCacheKey, getToolsDir } from './helpers';
import { getPluginsDir, getToolchainCacheKey, getToolsDir, isCacheEnabled } from './helpers';

async function cleanToolchain() {
try {
Expand All @@ -14,8 +14,17 @@ async function cleanToolchain() {
}
}

function shouldSaveCache() {
const base = core.getInput('cache-base');

// Only save the cache for the following 2 scenarios:
// - If not using the base warmup strategy.
// - If using the base warmup strategy, and the current ref matches.
return !base || !!(base && !!(process.env.GITHUB_REF_NAME ?? '').match(base));
}

async function saveCache() {
if (!cache.isFeatureAvailable()) {
if (!isCacheEnabled() || !shouldSaveCache()) {
return;
}

Expand Down

0 comments on commit ace7744

Please sign in to comment.