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

Enable custom caching with Github Actions workflow #85

Closed dambem closed 1 year ago

dambem commented 1 year ago

Issue

When using setup-sam@v2 alongside actions/cache@v3.2.4', despite the.aws-sam/cache` directory succefully being stored by the github cache, the following response is always given:

Cache is invalid, running build and copying resources for following functions

This appears to have something to do with the way the cache is being stored, leading to the cache being unable to be accessed.

As a recreateable example, the following action showcases a succesful cache store of .aws-sam/cache with an invalid cache hit.

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

      - name: Cache SAM dependencies
        id: cache
        uses: actions/cache@v3.2.4
        with:
          path: |
            .aws-sam/cache
          key: ${{ runner.os }}-SAM_BUILD-${{hashFiles('**/requirements.txt')}}

      - name: Build resources
        run: sam build --template ${SAM_TEMPLATE} --parallel --use-container --cached  --beta-features

This has also been attempted using the cache created by using use-installer and version, with a cache invalid response returned.

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

      - name: Cache SAM dependencies
        id: cache
        uses: actions/cache@v3.2.4
        with:
          path: |
            /opt/hostedtoolcache/sam/1.95.0/x64
          key: ${{ runner.os }}-AWSbuildV2-${{hashFiles('**/requirements.txt')}}

      - name: Build resources
        run: sam build --template ${SAM_TEMPLATE} --parallel --use-container --cached  --beta-features

Proposal

I'd like to request the ability to cache using the sam build --cached option, in the same capacity as other dependency caching found Cahing Dependencies to Speed Up Workflows. I'm aware there is SAM CLI caching as found in #73, however, this appears to not be supported by the github runner, and only works for self hosted runners.

I'm unsure if this is a bug, or if it's intended that caching is only supported via self-hosted runners, so please let me know any further logs that may be useful for debugging this issue.

Please let me know if i'm using the setup-sam incorrectly to cache, or if there's a different recommended method for doing so.

GavinZZ commented 1 year ago

Hi @dambem, I did some investigation on your issue, and I believe the root cause is that this is a bug in aws-sam-cli. In your workflow file, you're using path: .aws-sam/cache.

- name: Cache SAM dependencies
        id: cache
        uses: actions/cache@v3.2.4
        with:
          path: |
            .aws-sam/cache 
          key: ${{ runner.os }}-SAM_BUILD-${{hashFiles('**/requirements.txt')}}

In my testing, I noticed that the cache folder is empty and that's why when you load the cache, it's saying it's invalid. I can confirm that this happens in both GitHub Actions and locally.


Workaround:

The temporary workaround for you to use cache is to use the entire .aws-sam or .aws-sam/deps folder. I tested it and it worked in github action, see below screenshot.

Screenshot 2023-10-24 at 2 18 38 PM
GavinZZ commented 1 year ago

Going to cut a ticket to aws-sam-cli team to look into why .aws-sam/cache folder is empty. According to SAM CLI documentation, ./aws-sam/cache is the default cache directory and it is where the cache artifacts are stored.

Ticket cut: https://github.com/aws/aws-sam-cli/issues/6144

GavinZZ commented 1 year ago

I'm going to mark this issue as closed and monitor the aws-sam-cli ticket to learn more on why the cache folder is empty. Feel free to create an new issue or re-open this issue if you have any additional problems.

dambem commented 1 year ago

Just tested this morning and can confirm it works, thank you for looking into it!