@@ -28,6 +28,7 @@ type BuildContext struct {
28
28
Debug bool // enables additional debugging or verbose logging
29
29
FastBuild bool // skip all non-essential steps (linting, testing etc.) to build faster
30
30
RepositoryURL string // human-visitable URL, like "https://github.com/function61/turbobob"
31
+ IsDefaultBranch bool // whether we are in "main" / "master" or equivalent branch
31
32
}
32
33
33
34
func runBuilder (builder BuilderSpec , buildCtx * BuildContext , opDesc string , cmdToRun []string ) error {
@@ -130,6 +131,10 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
130
131
tagLatest := tagWithoutVersion + ":latest"
131
132
dockerfilePath := dockerImage .DockerfilePath
132
133
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
+
133
138
labelArgs := []string {
134
139
"--label=org.opencontainers.image.created=" + time .Now ().UTC ().Format (time .RFC3339 ),
135
140
"--label=org.opencontainers.image.revision=" + buildCtx .RevisionId .RevisionId ,
@@ -152,7 +157,9 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
152
157
153
158
// use buildx when platforms set. it's almost same as "$ docker build" but it almost transparently
154
159
// 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 {
156
163
// TODO: if in CI, install buildx automatically if needed?
157
164
158
165
args := []string {
@@ -165,7 +172,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
165
172
166
173
args = append (args , labelArgs ... )
167
174
168
- if dockerImage . TagLatest {
175
+ if shouldTagLatest {
169
176
args = append (args , "--tag=" + tagLatest )
170
177
}
171
178
@@ -215,7 +222,7 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
215
222
return err
216
223
}
217
224
218
- if dockerImage . TagLatest {
225
+ if shouldTagLatest {
219
226
if err := exec .Command ("docker" , "tag" , tag , tagLatest ).Run (); err != nil {
220
227
return fmt .Errorf ("tagging failed %s -> %s failed: %v" , tag , tagLatest , err )
221
228
}
@@ -501,6 +508,12 @@ func buildEntry() *cobra.Command {
501
508
buildCtx .RepositoryURL = fmt .Sprintf ("%s/%s" , os .Getenv ("GITHUB_SERVER_URL" ), ownerAndRepo )
502
509
}
503
510
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
+
504
517
if os .Getenv ("RUNNER_DEBUG" ) == "1" {
505
518
buildCtx .Debug = true
506
519
}
0 commit comments