Skip to content

Commit 1f9973c

Browse files
committed
build/bake: support configurable image layer compression
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 02ccc51 commit 1f9973c

7 files changed

Lines changed: 273 additions & 53 deletions

File tree

‎.github/workflows/.test-bake.yml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,3 +839,86 @@ jobs:
839839
await exec.exec('docker', ['rm', container]);
840840
fs.rmSync(directory, {recursive: true, force: true});
841841
}
842+
843+
bake-compression:
844+
strategy:
845+
matrix:
846+
include:
847+
- variant: default
848+
compression: ''
849+
level: ''
850+
force: false
851+
- variant: zstd
852+
compression: zstd
853+
level: '0'
854+
force: false
855+
- variant: zstd-force
856+
compression: zstd
857+
level: '9'
858+
force: true
859+
uses: ./.github/workflows/bake.yml
860+
permissions:
861+
contents: read
862+
id-token: write
863+
packages: write
864+
with:
865+
compression: ${{ matrix.compression }}
866+
compression-level: ${{ matrix.level }}
867+
force-compression: ${{ matrix.force }}
868+
output: image
869+
push: ${{ github.event_name != 'pull_request' }}
870+
context: test
871+
target: compression
872+
meta-images: ghcr.io/docker/github-builder-test
873+
meta-tags: |
874+
type=raw,value=bake-compression-${{ matrix.variant }}-${{ github.run_id }}-${{ github.run_attempt }}
875+
secrets:
876+
registry-auths: |
877+
- registry: ghcr.io
878+
username: ${{ github.actor }}
879+
password: ${{ secrets.GITHUB_TOKEN }}
880+
881+
bake-compression-check:
882+
runs-on: ubuntu-26.04
883+
if: ${{ github.event_name != 'pull_request' }}
884+
needs:
885+
- bake-compression
886+
strategy:
887+
matrix:
888+
include:
889+
- variant: default
890+
base: gzip
891+
added: gzip
892+
- variant: zstd
893+
base: gzip
894+
added: zstd
895+
- variant: zstd-force
896+
base: zstd
897+
added: zstd
898+
steps:
899+
-
900+
name: Check image layer compression
901+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
902+
env:
903+
INPUT_IMAGE: ghcr.io/docker/github-builder-test
904+
INPUT_TAG: bake-compression-${{ matrix.variant }}-${{ github.run_id }}-${{ github.run_attempt }}
905+
INPUT_BASE-COMPRESSION: ${{ matrix.base }}
906+
INPUT_ADDED-COMPRESSION: ${{ matrix.added }}
907+
with:
908+
script: |
909+
const assert = require('node:assert/strict');
910+
const image = core.getInput('image');
911+
const inspect = async ref => {
912+
const {stdout} = await exec.getExecOutput('docker', ['buildx', 'imagetools', 'inspect', '--raw', ref], {silent: true});
913+
return JSON.parse(stdout);
914+
};
915+
const index = await inspect(`${image}:${core.getInput('tag')}`);
916+
const manifests = index.manifests.filter(manifest => manifest.platform?.os === 'linux');
917+
assert.deepEqual(manifests.map(manifest => manifest.platform.architecture).sort(), ['amd64', 'arm64']);
918+
for (const {digest} of manifests) {
919+
const {layers} = await inspect(`${image}@${digest}`);
920+
assert.ok(layers.length >= 2, 'Expected base image layers and the fixture layer');
921+
const expected = layers.map((_, index) => core.getInput(index === layers.length - 1 ? 'added-compression' : 'base-compression'));
922+
const actual = layers.map(layer => layer.mediaType.split('+').pop());
923+
assert.deepEqual(actual, expected, `Unexpected layer compression for ${image}@${digest}`);
924+
}

