Skip to content

Commit

Permalink
docker images: only tag latest from default branch (main/master/...)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed May 28, 2024
1 parent 43f8f4d commit f6a7ff4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/bob/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -165,7 +172,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont

args = append(args, labelArgs...)

if dockerImage.TagLatest {
if shouldTagLatest {
args = append(args, "--tag="+tagLatest)
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/bob/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit f6a7ff4

Please sign in to comment.