From c6845e72b88eeebe5ffbae4d18587859ca744a9b Mon Sep 17 00:00:00 2001 From: Adam Dinwoodie Date: Tue, 10 Jan 2023 21:31:53 +0000 Subject: [PATCH] Allow save-only/restore-only cache runs Add an option to restore a cache without creating a new one, or to create a cache without restoring an old one. This is useful in the scenario where the action is called multiple times in a run, as it allows the cache to only be restored on the first call, and for a new cache to only be written on the last call. This option replaces the `package-cache-key` option; as discussed in #6 that option isn't very useful. Additionally, add some more scopes to the cache name. Without this change, a second attempt to create a cache in the same action run (e.g. because one step installs some packages, then another step installs some more) will fail because of the cache name collision. Also update the README and tests, although the tests are only getting a very quick update for this function, as I'm going to do a much more comprehensive test update separately. --- .github/workflows/test.yml | 4 ++-- README.md | 26 ++++++++++++++++++++++---- action.yml | 22 ++++++++++++---------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e3f53f6..ce5470a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -241,7 +241,7 @@ jobs: uses: ./ with: packages: bash-completion - package-cache-key: testing-cache + cache: enabled add-to-path: false - name: Delete Cygwin installation and cache run: | @@ -250,7 +250,7 @@ jobs: - name: Reinstall Cygwin only uses: ./ with: - package-cache-key: testing-cache + cache: enabled add-to-path: false - name: Find bash-completion in the package cache shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0} diff --git a/README.md b/README.md index 2df1308..e8582b0 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Parameters | site | http://mirrors.kernel.org/sourceware/cygwin/ | Mirror site to install from | check-sig | true | Whether to check the setup.ini signature | add-to-path | true | Whether to add Cygwin's `/bin` directory to the system `PATH` -| package-cache-key | '' | The key to use for caching downloaded packages. +| cache | disabled | Whether to cache the package downloads Line endings ------------ @@ -91,9 +91,8 @@ Caching If you're likely to do regular builds, you might want to store the packages locally rather than needing to download them from the Cygwin mirrors on every -build. Set `package-cache-key` to some string (e.g. `cygwin-package-cache`), -and the action will use [GitHub's dependency caching][0] to store downloaded -package files between runs. +build. Set `cache` to `enabled` and the action will use [GitHub's dependency +caching][0] to store downloaded package files between runs. [0]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows @@ -102,6 +101,25 @@ expense of taking slightly longer before and after the installation to check and potentially update the cache. The installer will still check for updated packages, and will download new packages if the cached ones are out of date +In certain circumstances you might want to ignore any existing caches but still +store a new one, or restore a cache but not write one. Do this by setting +`cache` to `saveonly` or `restoreonly` as appropriate. This is particularly +useful when calling the action multiple times in the same run, where you +probably want to restore the cache the first time the action is called, then +save it the last time it is called. + +You should make sure to clear these caches every so often. This action, like +the underlying Cygwin installer, doesn't remove old package files from its +download directory, so if you don't clear the caches occasionally (and you run +builds often enough that GitHub doesn't do it for you automatically) you'll +find the caches keep getting larger as they gain more and more outdated and +unused packages. Either [delete them manually][1], [use a separate action or +API call][2], or do occasional runs with `saveonly` to create a fresher small +cache. + +[1]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#deleting-cache-entries +[2]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#deleting-cache-entries + Mirrors and signatures ---------------------- diff --git a/action.yml b/action.yml index 0d1d231..95bd886 100644 --- a/action.yml +++ b/action.yml @@ -26,10 +26,10 @@ inputs: description: Should Cygwin's bin directory be added to the system PATH? required: false default: true - package-cache-key: - description: Key prefix to use for package caches + cache: + description: Cache package downloads for speed required: false - default: '' + default: disabled runs: using: "composite" @@ -40,15 +40,17 @@ runs: run: Write-Output "GIT_BASH_PATH=$($(Get-Command git).Source)" >> $Env:GITHUB_ENV - uses: actions/cache/restore@v3 - if: inputs.package-cache-key != '' + if: inputs.cache == 'enabled' || inputs.cache == 'restoreonly' with: - key: ${{ inputs.package-cache-key }}-${{ github.run_id }}-${{ github.run_attempt }} + key: cygwin-install-action-packages-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}-${{ github.action }} path: C:\cygwin-packages restore-keys: - ${{ inputs.package-cache-key }}-${{ github.run_id }}- - ${{ inputs.package-cache-key }}- + cygwin-install-action-packages-${{ github.run_id }}-${{ github.job }}-${{ github.run_attempt }}- + cygwin-install-action-packages-${{ github.run_id }}-${{ github.job }}- + cygwin-install-action-packages-${{ github.run_id }}- + cygwin-install-action-packages- - - if: inputs.package-cache-key != '' + - if: inputs.cache == 'enabled' || inputs.cache == 'restoreonly' working-directory: C:\ shell: ${{ env.GIT_BASH_PATH }} --noprofile --norc -eo pipefail {0} run: | @@ -110,7 +112,7 @@ runs: & C:\setup.exe $args | Out-Default shell: powershell - - if: inputs.package-cache-key != '' + - if: inputs.cache == 'enabled' || inputs.cache == 'saveonly' id: refresh-cache working-directory: C:\ shell: ${{ env.GIT_BASH_PATH }} --noprofile --norc -eo pipefail {0} @@ -127,7 +129,7 @@ runs: - if: steps.refresh-cache.outputs.update_package_cache != '' uses: actions/cache/save@v3 with: - key: ${{ inputs.package-cache-key }}-${{ github.run_id }}-${{ github.run_attempt }} + key: cygwin-install-action-packages-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}-${{ github.action }} path: C:\cygwin-packages - if: ${{ inputs.add-to-path == 'true' }}