-
Notifications
You must be signed in to change notification settings - Fork 676
build: set provenance vcs details #1462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,23 +3,41 @@ package build | |
| import ( | ||
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/pkg/errors" | ||
| "github.com/docker/buildx/util/gitutil" | ||
| specs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const DockerfileLabel = "com.docker.image.source.entrypoint" | ||
|
|
||
| func addGitProvenance(ctx context.Context, contextPath string, dockerfilePath string) (map[string]string, error) { | ||
| v := os.Getenv("BUILDX_GIT_LABELS") | ||
| if (v != "1" && v != "full") || contextPath == "" { | ||
| return nil, nil | ||
| func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath string) (res map[string]string) { | ||
| res = make(map[string]string) | ||
| if contextPath == "" { | ||
| return | ||
| } | ||
|
|
||
| setGitLabels := false | ||
| if v, ok := os.LookupEnv("BUILDX_GIT_LABELS"); ok { | ||
| if v == "full" { // backward compatibility with old "full" mode | ||
| setGitLabels = true | ||
| } else if v, _ := strconv.ParseBool(v); v { | ||
| setGitLabels = v | ||
| } | ||
| } | ||
| setGitInfo := true | ||
| if v, ok := os.LookupEnv("BUILDX_GIT_INFO"); ok { | ||
| if v, _ := strconv.ParseBool(v); v { | ||
| setGitInfo = v | ||
| } | ||
| } | ||
|
|
||
| if !setGitLabels && !setGitInfo { | ||
| return | ||
| } | ||
| labels := make(map[string]string, 0) | ||
|
|
||
| // figure out in which directory the git command needs to run in | ||
| var wd string | ||
|
|
@@ -30,69 +48,62 @@ func addGitProvenance(ctx context.Context, contextPath string, dockerfilePath st | |
| wd, _ = filepath.Abs(filepath.Join(cwd, contextPath)) | ||
| } | ||
|
|
||
| // check if inside git working tree | ||
| cmd := exec.CommandContext(ctx, "git", "rev-parse", "--is-inside-work-tree") | ||
| cmd.Dir = wd | ||
| err := cmd.Run() | ||
| if err != nil { | ||
| gitc := gitutil.New(gitutil.WithContext(ctx), gitutil.WithWorkingDir(wd)) | ||
| if !gitc.IsInsideWorkTree() { | ||
| logrus.Warnf("Unable to determine Git information") | ||
| return nil, nil | ||
| return | ||
| } | ||
|
|
||
| // obtain Git sha of current HEAD | ||
| cmd = exec.CommandContext(ctx, "git", "rev-parse", "HEAD") | ||
| cmd.Dir = wd | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "error obtaining git head") | ||
| } | ||
| sha := strings.TrimSpace(string(out)) | ||
| var resRevision, resSource, resDockerfilePath string | ||
|
|
||
| // check if the current HEAD is clean | ||
| cmd = exec.CommandContext(ctx, "git", "status", "--porcelain", "--ignored") | ||
| cmd.Dir = wd | ||
| out, err = cmd.Output() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "error obtaining git status") | ||
| } | ||
| if len(strings.TrimSpace(string(out))) != 0 { | ||
| sha += "-dirty" | ||
| } | ||
| labels[ocispecs.AnnotationRevision] = sha | ||
|
|
||
| // add a remote url if full Git details are requested; if there aren't any remotes don't fail | ||
| if v == "full" { | ||
| cmd = exec.CommandContext(ctx, "git", "ls-remote", "--get-url") | ||
| cmd.Dir = wd | ||
| out, _ := cmd.Output() | ||
| if len(out) > 0 { | ||
| labels[ocispecs.AnnotationSource] = strings.TrimSpace(string(out)) | ||
| if sha, err := gitc.FullCommit(); err == nil && sha != "" { | ||
| resRevision = sha | ||
| if gitc.IsDirty() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this increases the amount of Edit: I might have counted the requests wrong. Looks like previous also did this with 2 commands. |
||
| resRevision += "-dirty" | ||
| } | ||
| } | ||
|
|
||
| // add Dockerfile path; there is no org.opencontainers annotation for this | ||
| if dockerfilePath == "" { | ||
| dockerfilePath = filepath.Join(wd, "Dockerfile") | ||
| if rurl, err := gitc.RemoteURL(); err == nil && rurl != "" { | ||
| resSource = rurl | ||
| } | ||
|
|
||
| // obtain Git root directory | ||
| cmd = exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") | ||
| cmd.Dir = wd | ||
| out, err = cmd.Output() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get git root") | ||
| if setGitLabels { | ||
| if root, err := gitc.RootDir(); err == nil && root != "" { | ||
| if dockerfilePath == "" { | ||
| dockerfilePath = filepath.Join(wd, "Dockerfile") | ||
| } | ||
| if !filepath.IsAbs(dockerfilePath) { | ||
| cwd, _ := os.Getwd() | ||
| dockerfilePath = filepath.Join(cwd, dockerfilePath) | ||
| } | ||
| dockerfilePath, _ = filepath.Rel(root, dockerfilePath) | ||
| if !strings.HasPrefix(dockerfilePath, "..") { | ||
| resDockerfilePath = dockerfilePath | ||
| } | ||
| } | ||
| } | ||
| root := strings.TrimSpace(string(out)) | ||
|
|
||
| // record only Dockerfile paths that are within the Git root | ||
| if !filepath.IsAbs(dockerfilePath) { | ||
| cwd, _ := os.Getwd() | ||
| dockerfilePath = filepath.Join(cwd, dockerfilePath) | ||
| if resSource != "" { | ||
| if setGitLabels { | ||
| res["label:"+specs.AnnotationSource] = resSource | ||
| } | ||
| if setGitInfo { | ||
| res["vcs:source"] = resSource | ||
| } | ||
| } | ||
| dockerfilePath, _ = filepath.Rel(root, dockerfilePath) | ||
| if !strings.HasPrefix(dockerfilePath, "..") { | ||
| labels[DockerfileLabel] = dockerfilePath | ||
| if resRevision != "" { | ||
| if setGitLabels { | ||
| res["label:"+specs.AnnotationRevision] = resRevision | ||
| } | ||
| if setGitInfo { | ||
| res["vcs:revision"] = resRevision | ||
| } | ||
| } | ||
| if resDockerfilePath != "" { | ||
| if setGitLabels { | ||
| res["label:"+DockerfileLabel] = resDockerfilePath | ||
| } | ||
| } | ||
|
|
||
| return labels, nil | ||
| return | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.