diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..42a6cc4 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,47 @@ +name: build + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + strategy: + matrix: + os: [ubuntu-latest] + fail-fast: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: "20.x" + - name: Install Yarn + run: npm install -g yarn + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + - uses: actions/cache@v3 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + - name: Install Deps + run: yarn install --pure-lockfile + # - name: Build & Test + # run: yarn test + - name: Ensure dist/ folder is up-to-date + run: | + yarn build + if [ "$(git diff --ignore-space-at-eol | wc -l)" -gt "0" ]; then + echo "Detected uncommitted changes after build. See status below:" + git diff + exit 1 + fi + diff --git a/.github/workflows/lookup-only.yaml b/.github/workflows/lookup-only.yaml new file mode 100644 index 0000000..9990686 --- /dev/null +++ b/.github/workflows/lookup-only.yaml @@ -0,0 +1,76 @@ +name: test-lookup-only + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test-save: + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + fail-fast: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Generate files in working directory + shell: bash + run: src/create-cache-files.sh ${{ runner.os }} test-cache + - name: Generate files outside working directory + shell: bash + run: src/create-cache-files.sh ${{ runner.os }} ~/test-cache + - name: Save cache + uses: ./ + env: + AWS_ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.SECRET_KEY }} + AWS_REGION: "us-east-1" + with: + endpoint: ${{ secrets.ENDPOINT }} + bucket: ${{ secrets.BUCKET }} + use-fallback: false + key: test-${{ runner.os }}-${{ github.run_id }} + path: | + test-cache + ~/test-cache + + test-lookup-only: + needs: test-save + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + fail-fast: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Lookup cache + id: lookup + uses: ./ + env: + AWS_ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.SECRET_KEY }} + # AWS_SESSION_TOKEN: "xxx" + AWS_REGION: "us-east-1" + with: + endpoint: ${{ secrets.ENDPOINT }} + bucket: ${{ secrets.BUCKET }} + key: test-${{ runner.os }}-${{ github.run_id }} + lookup-only: true + path: | + test-cache + ~/test-cache + - name: verify cache hit + env: + CACHE_HIT: ${{ steps.lookup.outputs.cache-hit }} + CACHE_SIZE: ${{ steps.lookup.outputs.cache-size }} + shell: bash + run: | + echo "CACHE_HIT $CACHE_HIT" + echo "CACHE_SIZE $CACHE_SIZE" + ls -R ~/test-cache || true + ls -R test-cache || true + src/create-cache-files.sh ${{ runner.os }} test-cache check_not_exist diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7aee9e1..9967513 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,44 +7,6 @@ on: branches: [main] jobs: - build: - strategy: - matrix: - os: [ubuntu-latest] - fail-fast: false - runs-on: ${{ matrix.os }} - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: "20.x" - - name: Install Yarn - run: npm install -g yarn - - name: Get yarn cache directory path - id: yarn-cache-dir-path - run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v3 - id: yarn-cache - with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-yarn- - - name: Install Deps - run: yarn install --pure-lockfile - # - name: Build & Test - # run: yarn test - - name: Ensure dist/ folder is up-to-date - run: | - yarn build - if [ "$(git diff --ignore-space-at-eol | wc -l)" -gt "0" ]; then - echo "Detected uncommitted changes after build. See status below:" - git diff - exit 1 - fi - test-save: strategy: matrix: @@ -120,7 +82,7 @@ jobs: secretKey: ${{ secrets.SECRET_KEY }} bucket: ${{ secrets.BUCKET }} use-fallback: false - key: test-${{ runner.os }}-${{ github.run_id }}-${{ github.sha }} + key: test-${{ runner.os }}-${{ github.run_id }} path: | test-cache ~/test-cache diff --git a/README.md b/README.md index 6e2a1ad..9a351b2 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,31 @@ To restore from the cache only: node_modules ``` +To check if cache hits and size is not zero: + +```yaml + - name: Check cache + id: cache + uses: tespkg/actions-cache/check@v1 + with: + accessKey: "Q3AM3UQ867SPQQA43P2F" # required + secretKey: "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" # required + bucket: actions-cache # required + lookup-only: true + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + path: | + node_modules + + - name: verify cache hit + env: + CACHE_HIT: ${{ steps.cache.outputs.cache-hit }} + CACHE_SIZE: ${{ steps.cache.outputs.cache-size }} + run: | + echo "CACHE_HIT $CACHE_HIT" + echo "CACHE_SIZE $CACHE_SIZE" +``` + + ## Restore keys `restore-keys` works similar to how github's `@actions/cache@v2` works: It search each item in `restore-keys` diff --git a/src/verify-cache-files.sh b/src/verify-cache-files.sh index 8e9a06c..532e6dd 100755 --- a/src/verify-cache-files.sh +++ b/src/verify-cache-files.sh @@ -20,7 +20,20 @@ if [ -z "$GITHUB_RUN_ID" ]; then fi # Verify file exists +check_not_exists="$3" file="$path/test-file.txt" + +if [ -n "$check_not_exists" ]; then + echo "CACHE_HIT $CACHE_HIT" + echo "CACHE_SIZE $CACHE_SIZE" + echo "Checking for $file to not exist" + if [ -e $file ]; then + echo "File exists when it should not" + exit 1 + fi + exit 0 +fi + echo "Checking for $file" if [ ! -e $file ]; then echo "File does not exist"