From c8b2735cffe213a115ab47167d79c410c5ab17a1 Mon Sep 17 00:00:00 2001 From: Snider Date: Wed, 1 Oct 2025 23:33:44 +0100 Subject: [PATCH] Refactor Docker build and Makefile for improved CI/CD Streamlines Docker build workflow by introducing a builder target and registry-based build cache in GitHub Actions. Refactors the Makefile to improve static/dynamic build flag handling, adds MSVC support, and separates configure/build steps. Updates Dockerfile to use new Makefile targets, simplifies build context handling, and fixes artifact copying. Removes redundant Docker build targets from the Makefile and enhances Conan MSVC runtime configuration. --- .github/workflows/build-docker.yml | 11 ++++- Makefile | 59 ++++++++++------------- conanfile.py | 6 +++ utils/docker/images/lthn-chain/Dockerfile | 47 +++++++++--------- 4 files changed, 63 insertions(+), 60 deletions(-) diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index 389b1fc9..cc82d516 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -38,6 +38,14 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v6 + with: + push: true + target: builder + tags: lthn/chain:${{ inputs.chain-network == 'testnet' && 'testnet' || 'latest' }} + cache-from: type=registry,ref=lthn/chain:buildcache + cache-to: type=registry,ref=lthn/chain:buildcache,mode=max - name: Build and push id: docker_build @@ -52,6 +60,5 @@ jobs: BUILD_THREADS=2 BUILD_LOCAL=1 tags: lthn/chain:${{ inputs.chain-network == 'testnet' && 'testnet' || 'latest' }} - cache-from: type=gha - cache-to: type=gha,mode=max + cache-from: type=registry,ref=lthn/chain:buildcache diff --git a/Makefile b/Makefile index 65b7bc88..9e1a1885 100644 --- a/Makefile +++ b/Makefile @@ -80,28 +80,46 @@ CONAN_CACHE := $(CURDIR)/build/sdk DEFAULT_CONAN_PROFILE := $(CONAN_CACHE)/profiles/default CC_DOCKER_FILE?=utils/docker/images/lthn-chain/Dockerfile +ifeq ($(OS),Windows_NT) + ifneq (,$(findstring cl,$(CC))) + MSVC := 1 + endif +endif ifeq ($(STATIC), 1) - CONAN_STATIC_FLAG = True + ifeq ($(MSVC), 1) + CONAN_STATIC_FLAG = -s compiler.runtime=static -o *:static=True + else + CONAN_STATIC_FLAG = -o *:static=True + endif else - CONAN_STATIC_FLAG = False + ifeq ($(MSVC), 1) + CONAN_STATIC_FLAG = -s compiler.runtime=dynamic -o *:static=False + else + CONAN_STATIC_FLAG = -o *:static=False + endif endif all: help -release: conan-profile-detect - @echo "Building profile: $(BUILD_TYPE) testnet=$(TESTNET)" - CONAN_HOME=$(CONAN_CACHE) conan install . --build=missing -s build_type=$(BUILD_TYPE) -o *:static=$(CONAN_STATIC_FLAG) - cmake -S . -B $(BUILD_FOLDER) -DCMAKE_TOOLCHAIN_FILE=$(BUILD_FOLDER)/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSTATIC=$(STATIC) -DTESTNET=$(TESTNET) -DBUILD_VERSION=$(BUILD_VERSION) - cmake --build $(BUILD_FOLDER) --config=$(BUILD_TYPE) --parallel=$(CPU_CORES) +release: build (cd $(BUILD_FOLDER) && cpack) +build: configure + cmake --build $(BUILD_FOLDER) --config=$(BUILD_TYPE) --parallel=$(CPU_CORES) + debug: conan-profile-detect @echo "Building profile: debug" CONAN_HOME=$(CONAN_CACHE) conan install . --output-folder=build/debug --build=missing -s build_type=Debug cmake -S . -B build/debug -DCMAKE_TOOLCHAIN_FILE=build/debug/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug -DTESTNET=$(TESTNET) cmake --build build/debug --config=Debug --parallel=$(CPU_CORES) +configure: conan-profile-detect + @echo "Config profile: $(BUILD_TYPE) testnet=$(TESTNET)" + CONAN_HOME=$(CONAN_CACHE) conan install . --build=missing -s build_type=$(BUILD_TYPE) $(CONAN_STATIC_FLAG) + cmake -S . -B $(BUILD_FOLDER) -DCMAKE_TOOLCHAIN_FILE=$(BUILD_FOLDER)/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSTATIC=$(STATIC) -DTESTNET=$(TESTNET) -DBUILD_VERSION=$(BUILD_VERSION) + + conan-profile-detect: @if [ ! -f "$(DEFAULT_CONAN_PROFILE)" ]; then \ echo "Default conan profile not found. Detecting a new one..."; \ @@ -149,10 +167,6 @@ test-debug: cmake --build build/test-debug --config=Debug --parallel=$(CPU_CORES) $(MAKE) test -configure: - @echo "Running Config: release" - CONAN_HOME=$(CONAN_CACHE) conan install . --output-folder=build/release --build=missing -s build_type=$(BUILD_TYPE) - cmake -S . -B build/release -DCMAKE_TOOLCHAIN_FILE=build/release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSTATIC=$(STATIC) -DTESTNET=$(TESTNET) -DBUILD_VERSION=$(BUILD_VERSION) docs: configure @echo "Building Documentation" @@ -162,29 +176,6 @@ docs-dev: configure @echo "Building Documentation" cmake --build build/release --target=serve_docs --config=Release -docker-chain-node: - @echo "Building docker image: lthn/chain" - docker buildx build -f $(CC_DOCKER_FILE) -t lthn/chain $(CURDIR) - -docker-cc-linux-amd64: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-amd64 --platform linux/amd64 $(CURDIR) - -docker-cc-linux-armv7: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-armv7 --platform linux/arm/v7 $(CURDIR) - -docker-cc-linux-arm64v8: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-arm64v8 --platform linux/arm64/v8 $(CURDIR) - -docker-cc-linux-ppc64le: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-ppc64le --platform linux/ppc64le $(CURDIR) - -docker-cc-linux-riscv64: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-riscv64 --platform linux/riscv64 $(CURDIR) - -docker-cc-linux-s390x: - docker buildx build -f $(CC_DOCKER_FILE) --target build-artifacts --output type=local,dest=build/cc-linux-s390x --platform linux/s390x $(CURDIR) - - clean: rm -rf build diff --git a/conanfile.py b/conanfile.py index efed9a7b..ac4c1ec5 100644 --- a/conanfile.py +++ b/conanfile.py @@ -33,6 +33,12 @@ class BlockchainConan(ConanFile): tc.variables["TESTNET"] = self.options.testnet # tc.preprocessor_definitions["TESTNET"] = None # tc.variables["BUILD_VERSION"] = self.options.build_version + if self.settings.get_safe("compiler") == "msvc": + if self.options.static: + tc.variables["CMAKE_MSVC_RUNTIME_LIBRARY"] = "MultiThreaded$<$:Debug>" + else: + tc.variables["CMAKE_MSVC_RUNTIME_LIBRARY"] = "MultiThreadedDLL$<$:Debug>" + tc.generate() deps = CMakeDeps(self) diff --git a/utils/docker/images/lthn-chain/Dockerfile b/utils/docker/images/lthn-chain/Dockerfile index 364c8f64..cc6820a1 100644 --- a/utils/docker/images/lthn-chain/Dockerfile +++ b/utils/docker/images/lthn-chain/Dockerfile @@ -1,23 +1,12 @@ # use --target=builder to return a docker image able to compile the software, probbly used with -v .:/code FROM ubuntu:24.04 AS builder LABEL authors="snider" - ARG BUILD_THREADS=1 -ARG BUILD_BRANCH=dev -ARG BUILD_LOCAL=1 -ARG BUILD_STATIC=0 -ARG BUILD_REPO=https://github.com/letheanVPN/blockchain.git -ARG BUILD_TARGET=gcc-linux-x86_64 -ARG BUILD_FOLDER=build/release -ARG BUILD_TYPE=Release ARG BUILD_TESTNET=1 -ARG USE_CUSTOM_PROFILE=0 - - +ARG BUILD_STATIC=0 ENV CONAN_HOME=/root/sdk # CONAN: disables the generation of color escape characters. ENV NO_COLOR=1 -ENV BUILD_TARGET=${BUILD_TARGET} ENV DEBIAN_FRONTEND=noninteractive RUN apt update && apt -y upgrade @@ -32,18 +21,32 @@ RUN pip3 install conan --break-system-packages WORKDIR / # Copy the build context. -COPY . /tmp/local-src -RUN if [ "$BUILD_LOCAL" = "1" ]; then \ - mv /tmp/local-src /code; \ - else \ - rm -rf /tmp/local-src; \ - git clone --recursive --branch ${BUILD_BRANCH} ${BUILD_REPO} code; \ - fi +COPY . /code VOLUME /code/build/sdk +RUN if [ "$BUILD_LOCAL" = "0" ]; then \ + rm -rf /code; \ + git clone --recursive --branch ${BUILD_BRANCH} ${BUILD_REPO} code; \ + fi + WORKDIR /code +RUN make configure CPU_CORES=${BUILD_THREADS} TESTNET=${BUILD_TESTNET} STATIC=${BUILD_STATIC} + + +FROM builder AS build +ARG BUILD_THREADS=1 +ARG BUILD_BRANCH=dev +ARG BUILD_LOCAL=1 +ARG BUILD_STATIC=0 +ARG BUILD_REPO=https://github.com/letheanVPN/blockchain.git +ARG BUILD_FOLDER=build/release +ARG BUILD_TYPE=Release +ARG BUILD_TESTNET=1 + + + RUN make release CPU_CORES=${BUILD_THREADS} TESTNET=${BUILD_TESTNET} STATIC=${BUILD_STATIC} #RUN cmake --build ${BUILD_FOLDER} --config=${BUILD_TYPE} --parallel=${THREADS} @@ -56,13 +59,9 @@ RUN if [ "$BUILD_TESTNET" = "1" ]; then \ done; \ fi -# use --target=build-cache to return just the cache files -FROM scratch AS build-cache -COPY --from=builder ${CONAN_HOME} / - # use --target=build-artifacts to return the binaries FROM scratch AS build-artifacts -COPY --from=builder /code/build/release/src/lethean-* / +COPY --from=build /code/build/release/src/lethean-* / # use --target=chain-service to return a working chain node FROM ubuntu:24.04 AS chain-service