From e4847a626c72e7770c132ad805a706b12eda0f9e Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Fri, 9 Aug 2024 15:47:38 -0700 Subject: [PATCH] Add support for caching, disabled by default --- .github/actions/docker/action.yml | 47 +++++++++++++++++++++++++++---- .github/workflows/ci-build.yml | 6 ++++ .github/workflows/ci.yml | 6 ++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/.github/actions/docker/action.yml b/.github/actions/docker/action.yml index 2665fb7e..30f8a9e4 100644 --- a/.github/actions/docker/action.yml +++ b/.github/actions/docker/action.yml @@ -71,6 +71,10 @@ inputs: default: 'type=gha,mode=max' description: 'Cache to.' required: false + ENABLE_CACHE: + default: 'false' + description: 'Whether to use cache when building the image' + required: false outputs: IMAGE_TAG: description: "Export image tag" @@ -126,15 +130,31 @@ runs: run: echo BUILD_ARGS=${{inputs.BUILD_ARGS}} >> $GITHUB_ENV shell: bash - - name: Build and export to Docker + - name: Build and export to Docker without cache uses: docker/build-push-action@v6 - if: inputs.TAG_ONLY == 'false' + if: inputs.TAG_ONLY == 'false' && inputs.ENABLE_CACHE == 'false' + with: + load: true + tags: | + ${{ steps.meta.outputs.tags }} + no-cache: true + file: ${{ inputs.FILE }} + context: ${{ inputs.CONTEXT }} + # cannot use multiple platforms with `load`, build a single arch image for validation purposes in CI + platforms: linux/amd64 + build-args: ${{ env.BUILD_ARGS }} + secrets: ${{ env.DOCKER_SECRETS }} + + - name: Build and export to Docker with cache + uses: docker/build-push-action@v6 + if: inputs.TAG_ONLY == 'false' && inputs.ENABLE_CACHE == 'true' with: load: true tags: | ${{ steps.meta.outputs.tags }} - # cache-from: type=gha - # cache-to: type=gha,mode=max + cache-from: type=gha + cache-to: type=gha,mode=max + no-cache: false file: ${{ inputs.FILE }} context: ${{ inputs.CONTEXT }} # cannot use multiple platforms with `load`, build a single arch image for validation purposes in CI @@ -155,8 +175,8 @@ runs: docker logs test ${{ inputs.CONTAINER_TEST_COMMAND }} - - name: Build and push - if: inputs.PUSH == 'true' # && inputs.TAG_ONY == 'false' + - name: Build and push with cache + if: inputs.PUSH == 'true' && inputs.ENABLE_CACHE == 'true' uses: docker/build-push-action@v6 with: push: true @@ -164,7 +184,22 @@ runs: ${{ steps.meta.outputs.tags }} cache-from: ${{ inputs.CACHE_FROM }} cache-to: ${{ inputs.CACHE_TO }}} + no-cache: false + file: ${{ inputs.FILE }} + context: ${{ inputs.CONTEXT }} + platforms: ${{ inputs.PLATFORMS }} + build-args: ${{ env.BUILD_ARGS }} + secrets: ${{ env.DOCKER_SECRETS }} + + - name: Build and push without cache + if: inputs.PUSH == 'true' && inputs.ENABLE_CACHE == 'false' + uses: docker/build-push-action@v6 + with: + push: true + tags: | + ${{ steps.meta.outputs.tags }} file: ${{ inputs.FILE }} + no-cache: true context: ${{ inputs.CONTEXT }} platforms: ${{ inputs.PLATFORMS }} build-args: ${{ env.BUILD_ARGS }} diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 27fcfb15..ddcfb743 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -116,6 +116,11 @@ on: default: 'type=gha,mode=max' description: 'Cache to.' required: false + ENABLE_DOCKER_CACHE: + type: boolean + default: false + description: 'Whether to use cache when building the image' + required: false ## Vault Secrets VAULT_SECRETS: type: string @@ -282,6 +287,7 @@ jobs: OUTPUT_TAG_INDEX: ${{ inputs.OUTPUT_TAG_INDEX }} CACHE_FROM: ${{ inputs.CACHE_FROM }} CACHE_TO: ${{ inputs.CACHE_TO }} + ENABLE_CACHE: ${{ inputs.ENABLE_DOCKER_CACHE }} env: GITHUB_TOKEN: ${{ github.token }} DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eec76798..630c6b6c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,6 +151,11 @@ on: default: . description: Whenever the WF requires a different path than . for the revision file required: false + ENABLE_DOCKER_CACHE: + type: boolean + default: false + description: 'Whether to use cache when building the image' + required: false ## Vault Secrets VAULT_SECRETS: type: string @@ -269,6 +274,7 @@ jobs: VAULT_SECRETS: ${{ inputs.VAULT_SECRETS }} TELEPORT_APP: ${{ inputs.TELEPORT_APP }} TELEPORT_PROXY_URL: ${{ inputs.TELEPORT_PROXY_URL }} + ENABLE_DOCKER_CACHE: ${{ inputs.ENABLE_DOCKER_CACHE }} secrets: GH_BOT_DEPLOY_KEY: ${{ secrets.GH_BOT_DEPLOY_KEY }} DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}