Debian Codegraph @

test/gitlab.com/images-build

raw
1 #!/bin/sh
2
3 # Usage:
4 #
5 # ./images-build BRANCH DOCKERFILE1 [DOCKERFILE2 ...]
6 #
7 # Build specified Dockerfiles for the host architecture and push them to the
8 # project registry. These are used by CI jobs, not a Charliecloud test suite
9 # image.
10 #
11 # WARNING: This does not tag the images in a usable way. See “images-tag”
12 # for that. Two separate scripts seemed the only way to do it without a race
13 # condition.
14 #
15 # Because of this bootstrap context, we need to run *this* job using an image
16 # available somewhere else. It also needs to have some image builder
17 # installed. GitLab recommends images provided by Docker that are very bare
18 # bones (Alpine with no programming language other than Busybox) [1], which is
19 # why this is a POSIX shell script.
20 #
21 # An alternative would be Podman images, which are Fedora based and do have a
22 # recent Python 3 [2].
23 #
24 # CONFUSION OPPORTUNITY: The local image builder does not know if the correct
25 # image is already in the registry, and it doesn’t even know the layer digests
26 # without building the layers, which we want to avoid. Therefore, we *tag* the
27 # images with the MD5 of the Dockerfile plus the base image digest, and build
28 # a new image if either is not in the registry. That is, we use tags that are
29 # digests, which is weird and confusing.
30 #
31 # PREREQUISITES:
32 #
33 # 1. Log in to the gitlab.com container registry. For testing, do a manual
34 # “docker login” with your username and a personal access token
35 # (PAT) [3]. While your username/password will work, don’t do that
36 # because Docker stores it in a plaintext file.
37 #
38 # 2. Set $CI_REGISTRY_IMAGE [4] if the default below isn’t appropriate.
39 #
40 # [1]: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker
41 # [2]: https://quay.io/repository/podman/stable
42 # [3]: https://docs.gitlab.com/ee/user/packages/container_registry/authenticate_with_container_registry.html
43 # [4]: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
44
45 set -e # needs to be POSIX so can’t use our fancy traps
46
47 # Add directory containing script to PATH so it doesn’t yell about arch.sh and
48 # friends during manual testing.
49 PATH=$(cd "$(dirname "$0")" && pwd):$PATH
50 export PATH
51
52 . images.sh
53
54 branch=$1
55 shift
56 echo "branch: $branch" 1>&2
57
58 arch=$(arch.sh)
59 echo "architecture: $arch" 1>&2
60 #echo "nv distro: $nv_distro"
61
62 for df in "$@"; do
63
64 echo
65 echo "file: $df" 1>&2
66 name=$(tag_from_filename "$df")
67 echo "image name: $name" 1>&2
68 digest=$(fulldigest "$arch" "$df")
69 echo "digest: $digest" 1>&2
70
71 # Check if image exists in our registry; if not, build and push it, tagged
72 # with the digest we computed.
73 ref_digest=$regy/$name:$digest
74 printf 'in registry: ' 1>&2
75 if registry_exists "$ref_digest"; then
76 echo 'yes, skipping build' 1>&2
77 else
78 echo 'no, building and pushing ...' 1>&2
79 context=$(dirname "$df")
80 echo "context: $context" 1>&2
81 docker image list
82 # We only want to pull the base image if it’s not in the local Docker
83 # images, because in that case the repository might contain an old
84 # version. This is the default behavior without --pull.
85 build -t "$ref_digest" -f "$df" \
86 --build-arg=regy="$regy/" \
87 --build-arg=branch="$branch" \
88 "$context"
89 echo 1>&2
90 docker push "$ref_digest"
91 # Tag so descendant images can use it even before we tag it up with
92 # the right manifest in images-manifest.
93 docker tag "$ref_digest" "$name:$branch" 1>&2
94 docker tag "$ref_digest" "$regy/$name:$branch" 1>&2
95 fi
96
97 done