Skip to content

Commit

Permalink
Allow save-only/restore-only cache runs
Browse files Browse the repository at this point in the history
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 cygwin#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.
  • Loading branch information
me-and committed Jan 11, 2023
1 parent 87d0741 commit 0622ea1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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}
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down Expand Up @@ -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

Expand All @@ -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
----------------------

Expand Down
22 changes: 12 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,15 +41,17 @@ runs:
run: Write-Output "GIT_BASH_PATH=$($(Get-Command bash).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: |
Expand Down Expand Up @@ -111,7 +113,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}
Expand All @@ -128,7 +130,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' }}
Expand Down

0 comments on commit 0622ea1

Please sign in to comment.