Skip to content
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

Skip pushing the git commit metadata if BUILDKITE_COMMIT is set #3152

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/job/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -780,12 +781,21 @@ func gitFetchCommitWithFallback(ctx context.Context, shell *shell.Shell, gitFetc

const CommitMetadataKey = "buildkite:git:commit"

var potentiallyValidGitHashRE = regexp.MustCompile(`^[a-f0-9]{40}|[a-f0-9]{64}$`)

// sendCommitToBuildkite sends commit information (commit, author, subject, body) to Buildkite, as the BK backend doesn't
// have access to user's VCSes. To do this, we set a special meta-data key in the build, but only if it isn't already present
// Functionally, this means that the first job in a build (usually a pipeline upload or similar) will push the commit info
// to buildkite, which uses this info to display commit info in the UI eg in the title for the build
// note that we bail early if the key already exists, as we don't want to overwrite it
func (e *Executor) sendCommitToBuildkite(ctx context.Context) error {
commitRef, _ := e.shell.Env.Get("BUILDKITE_COMMIT")
if potentiallyValidGitHashRE.MatchString(commitRef) {
// we can skip the metadata shenanigans here and push straight through
CerealBoy marked this conversation as resolved.
Show resolved Hide resolved
e.shell.Commentf("Skipping the meta-data git commit steps and assuming %q is correct...", commitRef)
return nil
}

e.shell.Commentf("Checking to see if git commit information needs to be sent to Buildkite...")
cmd := e.shell.Command("buildkite-agent", "meta-data", "exists", CommitMetadataKey)
if err := cmd.Run(ctx); err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,9 @@ func TestWithResolvingCommitExperiment_WithGitMirrors(t *testing.T) {
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
{"rev-parse", "HEAD"},
})

// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)

tester.RunAndCheck(t, env...)
}

Expand Down
6 changes: 0 additions & 6 deletions internal/job/integration/checkout_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,9 @@ func TestWithResolvingCommitExperiment(t *testing.T) {
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
{"rev-parse", "HEAD"},
})

// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)

tester.RunAndCheck(t, env...)
}

Expand Down