Skip to content

Commit

Permalink
Synchronize local Go version with container images Go version
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ericcornelissen committed Jan 11, 2025
1 parent 8ee9d13 commit fbc6f6e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,7 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

FROM docker.io/golang:1.23.0 AS build
ARG GO_VERSION=invalid
FROM docker.io/golang:${GO_VERSION} AS build

WORKDIR /src

Expand Down
5 changes: 3 additions & 2 deletions Containerfile.dev
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 \
Expand Down
33 changes: 20 additions & 13 deletions tasks.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,6 +34,7 @@ import (
"os"
"os/exec"
"regexp"
"runtime"
"strings"
)

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

Expand Down Expand Up @@ -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...")
Expand Down

0 comments on commit fbc6f6e

Please sign in to comment.