|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This is a very simple script to build and push the Docker image used by the |
| 4 | +# Docker volume workspace driver. It's normally run from the "Publish Docker |
| 5 | +# image dependencies" GitHub Action, but can be run locally if necessary. |
| 6 | +# |
| 7 | +# This script requires Python 3.8 or later, and Docker 19.03 (for buildx). You |
| 8 | +# are strongly encouraged to use Black to format this file after modifying it. |
| 9 | +# |
| 10 | +# To run it locally, you'll need to be logged into Docker Hub, and create an |
| 11 | +# image in a namespace that you have access to. For example, if your username is |
| 12 | +# "alice", you could build and push the image as follows: |
| 13 | +# |
| 14 | +# $ ./push.py -d Dockerfile -i alice/src-campaign-volume-workspace |
| 15 | +# |
| 16 | +# By default, only the "latest" tag will be built and pushed. The script refers |
| 17 | +# to the HEAD ref given to it, either via $GITHUB_REF or the -r argument. If |
| 18 | +# this is in the form refs/tags/X.Y.Z, then we'll also push X, X.Y, and X.Y.Z |
| 19 | +# tags. |
| 20 | +# |
| 21 | +# Finally, if you have your environment configured to allow multi-architecture |
| 22 | +# builds with docker buildx, you can provide a --platform argument that will be |
| 23 | +# passed through verbatim to docker buildx build. (This is how we build ARM64 |
| 24 | +# builds in our CI.) For example, you could build ARM64 and AMD64 images with: |
| 25 | +# |
| 26 | +# $ ./push.py --platform linux/arm64,linux/amd64 ... |
| 27 | +# |
| 28 | +# For this to work, you will need a builder with the relevant platforms enabled. |
| 29 | +# More instructions on this can be found at |
| 30 | +# https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images. |
| 31 | + |
| 32 | +import argparse |
| 33 | +import itertools |
| 34 | +import os |
| 35 | +import subprocess |
| 36 | + |
| 37 | +from typing import BinaryIO, Optional, Sequence |
| 38 | + |
| 39 | + |
| 40 | +def calculate_tags(ref: str) -> Sequence[str]: |
| 41 | + # The tags always include latest. |
| 42 | + tags = ["latest"] |
| 43 | + |
| 44 | + # If the ref is a tag ref, then we should parse the version out and add each |
| 45 | + # component to our tag list: for example, for X.Y.Z, we want tags X, X.Y, |
| 46 | + # and X.Y.Z. |
| 47 | + if ref.startswith("refs/tags/"): |
| 48 | + tags.extend( |
| 49 | + [ |
| 50 | + # Join the version components back into a string. |
| 51 | + ".".join(vc) |
| 52 | + for vc in itertools.accumulate( |
| 53 | + # Split the version string into its components. |
| 54 | + ref.split("/", 2)[2].split("."), |
| 55 | + # Accumulate each component we've seen into a new list |
| 56 | + # entry. |
| 57 | + lambda vlist, v: vlist + [v], |
| 58 | + initial=[], |
| 59 | + ) |
| 60 | + # We also get the initial value, so we need to skip that. |
| 61 | + if len(vc) > 0 |
| 62 | + ] |
| 63 | + ) |
| 64 | + |
| 65 | + return tags |
| 66 | + |
| 67 | + |
| 68 | +def docker_build( |
| 69 | + dockerfile: BinaryIO, platform: Optional[str], image: str, tags: Sequence[str] |
| 70 | +): |
| 71 | + args = ["docker", "buildx", "build", "--push"] |
| 72 | + |
| 73 | + for tag in tags: |
| 74 | + args.extend(["-t", f"{image}:{tag}"]) |
| 75 | + |
| 76 | + if platform is not None: |
| 77 | + args.extend(["--platform", platform]) |
| 78 | + |
| 79 | + # Since we provide the Dockerfile via stdin, we don't need to provide it |
| 80 | + # here. (Doing so means that we don't carry an unncessary context into the |
| 81 | + # builder.) |
| 82 | + args.append("-") |
| 83 | + |
| 84 | + run(args, stdin=dockerfile) |
| 85 | + |
| 86 | + |
| 87 | +def docker_login(username: str, password: str): |
| 88 | + run( |
| 89 | + ["docker", "login", f"-u={username}", "--password-stdin"], |
| 90 | + input=password, |
| 91 | + text=True, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def run(args: Sequence[str], /, **kwargs) -> subprocess.CompletedProcess: |
| 96 | + print(f"+ {' '.join(args)}") |
| 97 | + return subprocess.run(args, check=True, **kwargs) |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + parser = argparse.ArgumentParser() |
| 102 | + parser.add_argument( |
| 103 | + "-d", "--dockerfile", required=True, help="the Dockerfile to build" |
| 104 | + ) |
| 105 | + parser.add_argument("-i", "--image", required=True, help="Docker image to push") |
| 106 | + parser.add_argument( |
| 107 | + "-p", |
| 108 | + "--platform", |
| 109 | + help="platforms to build using docker buildx (if omitted, the default will be used)", |
| 110 | + ) |
| 111 | + parser.add_argument( |
| 112 | + "-r", |
| 113 | + "--ref", |
| 114 | + default=os.environ.get("GITHUB_REF"), |
| 115 | + help="current ref in refs/heads/... or refs/tags/... form", |
| 116 | + ) |
| 117 | + args = parser.parse_args() |
| 118 | + |
| 119 | + tags = calculate_tags(args.ref) |
| 120 | + print(f"will push tags: {', '.join(tags)}") |
| 121 | + |
| 122 | + print("logging into Docker Hub") |
| 123 | + try: |
| 124 | + docker_login(os.environ["DOCKER_USERNAME"], os.environ["DOCKER_PASSWORD"]) |
| 125 | + except KeyError as e: |
| 126 | + print(f"error retrieving environment variables: {e}") |
| 127 | + raise |
| 128 | + |
| 129 | + print("building and pushing image") |
| 130 | + docker_build(open(args.dockerfile, "rb"), args.platform, args.image, tags) |
| 131 | + |
| 132 | + print("success!") |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments