diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 200626ed2..1d644b681 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -27,8 +27,10 @@ "BuildAll", "BuildAndLoadLocalDebugKubernetesTentacleContainerImage", "BuildAndLoadLocallyKubernetesTentacleContainerImage", + "BuildAndLoadLocallyLinuxTentacleContainerImage", "BuildAndPushForMicrok8sKubernetesTentacleContainerImage", "BuildAndPushKubernetesTentacleContainerImage", + "BuildAndPushLinuxTentacleContainerImage", "BuildLinux", "BuildOsx", "BuildSoftwareBillOfMaterials", @@ -197,7 +199,7 @@ }, "TentacleLinuxDockerImage": { "type": "string", - "description": "The Linux Tentacle image for TestLinuxDockerImage to test. Defaults to the tag docker-compose.build.yml gives the image for this version" + "description": "The Linux Tentacle image for TestLinuxDockerImage to test. If not given, the tests use the TentacleDockerTests_ImageAndTag or TentacleDockerTests_ImageTag environment variable" }, "TestFilter": { "type": "string" diff --git a/build/Build.Pack.cs b/build/Build.Pack.cs index f1bacc275..9a7ded094 100644 --- a/build/Build.Pack.cs +++ b/build/Build.Pack.cs @@ -215,6 +215,29 @@ void CreateLinuxPackages(string runtimeId) BuildAndPushOrLoadKubernetesTentacleContainerImage(push: false, load: true, KubernetesTentacleContainerRuntimeDepsTag, includeDebugger: true); }); + [PublicAPI] + Target BuildAndPushLinuxTentacleContainerImage => _ => _ + .Description("Builds the Linux Tentacle container image for every DockerPlatform, and pushes it as -linux and ") + .Executes(() => + { + BuildLinuxTentacleContainerImage(push: true, DockerPlatform); + }); + + [PublicAPI] + Target BuildAndLoadLocallyLinuxTentacleContainerImage => _ => _ + .Description("Builds the Linux Tentacle container image for one platform, and loads it into the local Docker. Uses the host's architecture unless DockerPlatform names a single platform.") + .OnlyWhenStatic(() => IsLocalBuild) + .DependsOn(PackDebianPackage) + .Executes(() => + { + // Docker can only load one platform's image, so fall back to the host's architecture when DockerPlatform names several. + var platform = DockerPlatform.Contains(',') + ? RuntimeInformation.OSArchitecture == Architecture.Arm64 ? "linux/arm64" : "linux/amd64" + : DockerPlatform; + + BuildLinuxTentacleContainerImage(push: false, platform); + }); + [PublicAPI] Target PackRedHatPackage => _ => _ .Description("TODO: Move .rpm creation into this task") @@ -770,6 +793,54 @@ void BuildAndPushOrLoadKubernetesTentacleContainerImage(bool push, bool load, st }); } + void BuildLinuxTentacleContainerImage(bool push, string platform) + { + const string repository = "docker.packages.octopushq.com/octopusdeploy/tentacle"; + + // is the tag Octopus publishes to Docker Hub. It used to be a manifest list made by a separate TeamCity build. + var tags = push + ? new[] { $"{repository}:{FullSemVer}-linux", $"{repository}:{FullSemVer}" } + : new[] { $"{repository}:{FullSemVer}-linux" }; + + // Docker's default buildx builder can't build an image for several platforms, so create a temporary one + // unless a builder was given. + var builder = DockerBuildxBuilder; + var createTemporaryBuilder = builder is null && platform.Contains(','); + if (createTemporaryBuilder) + { + builder = $"tentacle-linux-image-{Guid.NewGuid():N}"; + ProcessTasks.StartProcess("docker", $"buildx create --name {builder} --driver docker-container").AssertZeroExitCode(); + } + + try + { + DockerTasks.DockerBuildxBuild(settings => + { + if (builder is not null) + { + settings = settings.SetBuilder(builder); + } + + return settings + .AddBuildArg($"BUILD_NUMBER={FullSemVer}", $"BUILD_DATE={DateTime.UtcNow:O}") + .SetPlatform(platform) + .SetTag(tags) + .SetFile("./docker/linux/Dockerfile") + .SetPath(RootDirectory) + .SetPull(true) + .SetPush(push) + .SetLoad(!push); + }); + } + finally + { + if (createTemporaryBuilder) + { + ProcessTasks.StartProcess("docker", $"buildx rm --force {builder}").AssertZeroExitCode(); + } + } + } + void CopyDebianPackageToDockerFolder(string runtimeId) { if (!DebDockerMap.TryGetValue(runtimeId, out var debDockerArch)) return; diff --git a/docker-compose.build.yml b/docker-compose.build.yml index 75408a939..b18dd22e3 100644 --- a/docker-compose.build.yml +++ b/docker-compose.build.yml @@ -1,13 +1,17 @@ version: '3' services: + # Builds one architecture (amd64 unless BUILD_ARCH says otherwise). + # The published multi-architecture image is built by the BuildAndPushLinuxTentacleContainerImage NUKE target. octopusdeploy-tentacle-linux: + platform: "linux/${BUILD_ARCH:-amd64}" build: context: . dockerfile: ./docker/linux/Dockerfile args: BUILD_DATE: ${BUILD_DATE:?err} BUILD_NUMBER: ${BUILD_NUMBER:?err} + TARGETARCH: ${BUILD_ARCH:-amd64} image: docker.packages.octopushq.com/octopusdeploy/tentacle:${BUILD_NUMBER?err}-linux octopusdeploy-kubernetes-agent-tentacle-linux: diff --git a/docker/linux/Dockerfile b/docker/linux/Dockerfile index af5fe799f..cef2256d9 100644 --- a/docker/linux/Dockerfile +++ b/docker/linux/Dockerfile @@ -44,9 +44,12 @@ COPY docker/linux/scripts/dockerd-entrypoint.sh /usr/local/bin/ RUN /install-scripts/install-docker.sh # Install Tentacle -COPY _artifacts/deb/tentacle_${BUILD_NUMBER}_amd64.deb /tmp/ +# BuildKit sets TARGETARCH (amd64 or arm64) for each platform being built. +# PackDebianPackage copies each architecture's package to _artifacts/docker/ under this name. +ARG TARGETARCH +COPY _artifacts/docker/tentacle_${BUILD_NUMBER}_linux-${TARGETARCH}.deb /tmp/tentacle.deb RUN apt-get update && \ - apt-get install -y --no-install-recommends ./tentacle_${BUILD_NUMBER}_amd64.deb && \ + apt-get install -y --no-install-recommends ./tentacle.deb && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* && \ ln -s /opt/octopus/tentacle/Tentacle /usr/bin/tentacle diff --git a/testing/docker-linux/build-and-test-linux-docker-image.sh b/testing/docker-linux/build-and-test-linux-docker-image.sh index 78fd391a2..f86392964 100755 --- a/testing/docker-linux/build-and-test-linux-docker-image.sh +++ b/testing/docker-linux/build-and-test-linux-docker-image.sh @@ -6,11 +6,13 @@ # end to end, the same way TeamCity does: # # TeamFireAndMotion_OctopusTentacle_TentacleVLatest -# -> Build: Tentacle (Linux) (produces _artifacts/deb/tentacle__amd64.deb) -# -> Build: Linux Docker image (docker-compose -f docker-compose.build.yml build ...) +# -> Build: Tentacle (Linux) (produces _artifacts/docker/tentacle__linux-.deb) +# -> Build: Linux Docker image (builds docker/linux/Dockerfile for amd64 and arm64) +# +# It tests one architecture per run, chosen with --platform. # # Stages: -# 1. deb - build the linux-x64 .deb via NUKE (--target PackDebianPackage) +# 1. deb - build the .deb for that architecture via NUKE (--target PackDebianPackage) # 2. image - build docker/linux/Dockerfile via docker-compose.build.yml # 3. smoke - assert the image's contents and behaviour # 4. e2e - stand up a real Octopus Server and point a listening and a @@ -39,7 +41,10 @@ # # Usage: ./testing/docker-linux/build-and-test-linux-docker-image.sh [options] # (runs from anywhere; it resolves the repo root itself) -# --skip-deb Reuse the newest existing _artifacts/deb/tentacle_*_amd64.deb +# --platform P The image's platform: linux/amd64 (the default) or linux/arm64. +# The SQL Server and Octopus Server containers in stage 4 always +# run as linux/amd64, as they have no arm64 images. +# --skip-deb Reuse the newest existing _artifacts/docker/tentacle_*_linux-.deb # --skip-image Reuse the already-built image for the resolved BUILD_NUMBER # --skip-smoke Skip the image smoke tests # --skip-e2e Skip the Octopus Server registration test @@ -51,7 +56,7 @@ set -euo pipefail # This script lives in testing/docker-linux/, but every path below (./build.sh, -# _artifacts/deb, docker-compose.build.yml, docker/linux/Dockerfile) is relative +# _artifacts/docker, docker-compose.build.yml, docker/linux/Dockerfile) is relative # to the repo root, so resolve that and work from there. REPO_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) cd "$REPO_DIR" @@ -64,6 +69,7 @@ SKIP_SMOKE=0 SKIP_E2E=0 KEEP=0 NO_1PASSWORD=0 +PLATFORM="linux/amd64" while [[ $# -gt 0 ]]; do case "$1" in @@ -73,6 +79,10 @@ while [[ $# -gt 0 ]]; do --skip-e2e) SKIP_E2E=1 ;; --keep) KEEP=1 ;; --no-1password) NO_1PASSWORD=1 ;; + --platform) + [[ $# -ge 2 ]] || { echo "--platform needs a value: linux/amd64 or linux/arm64" >&2; exit 2; } + PLATFORM="$2" + shift ;; # Print the header comment block, minus the shebang, as the help text. -h|--help) awk 'NR>1 { if (/^#/) { sub(/^# ?/, ""); print } else { exit } }' "${BASH_SOURCE[0]}" @@ -134,16 +144,30 @@ assert_equals() { # Host platform ############################################################################### -# The .deb we build (and therefore the image) is amd64. On an arm64 host -# (Apple Silicon) Docker would otherwise default to arm64 and the `apt install` -# of the .deb would fail, so pin the platform for every build and run. -PLATFORM="linux/amd64" +# The image is built and run for --platform, whatever the host is, so that the +# .deb's architecture always matches the image's. When they differ from the host, +# Docker runs them under emulation. +case "$PLATFORM" in + linux/amd64) ARCH=amd64; RUNTIME_ID=linux-x64 ;; + linux/arm64) ARCH=arm64; RUNTIME_ID=linux-arm64 ;; + *) die "unsupported --platform '$PLATFORM': use linux/amd64 or linux/arm64" ;; +esac export DOCKER_DEFAULT_PLATFORM="$PLATFORM" + +# SQL Server and Octopus Server only publish amd64 images, so stage 4 always runs +# them as amd64, under emulation on an arm64 host. +SERVER_PLATFORM="linux/amd64" + # Suppress Docker Scout hints, which are written to stderr and look like errors. export DOCKER_CLI_HINTS=false -if [[ "$(uname -m)" == "arm64" || "$(uname -m)" == "aarch64" ]]; then - info "Host is $(uname -m); building and running everything as $PLATFORM under emulation." +case "$(uname -m)" in + x86_64|amd64) HOST_ARCH=amd64 ;; + arm64|aarch64) HOST_ARCH=arm64 ;; + *) HOST_ARCH=$(uname -m) ;; +esac +if [[ "$HOST_ARCH" != "$ARCH" ]]; then + info "Host is $(uname -m); building and running the image as $PLATFORM under emulation." fi command -v docker >/dev/null || die "docker is not on PATH" @@ -154,22 +178,22 @@ docker info >/dev/null 2>&1 || die "the Docker daemon is not running" ############################################################################### find_deb() { - # Newest tentacle_*_amd64.deb, or empty if there are none. + # Newest tentacle_*_linux-.deb, or empty if there are none. # # The trailing `|| true` is what makes "or empty" true: with no match the # glob is passed through literally, `ls` fails, and under `set -o pipefail` # the caller's `DEB_PATH=$(find_deb)` would abort the script before it could # reach the `die` that explains what is missing. - ls -t _artifacts/deb/tentacle_*_amd64.deb 2>/dev/null | head -1 || true + ls -t _artifacts/docker/tentacle_*_linux-"${ARCH}".deb 2>/dev/null | head -1 || true } if [[ $SKIP_DEB -eq 1 ]]; then step "Stage 1/4: .deb (skipped, reusing existing)" DEB_PATH=$(find_deb) - [[ -n "$DEB_PATH" ]] || die "--skip-deb was given but no _artifacts/deb/tentacle_*_amd64.deb exists" + [[ -n "$DEB_PATH" ]] || die "--skip-deb was given but no _artifacts/docker/tentacle_*_linux-${ARCH}.deb exists" else - step "Stage 1/4: building the linux-x64 .deb via NUKE" - info "This runs 'PackDebianPackage', which cross-compiles Tentacle for linux-x64" + step "Stage 1/4: building the $RUNTIME_ID .deb via NUKE" + info "This runs 'PackDebianPackage', which cross-compiles Tentacle for $RUNTIME_ID" info "and packages it inside the tool-linux-packages container. Takes a few minutes." # NUKE resolves every [ParameterFromPasswordStore] at start-up, which shells out @@ -177,18 +201,18 @@ else # 'op' call hangs when it cannot prompt interactively, so opt out. export OCTOPUS__Tests__SecretManagerEnabled=False - # Only linux-x64 is needed for docker/linux/Dockerfile; building every runtime - # ID would take far longer for no benefit. - ./build.sh --target PackDebianPackage --runtime-ids linux-x64 + # Only this platform's runtime ID is needed for docker/linux/Dockerfile; + # building every runtime ID would take far longer for no benefit. + ./build.sh --target PackDebianPackage --runtime-ids "$RUNTIME_ID" DEB_PATH=$(find_deb) - [[ -n "$DEB_PATH" ]] || die "PackDebianPackage finished but no .deb landed in _artifacts/deb/" + [[ -n "$DEB_PATH" ]] || die "PackDebianPackage finished but no .deb landed in _artifacts/docker/" fi DEB_FILE=$(basename "$DEB_PATH") -# tentacle__amd64.deb +# tentacle__linux-.deb BUILD_NUMBER="${DEB_FILE#tentacle_}" -BUILD_NUMBER="${BUILD_NUMBER%_amd64.deb}" +BUILD_NUMBER="${BUILD_NUMBER%_linux-${ARCH}.deb}" [[ -n "$BUILD_NUMBER" ]] || die "could not derive BUILD_NUMBER from $DEB_FILE" # A local NUKE build appends '-' to FullSemVer for the package @@ -212,7 +236,7 @@ IMAGE="docker.packages.octopushq.com/octopusdeploy/tentacle:${BUILD_NUMBER}-linu BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) export BUILD_NUMBER BUILD_DATE -export BUILD_ARCH=amd64 +export BUILD_ARCH="$ARCH" if [[ $SKIP_IMAGE -eq 1 ]]; then step "Stage 2/4: image (skipped, reusing existing)" @@ -834,7 +858,7 @@ if [[ $SKIP_E2E -eq 0 ]]; then # --- SQL Server ------------------------------------------------------- info "Starting SQL Server..." - docker run -d --name "$E2E_SQL" --platform "$PLATFORM" --network "$E2E_NET" \ + docker run -d --name "$E2E_SQL" --platform "$SERVER_PLATFORM" --network "$E2E_NET" \ --env-file "$E2E_ENV_SQL" \ -e ACCEPT_EULA=Y \ -e MSSQL_PID=Express \ @@ -851,7 +875,7 @@ if [[ $SKIP_E2E -eq 0 ]]; then # --- Octopus Server --------------------------------------------------- info "Starting Octopus Server (this takes several minutes on first run)..." - docker run -d --name "$E2E_SERVER" --platform "$PLATFORM" --network "$E2E_NET" \ + docker run -d --name "$E2E_SERVER" --platform "$SERVER_PLATFORM" --network "$E2E_NET" \ --env-file "$E2E_ENV_SERVER" \ -e ACCEPT_EULA=Y \ -e "ADMIN_USERNAME=${OCTOPUS_USER}" \