‎.github/workflows/.test-build.yml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,86 @@ jobs:
848848
await exec.exec('docker', ['rm', container]);
849849
fs.rmSync(directory, {recursive: true, force: true});
850850
}
851+
852+
build-compression:
853+
strategy:
854+
matrix:
855+
include:
856+
- variant: default
857+
compression: ''
858+
level: ''
859+
force: false
860+
- variant: zstd
861+
compression: zstd
862+
level: '0'
863+
force: false
864+
- variant: zstd-force
865+
compression: zstd
866+
level: '9'
867+
force: true
868+
uses: ./.github/workflows/build.yml
869+
permissions:
870+
contents: read
871+
id-token: write
872+
packages: write
873+
with:
874+
compression: ${{ matrix.compression }}
875+
compression-level: ${{ matrix.level }}
876+
force-compression: ${{ matrix.force }}
877+
output: image
878+
push: ${{ github.event_name != 'pull_request' }}
879+
file: test/compression.Dockerfile
880+
platforms: linux/amd64,linux/arm64
881+
meta-images: ghcr.io/docker/github-builder-test
882+
meta-tags: |
883+
type=raw,value=build-compression-${{ matrix.variant }}-${{ github.run_id }}-${{ github.run_attempt }}
884+
secrets:
885+
registry-auths: |
886+
- registry: ghcr.io
887+
username: ${{ github.actor }}
888+
password: ${{ secrets.GITHUB_TOKEN }}
889+
890+
build-compression-check:
891+
runs-on: ubuntu-26.04
892+
if: ${{ github.event_name != 'pull_request' }}
893+
needs:
894+
- build-compression
895+
strategy:
896+
matrix:
897+
include:
898+
- variant: default
899+
base: gzip
900+
added: gzip
901+
- variant: zstd
902+
base: gzip
903+
added: zstd
904+
- variant: zstd-force
905+
base: zstd
906+
added: zstd
907+
steps:
908+
-
909+
name: Check image layer compression
910+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
911+
env:
912+
INPUT_IMAGE: ghcr.io/docker/github-builder-test
913+
INPUT_TAG: build-compression-${{ matrix.variant }}-${{ github.run_id }}-${{ github.run_attempt }}
914+
INPUT_BASE-COMPRESSION: ${{ matrix.base }}
915+
INPUT_ADDED-COMPRESSION: ${{ matrix.added }}
916+
with:
917+
script: |
918+
const assert = require('node:assert/strict');
919+
const image = core.getInput('image');
920+
const inspect = async ref => {
921+
const {stdout} = await exec.getExecOutput('docker', ['buildx', 'imagetools', 'inspect', '--raw', ref], {silent: true});
922+
return JSON.parse(stdout);
923+
};
924+
const index = await inspect(`${image}:${core.getInput('tag')}`);
925+
const manifests = index.manifests.filter(manifest => manifest.platform?.os === 'linux');
926+
assert.deepEqual(manifests.map(manifest => manifest.platform.architecture).sort(), ['amd64', 'arm64']);
927+
for (const {digest} of manifests) {
928+
const {layers} = await inspect(`${image}@${digest}`);
929+
assert.ok(layers.length >= 2, 'Expected base image layers and the fixture layer');
930+
const expected = layers.map((_, index) => core.getInput(index === layers.length - 1 ? 'added-compression' : 'base-compression'));
931+
const actual = layers.map(layer => layer.mediaType.split('+').pop());
932+
assert.deepEqual(actual, expected, `Unexpected layer compression for ${image}@${digest}`);
933+
}

