chore: fix CI cache cleanup step failing on wrong branch #22
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cache Yarn dependencies | |
on: | |
push: | |
branches: | |
- develop | |
pull_request: | |
paths: | |
- '.github/workflows/caching.yml' | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: false | |
jobs: | |
search: | |
name: Search for existing cache | |
runs-on: ubuntu-latest | |
outputs: | |
cache-hit: ${{ steps.cache.outputs.cache-hit }} | |
cache-key: ${{ steps.key.outputs.cache-key }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Setup NodeJS | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Generate cache key | |
id: key | |
run: | | |
echo "cache-key=${{ hashFiles('**/yarn.lock') }}-${{ runner.os }}" >> $GITHUB_OUTPUT | |
- name: Get Yarn cache directory path | |
id: yarn-cache-dir-path | |
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT | |
- name: Lookup cache | |
id: cache | |
uses: actions/cache/restore@v3 | |
with: | |
path: | | |
node_modules | |
${{ steps.yarn-cache-dir-path.outputs.dir }} | |
key: ${{ steps.key.outputs.cache-key }} | |
lookup-only: true | |
cache: | |
name: Cache | |
needs: search | |
runs-on: ubuntu-latest | |
if: ${{ !needs.search.outputs.cache-hit }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Setup NodeJS | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Install dependencies | |
shell: bash | |
run: yarn install --immutable --frozen-lockfile | |
- name: Get yarn cache directory path | |
id: yarn-cache-dir-path | |
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT | |
- name: Save cache | |
id: cache | |
uses: actions/cache/save@v3 | |
with: | |
path: | | |
node_modules | |
${{ steps.yarn-cache-dir-path.outputs.dir }} | |
key: ${{ needs.search.outputs.cache-key }} | |
cleanup: | |
name: Cleanup old caches | |
needs: search | |
runs-on: ubuntu-latest | |
if: ${{ !needs.search.outputs.cache-hit }} | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
permissions: | |
# `actions:write` permission is required to delete caches | |
# See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id | |
actions: write | |
contents: read | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Cleanup | |
run: | | |
gh extension install actions/gh-actions-cache | |
REPO=${{ github.repository }} | |
echo "Fetching list of cache keys..." | |
cacheKeysForPR=$(gh actions-cache list -R $REPO --branch develop | cut -f 1) | |
echo "Deleting caches..." | |
for cacheKey in $cacheKeysForPR | |
do | |
# Skip deletion if cache key matches the specified output | |
if [ "$cacheKey" = "${{ needs.search.outputs.cache-key }}" ]; then | |
echo "Skipping deletion for cache key: $cacheKey" | |
continue | |
fi | |
echo "Deleting cache: $cacheKey" | |
gh actions-cache delete $cacheKey -R $REPO --branch develop --confirm | |
done | |
echo "Done" |