Skip to content

Commit

Permalink
Skip pushing the git commit metadata if BUILDKITE_COMMIT is set
Browse files Browse the repository at this point in the history
  • Loading branch information
CerealBoy committed Jan 19, 2025
1 parent 0ddf5f9 commit 1544fbb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
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 potentiallyValidGitSHA = regexp.MustCompile(`^[a-f0-9]{40}$`)

// 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 commitRef != "HEAD" && potentiallyValidGitSHA.MatchString(commitRef) {
// we can skip the metadata shenanigans here and push straight through
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

0 comments on commit 1544fbb

Please sign in to comment.