From e3017a763c9554c4df1f8374df36156df9c2bb8b Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 13 Dec 2024 20:12:42 +0100 Subject: [PATCH] Default to enable-cache: true on GitHub hosted runners (#193) Closes: #54 --- .github/workflows/test-cache.yml | 29 +++++++++++++++++++++++------ .github/workflows/test.yml | 2 ++ action.yml | 2 +- dist/save-cache/index.js | 9 ++++++++- dist/setup/index.js | 9 ++++++++- src/utils/inputs.ts | 10 +++++++++- 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test-cache.yml b/.github/workflows/test-cache.yml index ffeaa38..93dcb69 100644 --- a/.github/workflows/test-cache.yml +++ b/.github/workflows/test-cache.yml @@ -11,18 +11,26 @@ concurrency: jobs: test-setup-cache: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + enable-cache: [ "true", "false", "auto" ] + os: ["ubuntu-latest", "selfhosted-ubuntu-arm64"] steps: - uses: actions/checkout@v4 - name: Setup with cache uses: ./ with: - enable-cache: true - cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache + enable-cache: ${{ matrix.enable-cache }} + cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-${{ matrix.os }}-${{ matrix.enable-cache }} - run: uv sync working-directory: __tests__/fixtures/uv-project test-restore-cache: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + enable-cache: [ "true", "false", "auto" ] + os: [ "ubuntu-latest", "selfhosted-ubuntu-arm64" ] needs: test-setup-cache steps: - uses: actions/checkout@v4 @@ -30,15 +38,24 @@ jobs: id: restore uses: ./ with: - enable-cache: true - cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache + enable-cache: ${{ matrix.enable-cache }} + cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-${{ matrix.os }}-${{ matrix.enable-cache }} - name: Cache was hit + if: ${{ matrix.enable-cache == 'true' || (matrix.enable-cache == 'auto' && matrix.os == 'ubuntu-latest') }} run: | if [ "$CACHE_HIT" != "true" ]; then exit 1 fi env: CACHE_HIT: ${{ steps.restore.outputs.cache-hit }} + - name: Cache was not hit + if: ${{ matrix.enable-cache == 'false' || (matrix.enable-cache == 'auto' && matrix.os == 'selfhosted-ubuntu-arm64') }} + run: | + if [ "$CACHE_HIT" == "true" ]; then + exit 1 + fi + env: + CACHE_HIT: ${{ steps.restore.outputs.cache-hit }} - run: uv sync working-directory: __tests__/fixtures/uv-project test-setup-cache-requirements-txt: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e62fc62..54b97f3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -84,6 +84,8 @@ jobs: with: version: "0.3.2" checksum: ${{ matrix.checksum }} + - run: uv sync + working-directory: __tests__/fixtures/uv-project test-with-explicit-token: runs-on: ubuntu-latest steps: diff --git a/action.yml b/action.yml index 4f5341f..a545c19 100644 --- a/action.yml +++ b/action.yml @@ -20,7 +20,7 @@ inputs: default: ${{ github.token }} enable-cache: description: "Enable caching of the uv cache" - default: "false" + default: "auto" cache-dependency-glob: description: "Glob pattern to match files relative to the repository root to control diff --git a/dist/save-cache/index.js b/dist/save-cache/index.js index f03fa31..5bae647 100644 --- a/dist/save-cache/index.js +++ b/dist/save-cache/index.js @@ -91803,7 +91803,7 @@ const node_path_1 = __importDefault(__nccwpck_require__(6760)); exports.version = core.getInput("version"); exports.pythonVersion = core.getInput("python-version"); exports.checkSum = core.getInput("checksum"); -exports.enableCache = core.getInput("enable-cache") === "true"; +exports.enableCache = getEnableCache(); exports.cacheSuffix = core.getInput("cache-suffix") || ""; exports.cacheLocalPath = getCacheLocalPath(); exports.cacheDependencyGlob = core.getInput("cache-dependency-glob"); @@ -91812,6 +91812,13 @@ exports.ignoreNothingToCache = core.getInput("ignore-nothing-to-cache") === "tru exports.toolBinDir = getToolBinDir(); exports.toolDir = getToolDir(); exports.githubToken = core.getInput("github-token"); +function getEnableCache() { + const enableCacheInput = core.getInput("enable-cache"); + if (enableCacheInput === "auto") { + return process.env.RUNNER_ENVIRONMENT === "github-hosted"; + } + return enableCacheInput === "true"; +} function getToolBinDir() { const toolBinDirInput = core.getInput("tool-bin-dir"); if (toolBinDirInput !== "") { diff --git a/dist/setup/index.js b/dist/setup/index.js index 7ece694..0452816 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -99334,7 +99334,7 @@ const node_path_1 = __importDefault(__nccwpck_require__(6760)); exports.version = core.getInput("version"); exports.pythonVersion = core.getInput("python-version"); exports.checkSum = core.getInput("checksum"); -exports.enableCache = core.getInput("enable-cache") === "true"; +exports.enableCache = getEnableCache(); exports.cacheSuffix = core.getInput("cache-suffix") || ""; exports.cacheLocalPath = getCacheLocalPath(); exports.cacheDependencyGlob = core.getInput("cache-dependency-glob"); @@ -99343,6 +99343,13 @@ exports.ignoreNothingToCache = core.getInput("ignore-nothing-to-cache") === "tru exports.toolBinDir = getToolBinDir(); exports.toolDir = getToolDir(); exports.githubToken = core.getInput("github-token"); +function getEnableCache() { + const enableCacheInput = core.getInput("enable-cache"); + if (enableCacheInput === "auto") { + return process.env.RUNNER_ENVIRONMENT === "github-hosted"; + } + return enableCacheInput === "true"; +} function getToolBinDir() { const toolBinDirInput = core.getInput("tool-bin-dir"); if (toolBinDirInput !== "") { diff --git a/src/utils/inputs.ts b/src/utils/inputs.ts index 8d33ecf..470a9d9 100644 --- a/src/utils/inputs.ts +++ b/src/utils/inputs.ts @@ -4,7 +4,7 @@ import path from "node:path"; export const version = core.getInput("version"); export const pythonVersion = core.getInput("python-version"); export const checkSum = core.getInput("checksum"); -export const enableCache = core.getInput("enable-cache") === "true"; +export const enableCache = getEnableCache(); export const cacheSuffix = core.getInput("cache-suffix") || ""; export const cacheLocalPath = getCacheLocalPath(); export const cacheDependencyGlob = core.getInput("cache-dependency-glob"); @@ -15,6 +15,14 @@ export const toolBinDir = getToolBinDir(); export const toolDir = getToolDir(); export const githubToken = core.getInput("github-token"); +function getEnableCache(): boolean { + const enableCacheInput = core.getInput("enable-cache"); + if (enableCacheInput === "auto") { + return process.env.RUNNER_ENVIRONMENT === "github-hosted"; + } + return enableCacheInput === "true"; +} + function getToolBinDir(): string | undefined { const toolBinDirInput = core.getInput("tool-bin-dir"); if (toolBinDirInput !== "") {