From 8ec11dc519b9d84b25191cd6fc55de15e279239d Mon Sep 17 00:00:00 2001 From: Joonas Loppi Date: Tue, 28 May 2024 12:44:23 +0300 Subject: [PATCH] docker images: only tag `latest` from default branch (main/master/...) --- cmd/bob/build.go | 19 ++++++++++++++++--- cmd/bob/utils.go | 9 +++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cmd/bob/build.go b/cmd/bob/build.go index 50185cf..71a6e5e 100644 --- a/cmd/bob/build.go +++ b/cmd/bob/build.go @@ -28,6 +28,7 @@ type BuildContext struct { Debug bool // enables additional debugging or verbose logging FastBuild bool // skip all non-essential steps (linting, testing etc.) to build faster RepositoryURL string // human-visitable URL, like "https://github.com/function61/turbobob" + IsDefaultBranch bool // whether we are in "main" / "master" or equivalent branch } func runBuilder(builder BuilderSpec, buildCtx *BuildContext, opDesc string, cmdToRun []string) error { @@ -130,6 +131,10 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont tagLatest := tagWithoutVersion + ":latest" dockerfilePath := dockerImage.DockerfilePath + // only tag latest from the default branch (= main / master / ...), because it is expected + // that non-default branch builds are dev/experimental builds. + shouldTagLatest := dockerImage.TagLatest && buildCtx.IsDefaultBranch + labelArgs := []string{ "--label=org.opencontainers.image.created=" + time.Now().UTC().Format(time.RFC3339), "--label=org.opencontainers.image.revision=" + buildCtx.RevisionId.RevisionId, @@ -152,7 +157,9 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont // use buildx when platforms set. it's almost same as "$ docker build" but it almost transparently // supports cross-architecture builds via binftm_misc + QEMU userspace emulation - if len(dockerImage.Platforms) > 0 { + useBuildx := len(dockerImage.Platforms) > 0 + + if useBuildx { // TODO: if in CI, install buildx automatically if needed? args := []string{ @@ -165,7 +172,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont args = append(args, labelArgs...) - if dockerImage.TagLatest { + if shouldTagLatest { args = append(args, "--tag="+tagLatest) } @@ -215,7 +222,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont return err } - if dockerImage.TagLatest { + if shouldTagLatest { if err := exec.Command("docker", "tag", tag, tagLatest).Run(); err != nil { return fmt.Errorf("tagging failed %s -> %s failed: %v", tag, tagLatest, err) } @@ -501,6 +508,12 @@ func buildEntry() *cobra.Command { buildCtx.RepositoryURL = fmt.Sprintf("%s/%s", os.Getenv("GITHUB_SERVER_URL"), ownerAndRepo) } + // not automatically available as ENV variable (it only exists as a workflow variable `github.event.repository.default_branch` which you'd have to pass to ENV) + defaultBranchName := firstNonEmpty(os.Getenv("DEFAULT_BRANCH_NAME"), "main") + if defaultBranchName == os.Getenv("GITHUB_REF_NAME") { + buildCtx.IsDefaultBranch = true + } + if os.Getenv("RUNNER_DEBUG") == "1" { buildCtx.Debug = true } diff --git a/cmd/bob/utils.go b/cmd/bob/utils.go index ff8ef27..e658534 100644 --- a/cmd/bob/utils.go +++ b/cmd/bob/utils.go @@ -91,3 +91,12 @@ func (l *lineSplitterTee) Write(data []byte) (int, error) { return len(data), nil } + +// tech debt: can't update to newer Go to use this func from gokit +func firstNonEmpty(a, b string) string { + if a != "" { + return a + } else { + return b + } +}