diff --git a/.github/workflows/flaskr-ci.yaml b/.github/workflows/flaskr-ci.yaml index a4325078..752124c6 100644 --- a/.github/workflows/flaskr-ci.yaml +++ b/.github/workflows/flaskr-ci.yaml @@ -35,14 +35,13 @@ jobs: # Required permission to push images with the built-in GITHUB_TOKEN packages: write runs-on: ubuntu-latest - env: - CACHE_KEY: flaskr-buildx-${{ github.ref_name }} steps: - uses: actions/checkout@v3 - - name: Job vars and prep + - name: Job vars # Use bash string manipulation to create vars # 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 + shell: bash run: | echo "SHA_SHORT=${GITHUB_SHA:0:10}" >> ${GITHUB_ENV} echo "REPO_LOWER_CASE=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV} @@ -56,9 +55,9 @@ jobs: uses: actions/cache@v2 with: path: ${{ env.CACHE_DIR }} - key: ${{ env.CACHE_KEY }} + key: buildx-flaskr-${{ github.sha }} restore-keys: | - flaskr-buildx-main + buildx-flaskr- - name: Login to GitHub Container Registry uses: docker/login-action@v2 with: @@ -73,16 +72,12 @@ jobs: tags: "ghcr.io/${{ env.REPO_LOWER_CASE }}:${{ env.SHA_SHORT }}" cache-from: type=local,src=${{ env.CACHE_DIR }} cache-to: type=local,dest=${{ env.NEW_CACHE_DIR }} - # Only save the latest Docker build layers to the cache, otherwise - # the cache will keep growing indefinitely, past the 10GB max + # Only save the latest Docker build layers to new cache key, otherwise + # new cache keys will keep getting bigger indefinitely # https://github.com/docker/build-push-action/issues/252 # https://github.com/moby/buildkit/issues/1896 - # - name: Only persist the new cache layers - # run: | - # rm -rf ${CACHE_DIR} - # mv ${NEW_CACHE_DIR} ${CACHE_DIR} + # Thanks https://evilmartians.com/chronicles/build-images-on-github-actions-with-docker-layer-caching - name: Only persist the new cache layers - uses: actions/cache/save@v3 - with: - path: ${{ env.NEW_CACHE_DIR }} - key: ${{ env.CACHE_KEY }} \ No newline at end of file + run: | + rm -rf ${CACHE_DIR} + mv ${NEW_CACHE_DIR} ${CACHE_DIR}