Skip to content

Commit f0845ac

Browse files
committed
set index annotations on imagetools create
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 61b16ec commit f0845ac

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

.github/workflows/.test-bake.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,29 @@ jobs:
596596
sbom: true
597597
sign: ${{ github.event_name != 'pull_request' }}
598598
target: go-cross-with-contexts
599+
600+
bake-ghcr-index-annotations:
601+
uses: ./.github/workflows/bake.yml
602+
permissions:
603+
contents: read
604+
id-token: write
605+
packages: write
606+
with:
607+
context: test
608+
output: image
609+
push: ${{ github.event_name != 'pull_request' }}
610+
sbom: true
611+
set: |
612+
*.args.VERSION={{meta.version}}
613+
target: hello-cross
614+
set-meta-annotations: true
615+
meta-images: ghcr.io/docker/github-builder-test
616+
meta-tags: |
617+
type=raw,value=bake-index-annotations-${{ github.run_id }}
618+
meta-annotations: |
619+
io.github.docker.github-builder.test-index-annotation=bake-${{ github.run_id }}
620+
secrets:
621+
registry-auths: |
622+
- registry: ghcr.io
623+
username: ${{ github.actor }}
624+
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/.test-build.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,29 @@ jobs:
615615
- registry: registry-1-stage.docker.io
616616
username: ${{ vars.DOCKERHUB_STAGE_USERNAME }}
617617
password: ${{ secrets.DOCKERHUB_STAGE_TOKEN }}
618+
619+
build-ghcr-index-annotations:
620+
uses: ./.github/workflows/build.yml
621+
permissions:
622+
contents: read
623+
id-token: write
624+
packages: write
625+
with:
626+
annotations: |
627+
io.github.docker.github-builder.test-index-annotation=build-${{ github.run_id }}
628+
build-args: |
629+
VERSION={{meta.version}}
630+
file: test/hello.Dockerfile
631+
output: image
632+
platforms: linux/amd64,linux/arm64
633+
push: ${{ github.event_name != 'pull_request' }}
634+
sbom: true
635+
set-meta-annotations: true
636+
meta-images: ghcr.io/docker/github-builder-test
637+
meta-tags: |
638+
type=raw,value=build-index-annotations-${{ github.run_id }}
639+
secrets:
640+
registry-auths: |
641+
- registry: ghcr.io
642+
username: ${{ github.actor }}
643+
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/bake.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,8 @@ jobs:
976976
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
977977
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
978978
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
979+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
980+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
979981
with:
980982
script: |
981983
const { ImageTools } = require('@docker/actions-toolkit/lib/buildx/imagetools');
@@ -984,6 +986,28 @@ jobs:
984986
const inpImageNames = core.getMultilineInput('image-names');
985987
const inpTagNames = core.getMultilineInput('tag-names');
986988
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
989+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
990+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
991+
992+
const toIndexAnnotation = annotation => {
993+
const keyEnd = annotation.indexOf('=');
994+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
995+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
996+
const typeSeparator = rawKey.indexOf(':');
997+
if (typeSeparator !== -1) {
998+
const typeExpr = rawKey.substring(0, typeSeparator);
999+
const key = rawKey.substring(typeSeparator + 1);
1000+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
1001+
if (hasKnownType) {
1002+
return `index:${key}${rawValue}`;
1003+
}
1004+
}
1005+
return `index:${annotation}`;
1006+
};
1007+
const indexAnnotations = [];
1008+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
1009+
indexAnnotations.push(...inpMetaAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation));
1010+
}
9871011
9881012
const digests = [];
9891013
for (const key of Object.keys(inpBuildOutputs)) {
@@ -1006,6 +1030,7 @@ jobs:
10061030
const result = await new ImageTools().create({
10071031
sources: digests,
10081032
tags: tags,
1033+
annotations: indexAnnotations,
10091034
skipExec: !inpPush
10101035
});
10111036
if (inpPush) {

.github/workflows/build.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,9 @@ jobs:
839839
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
840840
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
841841
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
842+
INPUT_ANNOTATIONS: ${{ inputs.annotations }}
843+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
844+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
842845
with:
843846
script: |
844847
const { ImageTools } = require('@docker/actions-toolkit/lib/buildx/imagetools');
@@ -847,6 +850,29 @@ jobs:
847850
const inpImageNames = core.getMultilineInput('image-names');
848851
const inpTagNames = core.getMultilineInput('tag-names');
849852
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
853+
const inpAnnotations = core.getMultilineInput('annotations');
854+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
855+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
856+
857+
const toIndexAnnotation = annotation => {
858+
const keyEnd = annotation.indexOf('=');
859+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
860+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
861+
const typeSeparator = rawKey.indexOf(':');
862+
if (typeSeparator !== -1) {
863+
const typeExpr = rawKey.substring(0, typeSeparator);
864+
const key = rawKey.substring(typeSeparator + 1);
865+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
866+
if (hasKnownType) {
867+
return `index:${key}${rawValue}`;
868+
}
869+
}
870+
return `index:${annotation}`;
871+
};
872+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
873+
inpAnnotations.push(...inpMetaAnnotations);
874+
}
875+
const indexAnnotations = inpAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation);
850876
851877
const digests = [];
852878
for (const key of Object.keys(inpBuildOutputs)) {
@@ -869,6 +895,7 @@ jobs:
869895
const result = await new ImageTools().create({
870896
sources: digests,
871897
tags: tags,
898+
annotations: indexAnnotations,
872899
skipExec: !inpPush
873900
});
874901
if (inpPush) {

0 commit comments

Comments
 (0)