‎.github/workflows/bake.yml‎

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ on:
5959
description: "Cache layers to export if cache enabled (min or max)"
6060
required: false
6161
default: 'min'
62+
compression:
63+
type: string
64+
description: "Image layer compression (uncompressed, gzip, estargz or zstd)"
65+
required: false
66+
compression-level:
67+
type: string
68+
description: "Image layer compression level (0-9 for gzip and estargz, 0-22 for zstd)"
69+
required: false
70+
force-compression:
71+
type: boolean
72+
description: "Recompress existing image layers using the selected compression type"
73+
required: false
74+
default: false
6275
context:
6376
type: string
6477
description: "Context to build from in the Git working tree"
@@ -225,6 +238,7 @@ jobs:
225238
outputs:
226239
sourceDateEpoch: ${{ steps.set.outputs.sourceDateEpoch }}
227240
sourceDateCreated: ${{ steps.set.outputs.sourceDateCreated }}
241+
buildOutput: ${{ steps.set.outputs.buildOutput }}
228242
includes: ${{ steps.set.outputs.includes }}
229243
metaImages: ${{ steps.set.outputs.metaImages }}
230244
sign: ${{ steps.set.outputs.sign }}
@@ -321,6 +335,10 @@ jobs:
321335
id: set
322336
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
323337
env:
338+
INPUT_LOCAL-EXPORT-DIR: ${{ env.LOCAL_EXPORT_DIR }}
339+
INPUT_COMPRESSION: ${{ inputs.compression }}
340+
INPUT_COMPRESSION-LEVEL: ${{ inputs.compression-level }}
341+
INPUT_FORCE-COMPRESSION: ${{ inputs.force-compression }}
324342
INPUT_SBOM-IMAGE: ${{ env.SBOM_IMAGE }}
325343
INPUT_MATRIX-SIZE-LIMIT: ${{ env.MATRIX_SIZE_LIMIT }}
326344
INPUT_META-IMAGES: ${{ inputs.meta-images }}
@@ -349,6 +367,7 @@ jobs:
349367
const { GitHub } = require('@docker/github-builder-runtime/lib/github/github');
350368
const { MetaImages } = require('@docker/github-builder-runtime/lib/github-builder/meta-images');
351369
const { SourceDateEpoch } = require('@docker/github-builder-runtime/lib/github-builder/source-date-epoch');
370+
const { BuildOutput } = require('@docker/github-builder-runtime/lib/github-builder/build-output');
352371
const { RunnerMapping } = require('@docker/github-builder-runtime/lib/github-builder/runner-mapping');
353372
const { BakeTargets } = require('@docker/github-builder-runtime/lib/github-builder/bake-targets');
354373
const { BakeVars } = require('@docker/github-builder-runtime/lib/github-builder/bake-vars');
@@ -373,6 +392,10 @@ jobs:
373392
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
374393
const inpSign = core.getInput('sign');
375394
const inpSourceDateEpoch = core.getInput('source-date-epoch');
395+
const inpCompression = core.getInput('compression');
396+
const inpCompressionLevel = core.getInput('compression-level');
397+
const inpForceCompression = core.getBooleanInput('force-compression');
398+
const inpLocalExportDir = core.getInput('local-export-dir');
376399
const inpTarget = core.getInput('target');
377400
const inpGitHubToken = core.getInput('github-token');
378401
const inpProxyNetwork = core.getBooleanInput('buildkit-proxy-network');
@@ -429,7 +452,29 @@ jobs:
429452
}).sort(), null, 2));
430453
});
431454
455+
if (inpCompressionLevel !== '' && (!/^\d+$/.test(inpCompressionLevel) || !Number.isSafeInteger(Number(inpCompressionLevel)))) {
456+
core.setFailed(`Invalid compression-level: ${inpCompressionLevel}`);
457+
return;
458+
}
459+
432460
const metaImages = MetaImages.resolve(inpMetaImages);
461+
let buildOutput = BuildOutput.resolve({
462+
output: inpOutput,
463+
images: metaImages,
464+
push: inpPush,
465+
localExportDir: inpLocalExportDir,
466+
compression: inpCompression || undefined,
467+
compressionLevel: inpCompressionLevel !== '' ? Number(inpCompressionLevel) : undefined,
468+
forceCompression: inpForceCompression || undefined
469+
});
470+
if (inpOutput === 'image' && inpSourceDateEpoch !== '') {
471+
buildOutput += ',rewrite-timestamp=true';
472+
}
473+
await core.group(`Set buildOutput output`, async () => {
474+
core.info(buildOutput);
475+
core.setOutput('buildOutput', buildOutput);
476+
});
477+
433478
await core.group(`Set metaImages output`, async () => {
434479
core.info(JSON.stringify(metaImages, null, 2));
435480
core.setOutput('metaImages', metaImages.join('\n'));
@@ -842,23 +887,20 @@ jobs:
842887
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
843888
env:
844889
INPUT_SOURCE-DATE-EPOCH: ${{ needs.prepare.outputs.sourceDateEpoch }}
890+
INPUT_BUILD-OUTPUT: ${{ needs.prepare.outputs.buildOutput }}
845891
INPUT_PLATFORM: ${{ matrix.platform }}
846892
INPUT_SBOM-IMAGE: ${{ env.SBOM_IMAGE }}
847-
INPUT_LOCAL-EXPORT-DIR: ${{ env.LOCAL_EXPORT_DIR }}
848893
INPUT_CACHE: ${{ inputs.cache }}
849894
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
850895
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
851896
INPUT_BUILD-SECRETS: ${{ secrets.build-secrets }}
852897
INPUT_TARGETS: ${{ needs.prepare.outputs.targets }}
853898
INPUT_CONTEXT: ${{ inputs.context }}
854899
INPUT_FILES: ${{ inputs.files }}
855-
INPUT_OUTPUT: ${{ inputs.output }}
856-
INPUT_PUSH: ${{ inputs.push }}
857900
INPUT_SBOM: ${{ inputs.sbom }}
858901
INPUT_SET: ${{ inputs.set }}
859902
INPUT_TARGET: ${{ inputs.target }}
860903
INPUT_VARS: ${{ inputs.vars }}
861-
INPUT_META-IMAGES: ${{ needs.prepare.outputs.metaImages }}
862904
INPUT_META-VERSION: ${{ steps.meta.outputs.version }}
863905
INPUT_META-TAGS: ${{ steps.meta.outputs.tags }}
864906
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
@@ -877,14 +919,12 @@ jobs:
877919
const { Util } = require('@docker/github-builder-runtime/lib/util');
878920
879921
const inpSourceDateEpoch = core.getInput('source-date-epoch');
880-
const rewriteTimestamp = inpSourceDateEpoch !== '' ? ',rewrite-timestamp=true' : '';
881922
882923
const inpPlatform = core.getInput('platform');
883924
const platformPairSuffix = inpPlatform ? `-${inpPlatform.replace(/\//g, '-')}` : '';
884925
core.setOutput('platform-pair-suffix', platformPairSuffix);
885926
886927
const inpSbomImage = core.getInput('sbom-image');
887-
const inpLocalExportDir = core.getInput('local-export-dir');
888928
889929
const inpCache = core.getBooleanInput('cache');
890930
const inpCacheScope = core.getInput('cache-scope');
@@ -893,15 +933,12 @@ jobs:
893933
const inpTargets = core.getInput('targets');
894934
const inpContext = core.getInput('context');
895935
const inpFiles = Util.getInputList('files');
896-
const inpOutput = core.getInput('output');
897-
const inpPush = core.getBooleanInput('push');
898936
const inpSbom = core.getBooleanInput('sbom');
899937
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
900938
const inpTarget = core.getInput('target');
901939
const inpVars = Util.getInputList('vars', {ignoreComma: true, quote: false});
902940
const inpBuildkitProxyNetwork = core.getBooleanInput('buildkit-proxy-network');
903941
904-
const inpMetaImages = core.getMultilineInput('meta-images');
905942
const inpMetaVersion = core.getInput('meta-version');
906943
const inpMetaTags = core.getMultilineInput('meta-tags');
907944
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
@@ -970,23 +1007,6 @@ jobs:
9701007
core.setOutput('files', bakeFiles.join(os.EOL));
9711008
});
9721009
973-
let outputOverride = '';
974-
switch (inpOutput) {
975-
case 'image':
976-
if (inpMetaImages.length == 0) {
977-
core.setFailed('meta-images is required when output is image');
978-
return;
979-
}
980-
outputOverride = `*.output=type=image,"name=${inpMetaImages.join(',')}",oci-artifact=true,push-by-digest=true,name-canonical=true,push=${inpPush}${rewriteTimestamp}`;
981-
break;
982-
case 'local':
983-
outputOverride = `*.output=type=local,platform-split=true,dest=${inpLocalExportDir}`;
984-
break;
985-
default:
986-
core.setFailed(`Invalid output: ${inpOutput}`);
987-
return;
988-
}
989-
9901010
let bakeOverrides = [];
9911011
try {
9921012
bakeOverrides = inpSet.map(override => renderTemplate(override));
@@ -998,7 +1018,7 @@ jobs:
9981018
if (inpSourceDateEpoch !== '') {
9991019
bakeOverrides.push(`*.args.SOURCE_DATE_EPOCH=${inpSourceDateEpoch}`);
10001020
}
1001-
bakeOverrides.push(outputOverride);
1021+
bakeOverrides.push(`*.output=${core.getInput('build-output')}`);
10021022
bakeOverrides.push('*.tags=');
10031023
if (GitHub.context.payload.repository?.private ?? false) {
10041024
// if this is a private repository, we set min provenance mode

0 commit comments

Comments
 (0)