Skip to content

Commit d2c37dc

Browse files
authored
Merge pull request #119 from docker/index-annotations
set index annotations on imagetools create
2 parents a5e7103 + f0845ac commit d2c37dc

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
@@ -981,6 +981,8 @@ jobs:
981981
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
982982
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
983983
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
984+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
985+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
984986
with:
985987
script: |
986988
const { ImageTools } = require('@docker/actions-toolkit/lib/buildx/imagetools');
@@ -989,6 +991,28 @@ jobs:
989991
const inpImageNames = core.getMultilineInput('image-names');
990992
const inpTagNames = core.getMultilineInput('tag-names');
991993
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
994+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
995+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
996+
997+
const toIndexAnnotation = annotation => {
998+
const keyEnd = annotation.indexOf('=');
999+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
1000+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
1001+
const typeSeparator = rawKey.indexOf(':');
1002+
if (typeSeparator !== -1) {
1003+
const typeExpr = rawKey.substring(0, typeSeparator);
1004+
const key = rawKey.substring(typeSeparator + 1);
1005+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
1006+
if (hasKnownType) {
1007+
return `index:${key}${rawValue}`;
1008+
}
1009+
}
1010+
return `index:${annotation}`;
1011+
};
1012+
const indexAnnotations = [];
1013+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
1014+
indexAnnotations.push(...inpMetaAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation));
1015+
}
9921016
9931017
const digests = [];
9941018
for (const key of Object.keys(inpBuildOutputs)) {
@@ -1011,6 +1035,7 @@ jobs:
10111035
const result = await new ImageTools().create({
10121036
sources: digests,
10131037
tags: tags,
1038+
annotations: indexAnnotations,
10141039
skipExec: !inpPush
10151040
});
10161041
if (inpPush) {

.github/workflows/build.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@ jobs:
844844
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
845845
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
846846
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
847+
INPUT_ANNOTATIONS: ${{ inputs.annotations }}
848+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
849+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
847850
with:
848851
script: |
849852
const { ImageTools } = require('@docker/actions-toolkit/lib/buildx/imagetools');
@@ -852,6 +855,29 @@ jobs:
852855
const inpImageNames = core.getMultilineInput('image-names');
853856
const inpTagNames = core.getMultilineInput('tag-names');
854857
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
858+
const inpAnnotations = core.getMultilineInput('annotations');
859+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
860+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
861+
862+
const toIndexAnnotation = annotation => {
863+
const keyEnd = annotation.indexOf('=');
864+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
865+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
866+
const typeSeparator = rawKey.indexOf(':');
867+
if (typeSeparator !== -1) {
868+
const typeExpr = rawKey.substring(0, typeSeparator);
869+
const key = rawKey.substring(typeSeparator + 1);
870+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
871+
if (hasKnownType) {
872+
return `index:${key}${rawValue}`;
873+
}
874+
}
875+
return `index:${annotation}`;
876+
};
877+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
878+
inpAnnotations.push(...inpMetaAnnotations);
879+
}
880+
const indexAnnotations = inpAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation);
855881
856882
const digests = [];
857883
for (const key of Object.keys(inpBuildOutputs)) {
@@ -874,6 +900,7 @@ jobs:
874900
const result = await new ImageTools().create({
875901
sources: digests,
876902
tags: tags,
903+
annotations: indexAnnotations,
877904
skipExec: !inpPush
878905
});
879906
if (inpPush) {

0 commit comments

Comments
 (0)