| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Usage: |
| 4 |
# |
| 5 |
# ./images-manifest BRANCH DOCKERFILE1 [DOCKERFILE2 ...] |
| 6 |
# |
| 7 |
# Create a new manifest list (fat manifest) containing the images built with |
| 8 |
# “images-build”, whether in this pipeline or previously, tagged with the Git |
| 9 |
# branch. |
| 10 |
# |
| 11 |
# This is needed because there is no way (that I could figure out) to push a |
| 12 |
# multi-arch image without the architectures racing. So we build and push the |
| 13 |
# images with their (skinny) manifests, then after all architectures are done, |
| 14 |
# run this script to set up the manifest lists. We do this unconditionally |
| 15 |
# because it’s relatively quick and then we don’t need to match any existing |
| 16 |
# state. |
| 17 |
# |
| 18 |
# Note that commits within the same branch still race (see #1981), but that |
| 19 |
# seemed hard to fix and thus acceptable. |
| 20 |
|
| 21 |
set -e # needs to be POSIX so can’t use our fancy traps |
| 22 |
|
| 23 |
# Add directory containing script to PATH so it doesn’t yell about arch.sh and |
| 24 |
# friends during manual testing. |
| 25 |
PATH=$(cd "$(dirname "$0")" && pwd):$PATH |
| 26 |
export PATH |
| 27 |
|
| 28 |
. images.sh |
| 29 |
|
| 30 |
branch=$1 |
| 31 |
shift |
| 32 |
echo "branch: $branch" 1>&2 |
| 33 |
|
| 34 |
for df in "$@"; do |
| 35 |
|
| 36 |
echo |
| 37 |
echo "file: $df" 1>&2 |
| 38 |
name=$(tag_from_filename "$df") |
| 39 |
echo "image name: $name" 1>&2 |
| 40 |
|
| 41 |
refs='' |
| 42 |
for arch in amd64 arm64; do |
| 43 |
echo "arch: $arch" |
| 44 |
digest=$(fulldigest "$arch" "$df") |
| 45 |
echo " digest: $digest" 1>&2 |
| 46 |
ref=$regy/$name:$digest |
| 47 |
#echo " ref: $ref" 1>&2 |
| 48 |
printf ' in registry: ' 1>&2 |
| 49 |
if registry_exists "$ref"; then |
| 50 |
echo 'yes' 1>&2 |
| 51 |
refs="$refs $ref" |
| 52 |
else |
| 53 |
echo "no, skipping" 1>&2 |
| 54 |
fi |
| 55 |
done |
| 56 |
|
| 57 |
echo 1>&2 |
| 58 |
ref_branch=$regy/$name:$branch |
| 59 |
ref_latest=$regy/$name:latest |
| 60 |
if [ "$refs" = '' ]; then |
| 61 |
# Nothing to put in a new manifest list, and we cannot create an empty |
| 62 |
# one (thanks OCI!). |
| 63 |
echo 'NOTE: No registry images to put in manifest list.' 1>&2 |
| 64 |
# Deleting an existing stale manifest list is hairy (you have to use |
| 65 |
# GitLab APIs and the Docker image has no tool that can send HTTP |
| 66 |
# DELETE), so just warn and YOLO. |
| 67 |
if docker buildx imagetools inspect "$ref_branch" 1>&2; then |
| 68 |
echo 1>&2 |
| 69 |
echo 'WARNING: Found stale manifest list; do not use it.' 1>&2 |
| 70 |
echo 1>&2 |
| 71 |
fi |
| 72 |
else |
| 73 |
# We also push the “latest” tag because various things keep using it. |
| 74 |
# Anything that cares about its content should definitely use the |
| 75 |
# branch-specific tag. |
| 76 |
for ref in "$ref_branch" "$ref_latest"; do |
| 77 |
# Write new manifest list, replacing any existing list. |
| 78 |
# shellcheck disable=SC2086 # POSIX sh has no arrays |
| 79 |
docker buildx imagetools create $refs --tag "$ref" 1>&2 |
| 80 |
docker buildx imagetools inspect "$ref" 1>&2 |
| 81 |
done |
| 82 |
fi |
| 83 |
|
| 84 |
done |