From ace7744c51e92e37466a1f8c8f1df7a7d434e39a Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 30 Nov 2023 12:52:42 -0800 Subject: [PATCH] new: Add caching inputs/settings. (#8) --- CHANGELOG.md | 6 ++++++ README.md | 3 +++ action.yml | 6 ++++++ helpers.ts | 5 +++++ index.ts | 3 ++- package.json | 2 +- post.ts | 13 +++++++++++-- 7 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 985aab5..1398181 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index b932b7e..f52c16d 100644 --- a/README.md +++ b/README.md @@ -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". diff --git a/action.yml b/action.yml index b39efaf..644de7f 100644 --- a/action.yml +++ b/action.yml @@ -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).' diff --git a/helpers.ts b/helpers.ts index a086b28..6bff2f4 100644 --- a/helpers.ts +++ b/helpers.ts @@ -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'; @@ -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')); } diff --git a/index.ts b/index.ts index 709ff13..b072a95 100644 --- a/index.ts +++ b/index.ts @@ -9,11 +9,12 @@ import { getToolchainCacheKey, getToolsDir, installBin, + isCacheEnabled, isUsingMoon, } from './helpers'; async function restoreCache() { - if (!cache.isFeatureAvailable()) { + if (!isCacheEnabled()) { return; } diff --git a/package.json b/package.json index 444019b..3dc0720 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/post.ts b/post.ts index 6ea2585..eea7a4b 100644 --- a/post.ts +++ b/post.ts @@ -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 { @@ -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; }