From fbc6f6ef3ec041bc6296506007a4d19c4668c152 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Fri, 3 Jan 2025 12:38:34 +0100 Subject: [PATCH] Synchronize local Go version with container images Go version Update the `Containerfile`s to accept a Go version as build argument and use it for the base image. The version must be specified at build time (otherwise the default value "invalid" causes the build to fail) which is done by the relevant tasks defined in `tasks.go`. These tasks use the `runtime` package's Version function to determine the current Go version and specify it as a build argument when building container images. Signed-off-by: Eric Cornelissen --- Containerfile | 5 +++-- Containerfile.dev | 5 +++-- tasks.go | 33 ++++++++++++++++++++------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Containerfile b/Containerfile index bf21fe9..86242ce 100644 --- a/Containerfile +++ b/Containerfile @@ -1,4 +1,4 @@ -# Copyright (C) 2023-2024 Eric Cornelissen +# Copyright (C) 2023-2025 Eric Cornelissen # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -13,7 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -FROM docker.io/golang:1.23.0 AS build +ARG GO_VERSION=invalid +FROM docker.io/golang:${GO_VERSION} AS build WORKDIR /src diff --git a/Containerfile.dev b/Containerfile.dev index d5f61f8..ff7759f 100644 --- a/Containerfile.dev +++ b/Containerfile.dev @@ -1,6 +1,6 @@ # MIT No Attribution # -# Copyright (c) 2023-2024 Eric Cornelissen +# Copyright (c) 2023-2025 Eric Cornelissen # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -17,7 +17,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -FROM docker.io/golang:1.23.0-alpine3.19 +ARG GO_VERSION=invalid +FROM docker.io/golang:${GO_VERSION}-alpine3.19 RUN apk add --no-cache \ bash git perl-utils zip \ diff --git a/tasks.go b/tasks.go index 624e5d1..5c25d86 100644 --- a/tasks.go +++ b/tasks.go @@ -1,6 +1,6 @@ // MIT No Attribution // -// Copyright (c) 2024 Eric Cornelissen +// Copyright (c) 2024-2025 Eric Cornelissen // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -34,6 +34,7 @@ import ( "os" "os/exec" "regexp" + "runtime" "strings" ) @@ -43,16 +44,20 @@ const ( ENV_CONTAINER_TAG = "CONTAINER_TAG" ) -var ( +const ( buildAllDir = "_compiled" webDir = "web" ) -var ( +const ( permFile fs.FileMode = 0o664 permDir fs.FileMode = 0o755 ) +var ( + GO_VERSION = runtime.Version()[2:] +) + // Audit the codebase. func TaskAudit(t *T) error { return t.Run( @@ -218,8 +223,9 @@ func TaskContainer(t *T) error { engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE) tag = t.Env(ENV_CONTAINER_TAG, "latest") build = fmt.Sprintf( - "%s build --file Containerfile --tag ericornelissen/ades:%s .", + "%s build --build-arg GO_VERSION=%s --file Containerfile --tag ericornelissen/ades:%s .", engine, + GO_VERSION, tag, ) ) @@ -261,8 +267,9 @@ func TaskDevImg(t *T) error { var ( engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE) build = fmt.Sprintf( - "%s build --file 'Containerfile.dev' --tag ades-dev-img .", + "%s build --build-arg GO_VERSION=%s --file Containerfile.dev --tag ades-dev-img .", engine, + GO_VERSION, ) ) @@ -444,28 +451,28 @@ func TaskReproducibleContainer(t *T) error { engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE) tag1 = "docker.io/ericornelissen/ades:a" tag2 = "docker.io/ericornelissen/ades:b" - buildCmd = "%s build --no-cache --file Containerfile --tag %s ." + buildCmd = "%s build --no-cache --build-arg GO_VERSION=%s --file Containerfile --tag %s ." removeCmd = "%s rmi %s" ) t.Log("Initial container build...") - cmd := fmt.Sprintf(buildCmd, engine, tag1) + cmd := fmt.Sprintf(buildCmd, engine, GO_VERSION, tag1) if err := t.Exec(cmd); err != nil { return err } + defer func() { + _ = t.ExecF(io.Discard, fmt.Sprintf(removeCmd, engine, tag1)) + }() + t.Log("Reproducing container build...") - cmd = fmt.Sprintf(buildCmd, engine, tag2) + cmd = fmt.Sprintf(buildCmd, engine, GO_VERSION, tag2) if err := t.Exec(cmd); err != nil { return err } defer func() { - _ = t.ExecF( - io.Discard, - fmt.Sprintf(removeCmd, engine, tag1), - fmt.Sprintf(removeCmd, engine, tag2), - ) + _ = t.ExecF(io.Discard, fmt.Sprintf(removeCmd, engine, tag2)) }() t.Log("Check...")