|
1 | | -#!/usr/bin/env bash |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | | -set -e |
4 | | -source "${BASH_SOURCE%/*}"/lib |
| 3 | +import argparse as ap |
| 4 | +import subprocess as sp |
| 5 | +import typing as tp |
| 6 | +import os |
5 | 7 |
|
6 | | -usage="$0 [OPTION]... IMAGESDIR [VERSION] |
7 | | -
|
8 | | -Build the Docker images under the IMAGESDIR directory. |
| 8 | +parser = ap.ArgumentParser( |
| 9 | + description=""" |
| 10 | +Build the Docker images under the IMAGESDIR directory (default: .). |
9 | 11 | Label each built image with the given VERSION number (default: latest). |
10 | 12 | The build takes advantage of any reusable layer from previously built images. |
11 | | -
|
12 | | -Options: |
13 | | -
|
14 | | - -h Show this help message. |
15 | | - -p Publish each built image to the GitHub Container Registry |
16 | | - (you need to be logged in to ghcr.io with the appropriate |
17 | | - permissions for this to work)." |
18 | | - |
19 | | -helpflag= |
20 | | -publishflag= |
21 | | - |
22 | | -while getopts hp name; do |
23 | | - case $name in |
24 | | - h) helpflag=1 ;; |
25 | | - p) publishflag=1 ;; |
26 | | - *) error "Invalid option. Use the -h flag for more information." ;; |
27 | | - esac |
28 | | -done |
29 | | - |
30 | | -shift $((OPTIND - 1)) |
31 | | - |
32 | | -if [[ -n $helpflag ]]; then |
33 | | - echo "$usage" |
34 | | - exit |
35 | | -fi |
36 | | - |
37 | | -if [[ $# -eq 0 ]]; then |
38 | | - error "Missing IMAGESDIR arguments. Use the -h flag for more information." |
39 | | -fi |
40 | | - |
41 | | -if [[ $# -gt 2 ]]; then |
42 | | - error "Extraneous arguments. Use the -h flag for more information." |
43 | | -fi |
44 | | - |
45 | | -imagesdir="$1" |
46 | | -version="$2" |
47 | | - |
48 | | -# Enable BuildKit for better cache behavior |
49 | | -# See <https://docs.docker.com/engine/reference/builder/#buildkit> |
50 | | -export DOCKER_BUILDKIT=1 |
51 | | - |
52 | | -docker-build() { |
53 | | - from="$(image-name "$1" "$version")" |
54 | | - target="$(image-name "$2" "$version")" |
55 | | - status "Building image '$target'" |
56 | | - docker image build \ |
57 | | - --build-arg BUILDKIT_INLINE_CACHE=1 \ |
58 | | - --build-arg FROM="$from" \ |
59 | | - --cache-from "$(image-name "$2")" \ |
60 | | - --tag "$target" . |
61 | | - |
62 | | - if [[ -n $publishflag ]]; then |
63 | | - docker image push "$target" |
64 | | - fi |
| 13 | +""" |
| 14 | +) |
| 15 | + |
| 16 | +builds = ["toolchain", "base", "golang", "python", "qt", "rust", "dotnet6"] |
| 17 | +requires = { |
| 18 | + "toolchain": None, |
| 19 | + "base": "toolchain", |
| 20 | + "golang": "base", |
| 21 | + "python": "base", |
| 22 | + "qt": "base", |
| 23 | + "rust": "base", |
| 24 | + "dotnet6": "toolchain", |
65 | 25 | } |
66 | 26 |
|
67 | | -pushd "$imagesdir"/toolchain |
68 | | -docker-build toolchain toolchain |
69 | | -popd |
70 | | - |
71 | | -pushd "$imagesdir"/base |
72 | | -docker-build toolchain base |
73 | | -popd |
74 | | - |
75 | | -pushd "$imagesdir"/golang |
76 | | -docker-build base golang |
77 | | -popd |
78 | | - |
79 | | -pushd "$imagesdir"/python |
80 | | -docker-build base python |
81 | | -popd |
82 | | - |
83 | | -pushd "$imagesdir"/qt |
84 | | -docker-build base qt |
85 | | -popd |
86 | | - |
87 | | -pushd "$imagesdir"/rust |
88 | | -docker-build base rust |
89 | | -popd |
90 | | - |
91 | | -pushd "$imagesdir"/dotnet6 |
92 | | -docker-build toolchain dotnet6 |
93 | | -popd |
94 | | - |
95 | | -status "Done" |
| 27 | +parser.add_argument( |
| 28 | + "-p", help="Publish built images to GitHub Container Registry", action="store_true" |
| 29 | +) |
| 30 | +parser.add_argument("-g", help="Use GitHub Actions for caching", action="store_true") |
| 31 | +parser.add_argument("-d", help="Set image directory", default=".", metavar="IMAGESDIR") |
| 32 | +parser.add_argument( |
| 33 | + "-s", help="Just build single image given as argument", choices=builds |
| 34 | +) |
| 35 | +parser.add_argument( |
| 36 | + "-v", help="Set version number", default="latest", metavar="VERSION" |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def run_cmd(line: list[str]) -> None: |
| 41 | + p = sp.Popen( |
| 42 | + line, |
| 43 | + stdout=sp.PIPE, |
| 44 | + stderr=sp.STDOUT, |
| 45 | + encoding="utf-8", |
| 46 | + errors="replace", |
| 47 | + ) |
| 48 | + while True: |
| 49 | + realtime_output = p.stdout.readline() |
| 50 | + if realtime_output == "" and p.poll() is not None: |
| 51 | + break |
| 52 | + if realtime_output: |
| 53 | + print(realtime_output.strip(), flush=True) |
| 54 | + if p.returncode != 0: |
| 55 | + exit(p.returncode) |
| 56 | + |
| 57 | + |
| 58 | +def build( |
| 59 | + image: str, version: str, publish: bool, github: bool, _from: tp.Optional[str] = None |
| 60 | +) -> None: |
| 61 | + cmd: list[str] = [ |
| 62 | + "docker", |
| 63 | + "buildx", |
| 64 | + "build", |
| 65 | + "--build-arg", |
| 66 | + "BUILDKIT_INLINE_CACHE=1", |
| 67 | + ] |
| 68 | + if _from: |
| 69 | + cmd.extend(["--build-arg", f"FROM=ghcr.io/toltec-dev/{_from}:{version}"]) |
| 70 | + if github: |
| 71 | + cmd.extend(["--cache-from", f"type=gha,scope={image}"]) |
| 72 | + cmd.extend(["--cache-to", f"type=gha,mode=max,scope={image}"]) |
| 73 | + if publish: |
| 74 | + cmd.extend(["--push"]) |
| 75 | + cmd.extend(["--tag", f"ghcr.io/toltec-dev/{image}:{version}", "."]) |
| 76 | + run_cmd(cmd) |
| 77 | + |
| 78 | + |
| 79 | +args = parser.parse_args() |
| 80 | +curdir = os.getcwd() |
| 81 | +os.chdir(args.d) |
| 82 | +workdir = os.getcwd() |
| 83 | +if args.s: |
| 84 | + os.chdir(args.s) |
| 85 | + build(args.s, args.v, args.p, args.g, requires[args.s]) |
| 86 | + os.chdir(workdir) |
| 87 | +else: |
| 88 | + for i in builds: |
| 89 | + print(f"Building {i}...") |
| 90 | + os.chdir(i) |
| 91 | + build(i, args.v, args.p, args.g, requires[i]) |
| 92 | + os.chdir(workdir) |
| 93 | +os.chdir(curdir) |
0 commit comments