77 "os/exec"
88 "path/filepath"
99 "strings"
10+ "time"
1011
1112 "github.com/function61/gokit/os/osutil"
1213 "github.com/function61/turbobob/pkg/versioncontrol"
@@ -23,7 +24,8 @@ type BuildContext struct {
2324 ENVsAreRequired bool
2425 VersionControl versioncontrol.Interface
2526 RevisionId * versioncontrol.RevisionId
26- FastBuild bool // skip all non-essential steps (linting, testing etc.) to build faster
27+ FastBuild bool // skip all non-essential steps (linting, testing etc.) to build faster
28+ RepositoryURL string // human-visitable URL, like "https://github.com/function61/turbobob"
2729}
2830
2931func runBuilder (builder BuilderSpec , buildCtx * BuildContext , opDesc string , cmdToRun []string ) error {
@@ -89,6 +91,19 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
8991 tagLatest := tagWithoutVersion + ":latest"
9092 dockerfilePath := dockerImage .DockerfilePath
9193
94+ labelArgs := []string {
95+ "--label=org.opencontainers.image.created=" + time .Now ().UTC ().Format (time .RFC3339 ),
96+ "--label=org.opencontainers.image.revision=" + buildCtx .RevisionId .RevisionId ,
97+ "--label=org.opencontainers.image.version=" + buildCtx .RevisionId .FriendlyRevisionId ,
98+ }
99+
100+ if buildCtx .RepositoryURL != "" {
101+ // "URL to get source code for building the image"
102+ labelArgs = append (labelArgs , "--label=org.opencontainers.image.source=" + buildCtx .RepositoryURL )
103+ // "URL to find more information on the image"
104+ labelArgs = append (labelArgs , "--label=org.opencontainers.image.url=" + buildCtx .RepositoryURL )
105+ }
106+
92107 // "" => "."
93108 // "Dockerfile" => "."
94109 // "subdir/Dockerfile" => "subdir"
@@ -106,10 +121,11 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
106121 "build" ,
107122 "--platform" , strings .Join (dockerImage .Platforms , "," ),
108123 "--file" , dockerfilePath ,
109- "--label=org.opencontainers.image.revision=" + buildCtx .RevisionId .RevisionId ,
110124 "--tag=" + tag ,
111125 }
112126
127+ args = append (args , labelArgs ... )
128+
113129 if dockerImage .TagLatest {
114130 args = append (args , "--tag=" + tagLatest )
115131 }
@@ -126,13 +142,15 @@ func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildCont
126142 return passthroughStdoutAndStderr (exec .Command ("docker" , args ... )).Run ()
127143 }
128144
129- buildCmd := passthroughStdoutAndStderr (exec .Command (
130- "docker" ,
145+ dockerBuildArgs := []string {"docker" ,
131146 "build" ,
132147 "--file" , dockerfilePath ,
133- "--tag" , tag ,
134- "--label=org.opencontainers.image.revision=" + buildCtx .RevisionId .RevisionId ,
135- buildContextDir ))
148+ "--tag" , tag }
149+ dockerBuildArgs = append (dockerBuildArgs , labelArgs ... )
150+ dockerBuildArgs = append (dockerBuildArgs , buildContextDir )
151+
152+ //nolint:gosec // ok
153+ buildCmd := passthroughStdoutAndStderr (exec .Command (dockerBuildArgs [0 ], dockerBuildArgs [1 :]... ))
136154
137155 if err := buildCmd .Run (); err != nil {
138156 return err
@@ -439,6 +457,11 @@ func buildEntry() *cobra.Command {
439457 return err
440458 }
441459
460+ if ownerAndRepo := os .Getenv ("GITHUB_REPOSITORY" ); ownerAndRepo != "" {
461+ // "function61/turbobob" => "https://github.com/function61/turbobob"
462+ buildCtx .RepositoryURL = fmt .Sprintf ("%s/%s" , os .Getenv ("GITHUB_SERVER_URL" ), ownerAndRepo )
463+ }
464+
442465 return build (buildCtx )
443466 }())
444467 },
0 commit comments