aws-actions / setup-sam

Action to set up AWS SAM CLI and add it to the PATH
Apache License 2.0
151 stars 23 forks source link

Support caching with `use-installer` when `version` not specified #76

Closed hoffa closed 12 months ago

hoffa commented 1 year ago

Context

With https://github.com/aws-actions/setup-sam/pull/73 merged, SAM CLI is cached when use-installer is enabled and version is specified.

For example:

- uses: aws-actions/setup-sam@v2
  with:
    version: 1.76.0
    use-installer: true

This is beneficial for self-hosted runners that don't get a fresh machine at every workflow run.

Issue

When version is not specified (i.e. it uses the latest available version), it currently does not cache:

https://github.com/aws-actions/setup-sam/blob/b42eb7a54dac4039080975e32860b1b30935c9af/lib/setup.js#L137-L144

https://github.com/aws-actions/setup-sam/blob/b42eb7a54dac4039080975e32860b1b30935c9af/lib/setup.js#L154-L159

Proposal

Ideally, it would do something like this when version is not specified:

  1. Get the version number of the latest SAM CLI release.
  2. If the version is in the cache, return it.
  3. If not, set up as usual and add the version to the cache.

Challenges

  1. How to reliably get the latest version number, without downloading the full release? If we use the version field in PyPI (e.g. curl https://pypi.org/pypi/aws-sam-cli/json | jq -r .info.version), there's no guarantee the GitHub release for that version will always exist at the same time (they're separate assets, so there can be a brief delay during releases). If we use the GitHub release directly, there's no robust way of getting the version (but could parse the title or tag).

Alternatives

  1. Something like check-latest from actions/setup-go and actions/setup-python? GitHub-hosted runners already include SAM CLI (see e.g. https://github.com/actions/runner-images/blob/main/images/linux/Ubuntu2204-Readme.md), so perhaps by default if it finds SAM CLI is installed, it could immediately return? Might require a new major version, as it no longer installs latest version by default. Might not be a reliable way to ensure pre-installed version is functioning well. The way check-latest works seems to be a little different; it's because the version can be a range, and so instead of picking whatever fits in the range from the cache, it ensures the latest version is used.
GavinZZ commented 12 months ago

Noticed that we easily parse the latest release tag using curl -s https://api.github.com/repos/aws/aws-sam-cli/releases | jq -r '.[0].tag_name'

GavinZZ commented 12 months ago

We've now support caching with use-installer even when version is not specified. Going to mark this issue as completed. Feel free to open a new issue or re-open this one if there's additional questions.