Skip to content

Commit

Permalink
[RB] Set the build user and add a remote bazel role (buildbuddy-io#6774)
Browse files Browse the repository at this point in the history
  • Loading branch information
maggie-lou committed Jun 11, 2024
1 parent a25e080 commit 136a60b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/format/format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ export function formatRole(role: string): string | null {
if (role === "CI") {
return "CI";
}
if (role === "HOSTED_BAZEL") {
return "Remote Bazel";
}
// Don't render unknown roles for now.
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions app/root/root.css
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,11 @@ code .comment {
background: #cfd8dc;
}

.role-badge.HOSTED_BAZEL {
background: #a1a6a8;
color: white;
}

.empty-state .button {
width: 100%;
padding: 16px 32px;
Expand Down
9 changes: 9 additions & 0 deletions cli/remotebazel/remotebazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ func Run(ctx context.Context, opts RunOpts, repoConfig *RepoConfig) (int, error)
envVars[envVar] = val
}

// If not explicitly set, set the build user (the user that initiated the build).
if !contains(envVars, "BUILD_USER") {
val := os.Getenv("BUILD_USER")
if val == "" {
val = os.Getenv("USER")
}
envVars["BUILD_USER"] = val
}

// If not explicitly set, try to set the default branch env var,
// because it will allow us to fallback to snapshots for the default branch
// if there is no snapshot for the current branch
Expand Down
2 changes: 2 additions & 0 deletions enterprise/app/filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export default class FilterComponent extends React.Component<FilterProps, State>
{selectedRoles.has("") && <span className="role-badge DEFAULT">Default</span>}
{selectedRoles.has("CI") && <span className="role-badge CI">CI</span>}
{selectedRoles.has("CI_RUNNER") && <span className="role-badge CI_RUNNER">Workflow</span>}
{selectedRoles.has("HOSTED_BAZEL") && <span className="role-badge HOSTED_BAZEL">Remote Bazel</span>}
{userValue && (
<span className="advanced-badge">
<User /> {userValue}
Expand Down Expand Up @@ -483,6 +484,7 @@ export default class FilterComponent extends React.Component<FilterProps, State>
{this.renderRoleCheckbox("Default", "", selectedRoles)}
{this.renderRoleCheckbox("CI", "CI", selectedRoles)}
{this.renderRoleCheckbox("Workflow", "CI_RUNNER", selectedRoles)}
{this.renderRoleCheckbox("Remote Bazel", "HOSTED_BAZEL", selectedRoles)}
</div>
</div>
<div className="option-group">
Expand Down
15 changes: 11 additions & 4 deletions enterprise/server/cmd/ci_runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,11 +1156,15 @@ func (ar *actionRunner) Run(ctx context.Context, ws *workspace) error {
}

func (ar *actionRunner) workspaceStatusEvent() *bespb.BuildEvent {
buildUser := os.Getenv("BUILD_USER")
if buildUser == "" {
buildUser = ar.username
}
return &bespb.BuildEvent{
Id: &bespb.BuildEventId{Id: &bespb.BuildEventId_WorkspaceStatus{WorkspaceStatus: &bespb.BuildEventId_WorkspaceStatusId{}}},
Payload: &bespb.BuildEvent_WorkspaceStatus{WorkspaceStatus: &bespb.WorkspaceStatus{
Item: []*bespb.WorkspaceStatus_Item{
{Key: "BUILD_USER", Value: ar.username},
{Key: "BUILD_USER", Value: buildUser},
{Key: "BUILD_HOST", Value: ar.hostname},
{Key: "GIT_BRANCH", Value: *pushedBranch},
{Key: "GIT_TREE_STATUS", Value: "Clean"},
Expand Down Expand Up @@ -2030,7 +2034,6 @@ func writeBazelrc(path, invocationID string) error {
defer f.Close()

lines := []string{
"build --build_metadata=ROLE=CI",
"build --build_metadata=PARENT_INVOCATION_ID=" + invocationID,
// Note: these pieces of metadata are set to match the WorkspaceStatus event
// for the outer (workflow) invocation.
Expand All @@ -2049,12 +2052,16 @@ func writeBazelrc(path, invocationID string) error {
// artifact for easier debugging.
"build --heap_dump_on_oom",
}
if *workflowID != "" {
isWorkflow := *workflowID != ""
if isWorkflow {
lines = append(lines, "build --build_metadata=WORKFLOW_ID="+*workflowID)
lines = append(lines, "build --build_metadata=ROLE=CI")
}
if !isWorkflow || *prNumber != 0 {
lines = append(lines, "build --build_metadata=DISABLE_TARGET_TRACKING=true")
}
if *prNumber != 0 {
lines = append(lines, "build --build_metadata=PULL_REQUEST_NUMBER="+fmt.Sprintf("%d", *prNumber))
lines = append(lines, "build --build_metadata=DISABLE_TARGET_TRACKING=true")
}
if isPushedRefInFork() {
lines = append(lines, "build --build_metadata=FORK_REPO_URL="+*pushedRepoURL)
Expand Down
2 changes: 1 addition & 1 deletion enterprise/server/hostedrunner/hostedrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (r *runnerService) createAction(ctx context.Context, req *rnpb.RunRequest,
image = req.GetContainerImage()
}

// By default, use the non-root user.
// By default, use the non-root user as the operating user on the runner.
user := nonRootUser
for _, p := range req.ExecProperties {
if p.Name == platform.DockerUserPropertyName {
Expand Down

0 comments on commit 136a60b

Please sign in to comment.