Skip to content

Commit

Permalink
Use annotations for Git info
Browse files Browse the repository at this point in the history
  • Loading branch information
cdupuis committed Aug 23, 2022
1 parent cb8253f commit 109c305
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
return err
}

labels, err := addGitProvenance(in.labels, in.contextPath, in.dockerfileName)
newOutputs, err := addGitProvenance(in.outputs, in.contextPath, in.dockerfileName)
if err != nil {
return err
}
in.outputs = newOutputs

opts := build.Options{
Inputs: build.Inputs{
Expand All @@ -144,7 +145,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
BuildArgs: listToMap(in.buildArgs, true),
ExtraHosts: in.extraHosts,
ImageIDFile: in.imageIDFile,
Labels: listToMap(labels, false),
Labels: listToMap(in.labels, false),
NetworkMode: in.networkMode,
NoCache: noCache,
NoCacheFilter: in.noCacheFilter,
Expand Down Expand Up @@ -649,10 +650,10 @@ func parsePrintFunc(str string) (*build.PrintFunc, error) {
return f, nil
}

func addGitProvenance(labels []string, contextPath string, dockerfilePath string) ([]string, error) {
func addGitProvenance(outputs []string, contextPath string, dockerfilePath string) ([]string, error) {
if v, ok := os.LookupEnv("BUILDX_GIT_INFO"); ok && contextPath != "" {
if len(labels) == 0 {
labels = make([]string, 0)
if len(outputs) == 0 {
outputs = make([]string, 0)
}
// figure out in which directory the git command needs to run
var wd string
Expand All @@ -662,6 +663,9 @@ func addGitProvenance(labels []string, contextPath string, dockerfilePath string
} else {
wd = contextPath
}

gitInfo := []string{"type=image", "name=target", "oci-mediatypes=true"}

// obtain Git sha of current HEAD
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = wd
Expand All @@ -674,9 +678,9 @@ func addGitProvenance(labels []string, contextPath string, dockerfilePath string
cmd.Dir = wd
_, err = cmd.Output()
if err == nil {
labels = append(labels, []string{fmt.Sprintf("org.opencontainers.image.revision=%s", strings.TrimSpace(string(sha)))}...)
gitInfo = append(gitInfo, []string{fmt.Sprintf("annotation.org.opencontainers.image.revision=%s", strings.TrimSpace(string(sha)))}...)
} else {
labels = append(labels, []string{fmt.Sprintf("org.opencontainers.image.revision=%s-dirty", strings.TrimSpace(string(sha)))}...)
gitInfo = append(gitInfo, []string{fmt.Sprintf("annotation.org.opencontainers.image.revision=%s-dirty", strings.TrimSpace(string(sha)))}...)
}
// add the origin url if full Git details are requested
if v == "full" {
Expand All @@ -686,16 +690,17 @@ func addGitProvenance(labels []string, contextPath string, dockerfilePath string
if err != nil {
return nil, err
}
labels = append(labels, []string{fmt.Sprintf("org.opencontainers.image.source=%s", strings.TrimSpace(string(remote)))}...)
gitInfo = append(gitInfo, []string{fmt.Sprintf("annotation.org.opencontainers.image.source=%s", strings.TrimSpace(string(remote)))}...)
}
// add Dockerfile path; there is no org.opencontainers annotation for this
if dockerfilePath == "" {
dockerfilePath = "Dockerfile"
}
labels = append(labels, []string{fmt.Sprintf("com.docker.image.dockerfile.path=%s", dockerfilePath)}...)
gitInfo = append(gitInfo, []string{fmt.Sprintf("annotation.com.docker.image.dockerfile.path=%s", dockerfilePath)}...)
outputs = append(outputs, []string{strings.Join(gitInfo[:], ",")}...)
}

return labels, nil
return outputs, nil
}

func writeMetadataFile(filename string, dt interface{}) error {
Expand Down

0 comments on commit 109c305

Please sign in to comment.