From 85793c7c6fee6f6f284cd8cfb22f5028724816d1 Mon Sep 17 00:00:00 2001 From: Scott Merchant Date: Mon, 18 Mar 2024 12:02:11 +1030 Subject: [PATCH] Move bootstrapBuild into Dockerfile --- .nuke/build.schema.json | 4 -- build/Build.Pack.cs | 11 +--- docker/kubernetes-tentacle/Dockerfile | 25 ++++++++- .../bootstrapRunner/build.sh | 51 ------------------- 4 files changed, 25 insertions(+), 66 deletions(-) delete mode 100755 docker/kubernetes-tentacle/bootstrapRunner/build.sh diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 19289ddad..02f70b2c6 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -38,10 +38,6 @@ "type": "string", "description": "Specifies the platforms to build the docker images in. Multiple platforms must be comma-separated. Defaults to 'linux/arm64,linux/amd64'" }, - "GolangContainerImageTag": { - "type": "string", - "description": "The tag to use when building the bootstrapRunner with the Golang Container Image" - }, "Help": { "type": "boolean", "description": "Shows the help text for this build assembly" diff --git a/build/Build.Pack.cs b/build/Build.Pack.cs index 8c377af09..664e56b64 100644 --- a/build/Build.Pack.cs +++ b/build/Build.Pack.cs @@ -33,8 +33,6 @@ partial class Build [Parameter("The version of upx to use when building the bootstrapRunner executable for the Kubernetes Tentacle")] string UpxVersion = "4.2.2"; - [Parameter("The tag to use when building the bootstrapRunner with the Golang Container Image")] string GolangContainerImageTag = "1.22"; - [PublicAPI] Target PackOsxTarballs => _ => _ .Description("Packs the OS/X tarballs containing the published binaries.") @@ -548,13 +546,6 @@ void PackTarballs(string runtimeId) void BuildAndPushOrLoadKubernetesTentacleContainerImage(bool push, bool load, string? host = null, bool includeDebugger = false, bool useUpx = true) { - DockerTasks.DockerRun(settings => settings.EnableRm() - .SetVolume("./docker/kubernetes-tentacle/bootstrapRunner:/usr/src/bootstrapRunner") - .SetWorkdir("/usr/src/bootstrapRunner") - .SetEnv($"UPX_VERSION={UpxVersion}", $"PLATFORMS={DockerPlatform}", $"USE_UPX={useUpx.ToString().ToLowerInvariant()}") - .SetImage($"golang:{GolangContainerImageTag}") - .SetCommand("./build.sh")); - var hostPrefix = host is not null ? $"{host}/" : string.Empty; DockerTasks.DockerBuildxBuild(settings => { @@ -573,7 +564,7 @@ void BuildAndPushOrLoadKubernetesTentacleContainerImage(bool push, bool load, st tag += "-debug"; settings = settings - .AddBuildArg($"BUILD_NUMBER={FullSemVer}", $"BUILD_DATE={DateTime.UtcNow:O}") + .AddBuildArg($"BUILD_NUMBER={FullSemVer}", $"BUILD_DATE={DateTime.UtcNow:O}", $"UPX_VERSION={UpxVersion}") .SetPlatform(DockerPlatform) .SetTag(tag) .SetFile(dockerfile) diff --git a/docker/kubernetes-tentacle/Dockerfile b/docker/kubernetes-tentacle/Dockerfile index 788c6ebe7..f3c843aaf 100644 --- a/docker/kubernetes-tentacle/Dockerfile +++ b/docker/kubernetes-tentacle/Dockerfile @@ -1,3 +1,26 @@ +FROM golang:1.22 as bootstrapRunnerBuilder + +ARG TARGETARCH +ARG TARGETOS +ARG UPX_VERSION="4.2.2" + +COPY docker/kubernetes-tentacle/bootstrapRunner/* /bootstrapRunner/ +WORKDIR /bootstrapRunner + +# need to install xz-utils to unpack upx +RUN apt update +RUN apt install -y xz-utils + +#download upx and unpack +RUN curl -OL "https://github.com/upx/upx/releases/download/v${UPX_VERSION}/upx-${UPX_VERSION}-${TARGETARCH}_${TARGETOS}.tar.xz" +RUN tar -x -f "upx-${UPX_VERSION}-${TARGETARCH}_${TARGETOS}.tar.xz" + +# build bootstrapRunner +# Note: the given ldflags remove debug symbols +RUN go build -ldflags "-s -w" -o "bin/bootstrapRunner" +# upx reduces the size of the exe +RUN "./upx-${UPX_VERSION}-${TARGETARCH}_${TARGETOS}/upx" "bin/bootstrapRunner" + FROM mcr.microsoft.com/dotnet/runtime-deps:6.0 ARG BUILD_NUMBER @@ -9,7 +32,7 @@ ARG TARGETVARIANT EXPOSE 10933 COPY docker/kubernetes-tentacle/scripts/* /scripts/ -COPY docker/kubernetes-tentacle/bootstrapRunner/bin/bootstrapRunner-${TARGETOS}-${TARGETARCH} /bootstrapRunner +COPY --from=bootstrapRunnerBuilder bootstrapRunner/bin/bootstrapRunner /bootstrapRunner RUN chmod +x /scripts/*.sh WORKDIR /tmp diff --git a/docker/kubernetes-tentacle/bootstrapRunner/build.sh b/docker/kubernetes-tentacle/bootstrapRunner/build.sh deleted file mode 100755 index 7b310fe41..000000000 --- a/docker/kubernetes-tentacle/bootstrapRunner/build.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - - # upx is a tool which reduces the size of the go executable. - # unfortunately there is a not a good container for it and - # it's not available to download via apt-get so we direct - # download it. -if ${USE_UPX}; then - TARGETARCH="$(dpkg --print-architecture)"; - TARGETOS="linux" - - apt update - apt install -y xz-utils - - tarName="upx-${UPX_VERSION}-${TARGETARCH}_${TARGETOS}"; - fullTarName="$tarName.tar.xz" - url="https://github.com/upx/upx/releases/download/v${UPX_VERSION}/$fullTarName"; - - echo "Downloading upx from $url"; - curl -OL "$url"; - - echo "Unpacking upx" - tar -x -f "$fullTarName" -fi - -for platform in ${PLATFORMS//,/$IFS}; do - # ${platform%/*} removes the first string matching the regex /* - # from the end of the string in the $platform (eg: /amd64 in 'linux/amd64') - export GOOS=${platform%/*} - - # ${platform#*/} removes the first string matching the regex */ - # from the start of the string in $platform (eg: linux/ in 'linux/arm64') - export GOARCH=${platform#*/} - - exeName="bootstrapRunner-$GOOS-$GOARCH" - - echo "Building BootstrapRunner for $platform" - # the given ldflags remove debug symbols - go build -ldflags "-s -w" -o "./bin/$exeName" - - if ${USE_UPX}; then - echo "Compressing executable $exeName with upx" - "./$tarName/upx" "./bin/$exeName" - fi -done - -if ${USE_UPX}; then - echo "Cleaning up upx files" - # cleanup upx files - rm -rf "./$tarName" - rm "./$fullTarName" -fi \ No newline at end of file