From 852066fee508970a62f615abe5d0898752f1f07d Mon Sep 17 00:00:00 2001 From: michael1011 Date: Fri, 27 Oct 2023 16:37:55 +0200 Subject: [PATCH] fix: CI Docker build workflow --- .github/workflows/docker-publish.yml | 2 +- docker/boltz/Dockerfile | 7 +- docker/build.py | 99 +++++++++++++++------------- 3 files changed, 59 insertions(+), 49 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 6dc8af2b..53a18d56 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -30,4 +30,4 @@ jobs: - name: Push backend image # Cross compiling to ARM64 is broken right now - run: ./docker/build.py buildx --platform linux/amd64 boltz + run: ./docker/build.py buildx --platform linux/amd64 --no-latest --branch ${GITHUB_REF##*/} boltz diff --git a/docker/boltz/Dockerfile b/docker/boltz/Dockerfile index 6b9b5c3d..12169fab 100644 --- a/docker/boltz/Dockerfile +++ b/docker/boltz/Dockerfile @@ -1,4 +1,3 @@ -ARG VERSION ARG NODE_VERSION FROM node:${NODE_VERSION} AS builder @@ -15,14 +14,14 @@ RUN apt-get -y install \ libzmq3-dev \ python3-dev -RUN git clone https://github.com/BoltzExchange/boltz-backend.git +ARG VERSION +RUN git clone --depth 1 https://github.com/BoltzExchange/boltz-backend.git -b ${VERSION} WORKDIR /boltz-backend -RUN git checkout ${VERSION} - # Remove dependency that is not needed for the build and unavailable on ARM64 RUN sed -i "/grpc-tools/d" package.json +RUN npm install -g npm RUN npm install RUN npm run compile diff --git a/docker/build.py b/docker/build.py index 95dd8ecd..8a662d05 100755 --- a/docker/build.py +++ b/docker/build.py @@ -21,7 +21,7 @@ class Image: """The tags and build arguments of a Docker image.""" - tags: list[str] + tag: str arguments: list[BuildArgument] @@ -56,52 +56,52 @@ class Image: IMAGES: dict[str, Image] = { "bitcoin-core": Image( - tags=[BITCOIN_VERSION], + tag=BITCOIN_VERSION, arguments=[ UBUNTU_VERSION, ], ), "litecoin-core": Image( - tags=[LITECOIN_VERSION], + tag=LITECOIN_VERSION, arguments=[ UBUNTU_VERSION, ], ), "geth": Image( - tags=[GETH_VERSION], + tag=GETH_VERSION, arguments=[ UBUNTU_VERSION, GOLANG_VERSION, ], ), "eclair": Image( - tags=[ECLAIR_VERSION], + tag=ECLAIR_VERSION, arguments=[ UBUNTU_VERSION, ], ), "c-lightning": Image( - tags=[C_LIGHTNING_VERSION], + tag=C_LIGHTNING_VERSION, arguments=[ UBUNTU_VERSION, BITCOIN_BUILD_ARG, ], ), "lnd": Image( - tags=[LND_VERSION], + tag=LND_VERSION, arguments=[ UBUNTU_VERSION, GOLANG_VERSION, ], ), "boltz": Image( - tags=["3.3.0"], + tag="v3.3.0", arguments=[ NODE_VERSION, ], ), "regtest": Image( - tags=["4.1.1"], + tag="4.1.1", arguments=[ UBUNTU_VERSION, BITCOIN_BUILD_ARG, @@ -157,10 +157,7 @@ def list_images(to_list: list[str]) -> None: build_details = get_build_details(image) print(f" - {image}") - print(" Tags:") - - for tag in build_details.tags: - print(f" - {tag}") + print(f" - Tag: {build_details.tag}") print() @@ -177,6 +174,7 @@ def build_images( organisation: str, no_cache: bool, no_latest: bool, + branch: str, buildx: bool, platform: str = "", ) -> None: @@ -185,45 +183,50 @@ def build_images( for image in to_build: build_details = get_build_details(image) + tag = build_details.tag + + if branch in ["master", "main"]: + tag = "latest" + elif branch != "": + tag = branch - for tag in build_details.tags: - build_args = [f"{arg.name}={arg.value}" for arg in build_details.arguments] + build_args = [f"{arg.name}={arg.value}" for arg in build_details.arguments] - # The regtest image doesn't need the version as a build flag - if image != "regtest": - build_args.append(f"VERSION={tag}") + # The regtest image doesn't need the version as a build flag + if image != "regtest": + build_args.append(f"VERSION={tag if tag != 'latest' else branch}") - # Add the prefix "--build-arg " to every entry and - # join the array to a string - args = " ".join(["--build-arg " + entry for entry in build_args]) + # Add the prefix "--build-arg " to every entry and + # join the array to a string + args = " ".join(["--build-arg " + entry for entry in build_args]) - name = f"{organisation}/{image}" - dockerfile = f"{image}/Dockerfile" + name = f"{organisation}/{image}" + dockerfile = f"{image}/Dockerfile" - if buildx: - extra_tag = "" if no_latest else f"--tag {name}:latest" - command = ( - f"docker buildx build --push {args} --platform {platform} " - f"--file {dockerfile} --tag {name}:{tag} {extra_tag} ." - ) - else: - extra_tag = "" if no_latest else f"-t {name}:latest" - command = ( - f"docker build -t {name}:{tag} {extra_tag} -f {dockerfile} {args} ." - ) + if buildx: + extra_tag = "" if no_latest else f"--tag {name}:latest" + command = ( + f"docker buildx build --push {args} --platform {platform} " + f"--file {dockerfile} --tag {name}:{tag} {extra_tag} ." + ) + else: + extra_tag = "" if no_latest else f"-t {name}:latest" + command = ( + f"docker build -t {name}:{tag} {extra_tag} -f {dockerfile} {args} ." + ) - if no_cache: - command = command + " --no-cache" + if no_cache: + command = command + " --no-cache" - print() - print_step(f"Building {image}:{tag}") + print() + print_step(f"Building {image}:{tag}") - print(command) - print() + print(command) + print() - if system(command) != 0: - print_error(f"Could not build image {image}") - sys.exit(1) + if system(command) != 0: + print_error(f"Could not build image {image}") + sys.exit(1) print() print_step("Built images: {}".format(", ".join(to_build))) @@ -254,6 +257,7 @@ def parse_images(to_parse: list[str]) -> list[str]: BUILD_PARSER.add_argument("images", type=str, nargs="*") BUILD_PARSER.add_argument("--no-cache", dest="no_cache", action="store_true") BUILD_PARSER.add_argument("--no-latest", dest="no_latest", action="store_true") + BUILD_PARSER.add_argument("--branch", default="", help="Branch to build") BUILD_PARSER.add_argument( "--organisation", default="boltz", @@ -263,6 +267,7 @@ def parse_images(to_parse: list[str]) -> list[str]: BUILDX_PARSER.add_argument("images", type=str, nargs="*") BUILDX_PARSER.add_argument("--no-cache", dest="no_cache", action="store_true") BUILDX_PARSER.add_argument("--no-latest", dest="no_latest", action="store_true") + BUILDX_PARSER.add_argument("--branch", default="", help="Branch to build") BUILDX_PARSER.add_argument( "--platform", default="linux/amd64,linux/arm64", @@ -282,7 +287,12 @@ def parse_images(to_parse: list[str]) -> list[str]: list_images(PARSED_IMAGES) elif ARGS.command == "build": build_images( - PARSED_IMAGES, ARGS.organisation, ARGS.no_cache, ARGS.no_latest, False + PARSED_IMAGES, + ARGS.organisation, + ARGS.no_cache, + ARGS.no_latest, + ARGS.branch, + False, ) elif ARGS.command == "buildx": build_images( @@ -290,6 +300,7 @@ def parse_images(to_parse: list[str]) -> list[str]: ARGS.organisation, ARGS.no_cache, ARGS.no_latest, + ARGS.branch, True, ARGS.platform, )