From c3736ae41a02723878522aabb096e67db8dc73f2 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 1 Sep 2026 22:14:16 +0000 Subject: [PATCH] Dockerfile: use our own Go toolchain, not the golang Docker image The golang:N-alpine Docker image lags behind Go minor releases by a day or two. When we bump go.mod to a new minor version before the image catches up, the required "Build Docker image" CI check fails with "go.mod requires go >= 1.27.1 (running go 1.27.0; GOTOOLCHAIN=local)" and blocks the toolchain bump from merging. Instead, base the build stage on plain alpine and download the Tailscale Go toolchain release for the revision in go.toolchain.rev, matching how everything else in this repo is built. Fixes #21072 Signed-off-by: Brad Fitzpatrick Change-Id: I1ccb77fa3c7a49532ea87dbdbf9e3340880ec94e --- Dockerfile | 26 +++++++++++++++++++++++++- docs/go-bump-checklist.md | 1 - version/version_test.go | 4 +++- version_test.go | 28 +++------------------------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Dockerfile b/Dockerfile index 18cfd6d9e..35e79c10b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,10 +36,34 @@ # $ docker exec tailscaled tailscale status -FROM golang:1.27-alpine AS build-env +# Use Tailscale's Go toolchain (a fork of Go) as specified by the +# go.toolchain.rev file, matching how everything else in this repo is +# built, rather than the golang Docker image. This avoids this +# Dockerfile breaking for a day or two after each Go minor version +# bump, when go.mod requires a Go version that the official golang +# Docker image doesn't yet ship. See +# https://github.com/tailscale/tailscale/issues/21072 +FROM alpine:3.22 AS build-env WORKDIR /go/src/tailscale +RUN apk add --no-cache curl tar + +COPY go.toolchain.rev ./ +RUN read -r REV &2; exit 1 ;; \ + esac && \ + curl --retry 3 -f -L -o /tmp/go.tar.gz "https://github.com/tailscale/go/releases/download/build-${REV}/linux-${ARCH}.tar.gz" && \ + mkdir -p /usr/local/go && \ + tar -C /usr/local/go --strip-components=1 -xzf /tmp/go.tar.gz && \ + rm /tmp/go.tar.gz + +ENV GOPATH=/go +ENV PATH=/usr/local/go/bin:$GOPATH/bin:$PATH + COPY go.mod go.sum ./ RUN go mod download diff --git a/docs/go-bump-checklist.md b/docs/go-bump-checklist.md index 5088777bb..3bb478f2d 100644 --- a/docs/go-bump-checklist.md +++ b/docs/go-bump-checklist.md @@ -26,7 +26,6 @@ and past bump commits (e.g. "go.toolchain.branch: switch to Go 1.26", ## Version strings elsewhere -- [ ] `Dockerfile`: `FROM golang:1.NN-alpine` in the build stage. - [ ] `README.md`: "We always require the latest Go release, currently Go 1.NN". - [ ] `.github/workflows/`: grep for hardcoded Go versions. As of Go 1.27 the workflows use `go-version-file: go.mod` and need no diff --git a/version/version_test.go b/version/version_test.go index 01fcd47ec..db01ae471 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -38,7 +38,9 @@ func readAlpineTag(t *testing.T, file string) string { if !ok { continue } - return string(suf) + // Drop anything after the tag, such as an "AS stage-name" suffix. + tag, _, _ := bytes.Cut(suf, []byte(" ")) + return string(tag) } return "" } diff --git a/version_test.go b/version_test.go index 6fb3ddef9..bcd5fb9c5 100644 --- a/version_test.go +++ b/version_test.go @@ -4,7 +4,6 @@ package tailscaleroot import ( - "fmt" "os" "os/exec" "runtime" @@ -14,19 +13,6 @@ import ( "golang.org/x/mod/modfile" ) -func TestDockerfileVersion(t *testing.T) { - goVersion := mustGetGoModVersion(t, false) - - dockerFile, err := os.ReadFile("Dockerfile") - if err != nil { - t.Fatal(err) - } - wantSub := fmt.Sprintf("FROM golang:%s-alpine AS build-env", goVersion) - if !strings.Contains(string(dockerFile), wantSub) { - t.Errorf("didn't find %q in Dockerfile", wantSub) - } -} - // TestGoVersion tests that the Go version specified in go.mod matches ./tool/go version. func TestGoVersion(t *testing.T) { // We could special-case ./tool/go path for Windows, but really there is no @@ -34,7 +20,7 @@ func TestGoVersion(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("Skipping test on Windows") } - goModVersion := mustGetGoModVersion(t, true) + goModVersion := mustGetGoModVersion(t) goToolCmd := exec.Command("./tool/go", "version") goToolOutput, err := goToolCmd.Output() @@ -56,7 +42,7 @@ func TestGoVersion(t *testing.T) { } } -func mustGetGoModVersion(t *testing.T, includePatchVersion bool) string { +func mustGetGoModVersion(t *testing.T) string { t.Helper() goModBytes, err := os.ReadFile("go.mod") @@ -73,13 +59,5 @@ func mustGetGoModVersion(t *testing.T, includePatchVersion bool) string { t.Fatal("no Go version found in go.mod") } - version := modFile.Go.Version - - parts := strings.Split(version, ".") - if !includePatchVersion { - if len(parts) >= 2 { - version = parts[0] + "." + parts[1] - } - } - return version + return modFile.Go.Version }