Skip to content

Commit fbc6f6e

Browse files
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 <ericornelissen@gmail.com>
1 parent 8ee9d13 commit fbc6f6e

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Containerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2023-2024 Eric Cornelissen
1+
# Copyright (C) 2023-2025 Eric Cornelissen
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -13,7 +13,8 @@
1313
# You should have received a copy of the GNU General Public License
1414
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

16-
FROM docker.io/golang:1.23.0 AS build
16+
ARG GO_VERSION=invalid
17+
FROM docker.io/golang:${GO_VERSION} AS build
1718

1819
WORKDIR /src
1920

Containerfile.dev

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT No Attribution
22
#
3-
# Copyright (c) 2023-2024 Eric Cornelissen
3+
# Copyright (c) 2023-2025 Eric Cornelissen
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -17,7 +17,8 @@
1717
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1818
# SOFTWARE.
1919

20-
FROM docker.io/golang:1.23.0-alpine3.19
20+
ARG GO_VERSION=invalid
21+
FROM docker.io/golang:${GO_VERSION}-alpine3.19
2122

2223
RUN apk add --no-cache \
2324
bash git perl-utils zip \

tasks.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// MIT No Attribution
22
//
3-
// Copyright (c) 2024 Eric Cornelissen
3+
// Copyright (c) 2024-2025 Eric Cornelissen
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy
66
// of this software and associated documentation files (the "Software"), to deal
@@ -34,6 +34,7 @@ import (
3434
"os"
3535
"os/exec"
3636
"regexp"
37+
"runtime"
3738
"strings"
3839
)
3940

@@ -43,16 +44,20 @@ const (
4344
ENV_CONTAINER_TAG = "CONTAINER_TAG"
4445
)
4546

46-
var (
47+
const (
4748
buildAllDir = "_compiled"
4849
webDir = "web"
4950
)
5051

51-
var (
52+
const (
5253
permFile fs.FileMode = 0o664
5354
permDir fs.FileMode = 0o755
5455
)
5556

57+
var (
58+
GO_VERSION = runtime.Version()[2:]
59+
)
60+
5661
// Audit the codebase.
5762
func TaskAudit(t *T) error {
5863
return t.Run(
@@ -218,8 +223,9 @@ func TaskContainer(t *T) error {
218223
engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE)
219224
tag = t.Env(ENV_CONTAINER_TAG, "latest")
220225
build = fmt.Sprintf(
221-
"%s build --file Containerfile --tag ericornelissen/ades:%s .",
226+
"%s build --build-arg GO_VERSION=%s --file Containerfile --tag ericornelissen/ades:%s .",
222227
engine,
228+
GO_VERSION,
223229
tag,
224230
)
225231
)
@@ -261,8 +267,9 @@ func TaskDevImg(t *T) error {
261267
var (
262268
engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE)
263269
build = fmt.Sprintf(
264-
"%s build --file 'Containerfile.dev' --tag ades-dev-img .",
270+
"%s build --build-arg GO_VERSION=%s --file Containerfile.dev --tag ades-dev-img .",
265271
engine,
272+
GO_VERSION,
266273
)
267274
)
268275

@@ -444,28 +451,28 @@ func TaskReproducibleContainer(t *T) error {
444451
engine = t.Env(ENV_CONTAINER_ENGINE, DEFAULT_CONTAINER_ENGINE)
445452
tag1 = "docker.io/ericornelissen/ades:a"
446453
tag2 = "docker.io/ericornelissen/ades:b"
447-
buildCmd = "%s build --no-cache --file Containerfile --tag %s ."
454+
buildCmd = "%s build --no-cache --build-arg GO_VERSION=%s --file Containerfile --tag %s ."
448455
removeCmd = "%s rmi %s"
449456
)
450457

451458
t.Log("Initial container build...")
452-
cmd := fmt.Sprintf(buildCmd, engine, tag1)
459+
cmd := fmt.Sprintf(buildCmd, engine, GO_VERSION, tag1)
453460
if err := t.Exec(cmd); err != nil {
454461
return err
455462
}
456463

464+
defer func() {
465+
_ = t.ExecF(io.Discard, fmt.Sprintf(removeCmd, engine, tag1))
466+
}()
467+
457468
t.Log("Reproducing container build...")
458-
cmd = fmt.Sprintf(buildCmd, engine, tag2)
469+
cmd = fmt.Sprintf(buildCmd, engine, GO_VERSION, tag2)
459470
if err := t.Exec(cmd); err != nil {
460471
return err
461472
}
462473

463474
defer func() {
464-
_ = t.ExecF(
465-
io.Discard,
466-
fmt.Sprintf(removeCmd, engine, tag1),
467-
fmt.Sprintf(removeCmd, engine, tag2),
468-
)
475+
_ = t.ExecF(io.Discard, fmt.Sprintf(removeCmd, engine, tag2))
469476
}()
470477

471478
t.Log("Check...")

0 commit comments

Comments
 (0)