Skip to content

Commit

Permalink
fix: CI Docker build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Oct 27, 2023
1 parent d8b7a60 commit 852066f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions docker/boltz/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ARG VERSION
ARG NODE_VERSION

FROM node:${NODE_VERSION} AS builder
Expand All @@ -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

Expand Down
99 changes: 55 additions & 44 deletions docker/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Image:

"""The tags and build arguments of a Docker image."""

tags: list[str]
tag: str
arguments: list[BuildArgument]


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -177,6 +174,7 @@ def build_images(
organisation: str,
no_cache: bool,
no_latest: bool,
branch: str,
buildx: bool,
platform: str = "",
) -> None:
Expand All @@ -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)))
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -282,14 +287,20 @@ 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(
PARSED_IMAGES,
ARGS.organisation,
ARGS.no_cache,
ARGS.no_latest,
ARGS.branch,
True,
ARGS.platform,
)

0 comments on commit 852066f

Please sign in to comment.