Skip to content

Commit fba8bb6

Browse files
authored
Add support for docker build caching, disabled by default (#313)
1 parent e57fb5c commit fba8bb6

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

Diff for: .github/actions/docker/action.yml

+42-15
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ inputs:
6363
default: 'false'
6464
description: 'Generate tag only.'
6565
required: false
66-
CACHE_FROM:
67-
default: 'type=gha'
68-
description: 'Cache from.'
69-
required: false
70-
CACHE_TO:
71-
default: 'type=gha,mode=max'
72-
description: 'Cache to.'
66+
ENABLE_CACHE:
67+
default: 'false'
68+
description: 'Whether to use cache when building the image'
7369
required: false
7470
outputs:
7571
IMAGE_TAG:
@@ -126,15 +122,31 @@ runs:
126122
run: echo BUILD_ARGS=${{inputs.BUILD_ARGS}} >> $GITHUB_ENV
127123
shell: bash
128124

129-
- name: Build and export to Docker
125+
- name: Build and export to Docker without cache
130126
uses: docker/build-push-action@v6
131-
if: inputs.TAG_ONLY == 'false'
127+
if: inputs.ENABLE_CACHE == 'false'
128+
with:
129+
load: true
130+
tags: |
131+
${{ steps.meta.outputs.tags }}
132+
no-cache: true
133+
file: ${{ inputs.FILE }}
134+
context: ${{ inputs.CONTEXT }}
135+
# cannot use multiple platforms with `load`, build a single arch image for validation purposes in CI
136+
platforms: linux/amd64
137+
build-args: ${{ env.BUILD_ARGS }}
138+
secrets: ${{ env.DOCKER_SECRETS }}
139+
140+
- name: Build and export to Docker with cache
141+
uses: docker/build-push-action@v6
142+
if: inputs.ENABLE_CACHE == 'true'
132143
with:
133144
load: true
134145
tags: |
135146
${{ steps.meta.outputs.tags }}
136-
# cache-from: type=gha
137-
# cache-to: type=gha,mode=max
147+
cache-from: type=gha
148+
cache-to: type=gha,mode=max
149+
no-cache: false
138150
file: ${{ inputs.FILE }}
139151
context: ${{ inputs.CONTEXT }}
140152
# cannot use multiple platforms with `load`, build a single arch image for validation purposes in CI
@@ -155,16 +167,31 @@ runs:
155167
docker logs test
156168
${{ inputs.CONTAINER_TEST_COMMAND }}
157169
158-
- name: Build and push
159-
if: inputs.PUSH == 'true' # && inputs.TAG_ONY == 'false'
170+
- name: Build and push with cache
171+
if: inputs.PUSH == 'true' && inputs.ENABLE_CACHE == 'true'
172+
uses: docker/build-push-action@v6
173+
with:
174+
push: true
175+
tags: |
176+
${{ steps.meta.outputs.tags }}
177+
cache-from: type=gha
178+
cache-to: type=gha,mode=max
179+
no-cache: false
180+
file: ${{ inputs.FILE }}
181+
context: ${{ inputs.CONTEXT }}
182+
platforms: ${{ inputs.PLATFORMS }}
183+
build-args: ${{ env.BUILD_ARGS }}
184+
secrets: ${{ env.DOCKER_SECRETS }}
185+
186+
- name: Build and push without cache
187+
if: inputs.PUSH == 'true' && inputs.ENABLE_CACHE == 'false'
160188
uses: docker/build-push-action@v6
161189
with:
162190
push: true
163191
tags: |
164192
${{ steps.meta.outputs.tags }}
165-
cache-from: ${{ inputs.CACHE_FROM }}
166-
cache-to: ${{ inputs.CACHE_TO }}}
167193
file: ${{ inputs.FILE }}
194+
no-cache: true
168195
context: ${{ inputs.CONTEXT }}
169196
platforms: ${{ inputs.PLATFORMS }}
170197
build-args: ${{ env.BUILD_ARGS }}

Diff for: .github/workflows/ci-build.yml

+5-11
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,10 @@ on:
106106
type: string
107107
description: ref to pull and build default to github.ref
108108
required: false
109-
CACHE_FROM:
110-
type: string
111-
default: 'type=gha'
112-
description: 'Cache from.'
113-
required: false
114-
CACHE_TO:
115-
type: string
116-
default: 'type=gha,mode=max'
117-
description: 'Cache to.'
109+
ENABLE_DOCKER_BUILD_CACHE:
110+
type: boolean
111+
default: false
112+
description: 'Whether to use cache when building the image'
118113
required: false
119114
## Vault Secrets
120115
VAULT_SECRETS:
@@ -280,8 +275,7 @@ jobs:
280275
CONTAINER_TEST_RUN_OPTIONS: ${{ inputs.CONTAINER_TEST_RUN_OPTIONS }}
281276
CONTAINER_TEST_COMMAND: ${{ inputs.CONTAINER_TEST_COMMAND }}
282277
OUTPUT_TAG_INDEX: ${{ inputs.OUTPUT_TAG_INDEX }}
283-
CACHE_FROM: ${{ inputs.CACHE_FROM }}
284-
CACHE_TO: ${{ inputs.CACHE_TO }}
278+
ENABLE_CACHE: ${{ inputs.ENABLE_DOCKER_BUILD_CACHE }}
285279
env:
286280
GITHUB_TOKEN: ${{ github.token }}
287281
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}

Diff for: .github/workflows/ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ on:
151151
default: .
152152
description: Whenever the WF requires a different path than . for the revision file
153153
required: false
154+
ENABLE_DOCKER_BUILD_CACHE:
155+
type: boolean
156+
default: false
157+
description: 'Whether to use cache when building the image'
158+
required: false
154159
## Vault Secrets
155160
VAULT_SECRETS:
156161
type: string
@@ -269,6 +274,7 @@ jobs:
269274
VAULT_SECRETS: ${{ inputs.VAULT_SECRETS }}
270275
TELEPORT_APP: ${{ inputs.TELEPORT_APP }}
271276
TELEPORT_PROXY_URL: ${{ inputs.TELEPORT_PROXY_URL }}
277+
ENABLE_DOCKER_BUILD_CACHE: ${{ inputs.ENABLE_DOCKER_BUILD_CACHE }}
272278
secrets:
273279
GH_BOT_DEPLOY_KEY: ${{ secrets.GH_BOT_DEPLOY_KEY }}
274280
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}

0 commit comments

Comments
 (0)