@@ -28,6 +28,7 @@ type BuildContext struct {
2828 Debug bool // enables additional debugging or verbose logging
2929 FastBuild bool // skip all non-essential steps (linting, testing etc.) to build faster
3030 RepositoryURL string // human-visitable URL, like "https://github.com/function61/turbobob"
31+ IsDefaultBranch bool // whether we are in "main" / "master" or equivalent branch
3132}
3233
3334func runBuilder (builder BuilderSpec , buildCtx * BuildContext , opDesc string , cmdToRun []string ) error {
@@ -130,6 +131,10 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
130131 tagLatest := tagWithoutVersion + ":latest"
131132 dockerfilePath := dockerImage .DockerfilePath
132133
134+ // only tag latest from the default branch (= main / master / ...), because it is expected
135+ // that non-default branch builds are dev/experimental builds.
136+ shouldTagLatest := dockerImage .TagLatest && buildCtx .IsDefaultBranch
137+
133138 labelArgs := []string {
134139 "--label=org.opencontainers.image.created=" + time .Now ().UTC ().Format (time .RFC3339 ),
135140 "--label=org.opencontainers.image.revision=" + buildCtx .RevisionId .RevisionId ,
@@ -152,7 +157,9 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
152157
153158 // use buildx when platforms set. it's almost same as "$ docker build" but it almost transparently
154159 // supports cross-architecture builds via binftm_misc + QEMU userspace emulation
155- if len (dockerImage .Platforms ) > 0 {
160+ useBuildx := len (dockerImage .Platforms ) > 0
161+
162+ if useBuildx {
156163 // TODO: if in CI, install buildx automatically if needed?
157164
158165 args := []string {
@@ -165,7 +172,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
165172
166173 args = append (args , labelArgs ... )
167174
168- if dockerImage . TagLatest {
175+ if shouldTagLatest {
169176 args = append (args , "--tag=" + tagLatest )
170177 }
171178
@@ -215,7 +222,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
215222 return err
216223 }
217224
218- if dockerImage . TagLatest {
225+ if shouldTagLatest {
219226 if err := exec .Command ("docker" , "tag" , tag , tagLatest ).Run (); err != nil {
220227 return fmt .Errorf ("tagging failed %s -> %s failed: %v" , tag , tagLatest , err )
221228 }
@@ -501,6 +508,12 @@ func buildEntry() *cobra.Command {
501508 buildCtx .RepositoryURL = fmt .Sprintf ("%s/%s" , os .Getenv ("GITHUB_SERVER_URL" ), ownerAndRepo )
502509 }
503510
511+ // 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)
512+ defaultBranchName := firstNonEmpty (os .Getenv ("DEFAULT_BRANCH_NAME" ), "main" )
513+ if defaultBranchName == os .Getenv ("GITHUB_REF_NAME" ) {
514+ buildCtx .IsDefaultBranch = true
515+ }
516+
504517 if os .Getenv ("RUNNER_DEBUG" ) == "1" {
505518 buildCtx .Debug = true
506519 }
0 commit comments