npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itchyny/s3-cache-action

v1.1.2

Published

Cache files to Amazon S3

Downloads

10

Readme

s3-cache-action

This action is a minimal implementation of a cache action that caches files to Amazon S3. This action works similarly to actions/cache, but uses Amazon S3 as the backend.

Usage

The action can be used in the same way as actions/cache, but requires input parameters for S3 bucket name and AWS credentials. Firstly, learn the basic usage of actions/cache in GitHub Docs: Using the cache action. The input parameters path, key, restore-keys, lookup-only, fail-on-cache-miss, and the output parameter cache-hit are compatible with actions/cache. For examples of caching configurations in each language, see actions/cache: Implementation Examples.

- uses: itchyny/s3-cache-action@v1
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      npm-${{ runner.os }}-
    bucket-name: ${{ vars.S3_CACHE_BUCKET_NAME }}
    aws-region: ${{ vars.S3_CACHE_AWS_REGION }}
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Attach s3:GetObject, s3:PutObject on the bucket objects, and s3:ListBucket on the bucket to the IAM role of the AWS credentials.

You can also use aws-actions/configure-aws-credentials to configure the AWS credentials. However, note that the credentials are stored in the environment variables, and can be accessed in subsequent steps.

- uses: aws-actions/configure-aws-credentials@v4
  with:
    aws-region: ${{ vars.S3_CACHE_AWS_REGION }}
    role-to-assume: ${{ vars.S3_CACHE_ASSUME_ROLE_ARN }}
- uses: itchyny/s3-cache-action@v1
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      npm-${{ runner.os }}-
    bucket-name: ${{ vars.S3_CACHE_BUCKET_NAME }}

Refer to action.yaml for the documentation of the inputs and outputs.

Differences from actions/cache

  • The action does not have cache scope based on branches, so it may restore caches from a sibling branch. You can include ${{ github.ref_name }} in key and default branch name in restore-keys to emulate the behavior.
  • The action restores caches using key by exact matching, while actions/cache restores by prefix matching. You can include the same key in restore-keys for prefix matching.
  • The action does not separate caches based on the operating system, especially for Windows. You can include ${{ runner.os }} in key and restore-keys.
  • The action uses only gzip compression to simplify implementation, while actions/cache uses Zstandard if possible. The action does not use any external commands, while actions/cache relies on tar, gzip, and zstd commands.

npm package

The core implementation of this action is available as an npm package: @itchyny/s3-cache-action.

npm install @itchyny/s3-cache-action
import * as s3 from '@aws-sdk/client-s3';
import * as cache from '@itchyny/s3-cache-action';

const bucketName: string = 'bucket-name';
const s3Client: s3.S3Client = new s3.S3Client({
  region: process.env.AWS_REGION!,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
    sessionToken: process.env.AWS_SESSION_TOKEN!,
  },
});

async function main() {
  const saved = await cache.saveCache(
    ['*.txt'], 'test-key', bucketName, s3Client,
  );
  if (!saved) {
    console.log('Cache already exists, skipped saving.');
  }

  const restoredKey = await cache.restoreCache(
    ['*.txt'], 'test-key', ['test-'], bucketName, s3Client,
  );
  if (restoredKey) {
    console.log(`Cache restored with key ${restoredKey}.`);
  }

  const foundKey = await cache.lookupCache(
    ['*.txt'], 'test-key', ['test-'], bucketName, s3Client,
  );
  if (foundKey) {
    console.log(`Cache found with key ${foundKey}.`);
  }
}

Bug Tracker

Report bug at Issues・itchyny/s3-cache-action - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.