Skip to content

Commit cd94e3f

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

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

.github/workflows/.test-bake.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,27 @@ jobs:
561561
sbom: true
562562
sign: ${{ github.event_name != 'pull_request' }}
563563
target: go-cross-with-contexts
564+
565+
bake-ghcr-index-annotations:
566+
uses: ./.github/workflows/bake.yml
567+
permissions:
568+
contents: read
569+
id-token: write
570+
packages: write
571+
with:
572+
context: test
573+
output: image
574+
push: ${{ github.event_name != 'pull_request' }}
575+
sbom: true
576+
target: hello-cross
577+
set-meta-annotations: true
578+
meta-images: ghcr.io/docker/github-builder-test
579+
meta-tags: |
580+
type=raw,value=bake-index-annotations-${{ github.run_id }}
581+
meta-annotations: |
582+
io.github.docker.github-builder.test-index-annotation=bake-${{ github.run_id }}
583+
secrets:
584+
registry-auths: |
585+
- registry: ghcr.io
586+
username: ${{ github.actor }}
587+
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/.test-build.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,27 @@ jobs:
578578
- registry: registry-1-stage.docker.io
579579
username: ${{ vars.DOCKERHUB_STAGE_USERNAME }}
580580
password: ${{ secrets.DOCKERHUB_STAGE_TOKEN }}
581+
582+
build-ghcr-index-annotations:
583+
uses: ./.github/workflows/build.yml
584+
permissions:
585+
contents: read
586+
id-token: write
587+
packages: write
588+
with:
589+
annotations: |
590+
io.github.docker.github-builder.test-index-annotation=build-${{ github.run_id }}
591+
file: test/hello.Dockerfile
592+
output: image
593+
platforms: linux/amd64,linux/arm64
594+
push: ${{ github.event_name != 'pull_request' }}
595+
sbom: true
596+
set-meta-annotations: true
597+
meta-images: ghcr.io/docker/github-builder-test
598+
meta-tags: |
599+
type=raw,value=build-index-annotations-${{ github.run_id }}
600+
secrets:
601+
registry-auths: |
602+
- registry: ghcr.io
603+
username: ${{ github.actor }}
604+
password: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/bake.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,12 +948,36 @@ jobs:
948948
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
949949
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
950950
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
951+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
952+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
951953
with:
952954
script: |
953955
const inpPush = core.getBooleanInput('push');
954956
const inpImageNames = core.getMultilineInput('image-names');
955957
const inpTagNames = core.getMultilineInput('tag-names');
956958
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
959+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
960+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
961+
962+
const toIndexAnnotation = annotation => {
963+
const keyEnd = annotation.indexOf('=');
964+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
965+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
966+
const typeSeparator = rawKey.indexOf(':');
967+
if (typeSeparator !== -1) {
968+
const typeExpr = rawKey.substring(0, typeSeparator);
969+
const key = rawKey.substring(typeSeparator + 1);
970+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
971+
if (hasKnownType) {
972+
return `index:${key}${rawValue}`;
973+
}
974+
}
975+
return `index:${annotation}`;
976+
};
977+
const indexAnnotations = [];
978+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
979+
indexAnnotations.push(...inpMetaAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation));
980+
}
957981
958982
const digests = [];
959983
for (const key of Object.keys(inpBuildOutputs)) {
@@ -972,6 +996,9 @@ jobs:
972996
for (const tag of inpTagNames) {
973997
createArgs.push('-t', `${imageName}:${tag}`);
974998
}
999+
for (const annotation of indexAnnotations) {
1000+
createArgs.push('--annotation', annotation);
1001+
}
9751002
for (const digest of digests) {
9761003
createArgs.push(digest);
9771004
}

.github/workflows/build.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,38 @@ jobs:
801801
INPUT_IMAGE-NAMES: ${{ inputs.meta-images }}
802802
INPUT_TAG-NAMES: ${{ steps.meta.outputs.tag-names }}
803803
INPUT_BUILD-OUTPUTS: ${{ toJSON(needs.build.outputs) }}
804+
INPUT_ANNOTATIONS: ${{ inputs.annotations }}
805+
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
806+
INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
804807
with:
805808
script: |
806809
const inpPush = core.getBooleanInput('push');
807810
const inpImageNames = core.getMultilineInput('image-names');
808811
const inpTagNames = core.getMultilineInput('tag-names');
809812
const inpBuildOutputs = JSON.parse(core.getInput('build-outputs'));
813+
const inpAnnotations = core.getMultilineInput('annotations');
814+
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
815+
const inpMetaAnnotations = core.getMultilineInput('meta-annotations');
816+
817+
const toIndexAnnotation = annotation => {
818+
const keyEnd = annotation.indexOf('=');
819+
const rawKey = keyEnd === -1 ? annotation : annotation.substring(0, keyEnd);
820+
const rawValue = keyEnd === -1 ? '' : annotation.substring(keyEnd);
821+
const typeSeparator = rawKey.indexOf(':');
822+
if (typeSeparator !== -1) {
823+
const typeExpr = rawKey.substring(0, typeSeparator);
824+
const key = rawKey.substring(typeSeparator + 1);
825+
const hasKnownType = typeExpr.split(',').map(type => type.replace(/\[.*\]$/, '')).some(type => ['manifest', 'index', 'manifest-descriptor', 'index-descriptor'].includes(type));
826+
if (hasKnownType) {
827+
return `index:${key}${rawValue}`;
828+
}
829+
}
830+
return `index:${annotation}`;
831+
};
832+
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {
833+
inpAnnotations.push(...inpMetaAnnotations);
834+
}
835+
const indexAnnotations = inpAnnotations.filter(annotation => annotation.length > 0).map(toIndexAnnotation);
810836
811837
const digests = [];
812838
for (const key of Object.keys(inpBuildOutputs)) {
@@ -825,6 +851,9 @@ jobs:
825851
for (const tag of inpTagNames) {
826852
createArgs.push('-t', `${imageName}:${tag}`);
827853
}
854+
for (const annotation of indexAnnotations) {
855+
createArgs.push('--annotation', annotation);
856+
}
828857
for (const digest of digests) {
829858
createArgs.push(digest);
830859
}

0 commit comments

Comments
 (0)