Make cache keys unique

This commit is contained in:
ted-cdw 2023-02-22 09:03:46 -05:00
parent 2aaf0cc5ca
commit 74fcd115ee

View file

@ -35,14 +35,13 @@ jobs:
# Required permission to push images with the built-in GITHUB_TOKEN # Required permission to push images with the built-in GITHUB_TOKEN
packages: write packages: write
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
CACHE_KEY: flaskr-buildx-${{ github.ref_name }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Job vars and prep - name: Job vars
# Use bash string manipulation to create vars # Use bash string manipulation to create vars
# Note runner context isn't available in job.env, so use it here # Note runner context isn't available in job.env, so use it here
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#context-availability # https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#context-availability
shell: bash
run: | run: |
echo "SHA_SHORT=${GITHUB_SHA:0:10}" >> ${GITHUB_ENV} echo "SHA_SHORT=${GITHUB_SHA:0:10}" >> ${GITHUB_ENV}
echo "REPO_LOWER_CASE=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV} echo "REPO_LOWER_CASE=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}
@ -56,9 +55,9 @@ jobs:
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
path: ${{ env.CACHE_DIR }} path: ${{ env.CACHE_DIR }}
key: ${{ env.CACHE_KEY }} key: buildx-flaskr-${{ github.sha }}
restore-keys: | restore-keys: |
flaskr-buildx-main buildx-flaskr-
- name: Login to GitHub Container Registry - name: Login to GitHub Container Registry
uses: docker/login-action@v2 uses: docker/login-action@v2
with: with:
@ -73,16 +72,12 @@ jobs:
tags: "ghcr.io/${{ env.REPO_LOWER_CASE }}:${{ env.SHA_SHORT }}" tags: "ghcr.io/${{ env.REPO_LOWER_CASE }}:${{ env.SHA_SHORT }}"
cache-from: type=local,src=${{ env.CACHE_DIR }} cache-from: type=local,src=${{ env.CACHE_DIR }}
cache-to: type=local,dest=${{ env.NEW_CACHE_DIR }} cache-to: type=local,dest=${{ env.NEW_CACHE_DIR }}
# Only save the latest Docker build layers to the cache, otherwise # Only save the latest Docker build layers to new cache key, otherwise
# the cache will keep growing indefinitely, past the 10GB max # new cache keys will keep getting bigger indefinitely
# https://github.com/docker/build-push-action/issues/252 # https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896 # https://github.com/moby/buildkit/issues/1896
# - name: Only persist the new cache layers # Thanks https://evilmartians.com/chronicles/build-images-on-github-actions-with-docker-layer-caching
# run: |
# rm -rf ${CACHE_DIR}
# mv ${NEW_CACHE_DIR} ${CACHE_DIR}
- name: Only persist the new cache layers - name: Only persist the new cache layers
uses: actions/cache/save@v3 run: |
with: rm -rf ${CACHE_DIR}
path: ${{ env.NEW_CACHE_DIR }} mv ${NEW_CACHE_DIR} ${CACHE_DIR}
key: ${{ env.CACHE_KEY }}