Skip to content

Commit c9ba07e

Browse files
authored
ci: Cloud Build jobs to propagate _PUSH substitution variable (#1757)
The Cloud Build generate-worker job to pass the _PUSH substitution variable to the next Cloud Build jobs. They invoke Librarian CLI with either '-push=true' or '-push=false' parameter. This controls whether the job creates a pull request or not. This change should come with the Terraform change cl/795539979.
1 parent c9d075e commit c9ba07e

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

infra/prod/generate-worker.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ steps:
2323
- './cmd/automation'
2424
- '--project=$PROJECT_ID'
2525
- '--command=generate'
26+
- '--push=$_PUSH'
2627
tags: ['generate-dispatcher']
2728
availableSecrets:
2829
options:

infra/prod/generate.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ steps:
4141
- $_LIBRARY_ID
4242
- '-api-source'
4343
- '/workspace/googleapis'
44+
- '-push=$_PUSH'
4445
secretEnv: ['LIBRARIAN_GITHUB_TOKEN']
4546
tags: ['generate-$_REPOSITORY']
4647
availableSecrets:

internal/automation/cli.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Run(args []string) error {
2929
return err
3030
}
3131

32-
err = RunCommand(ctx, options.Command, options.ProjectId)
32+
err = RunCommand(ctx, options.Command, options.ProjectId, options.Push)
3333
if err != nil {
3434
slog.Error("Error running command", slog.Any("err", err))
3535
return err
@@ -40,18 +40,21 @@ func Run(args []string) error {
4040
type runOptions struct {
4141
Command string
4242
ProjectId string
43+
Push bool
4344
}
4445

4546
func parseFlags(args []string) (*runOptions, error) {
4647
flagSet := flag.NewFlagSet("dispatcher", flag.ContinueOnError)
4748
projectId := flagSet.String("project", "cloud-sdk-librarian-prod", "GCP project ID")
4849
command := flagSet.String("command", "generate", "The librarian command to run")
50+
push := flagSet.Bool("push", true, "The _PUSH flag (true/false) to Librarian CLI's -push option")
4951
err := flagSet.Parse(args)
5052
if err != nil {
5153
return nil, err
5254
}
5355
return &runOptions{
5456
ProjectId: *projectId,
5557
Command: *command,
58+
Push: *push,
5659
}, nil
5760
}

internal/automation/cli_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestParseArgs(t *testing.T) {
3434
want: &runOptions{
3535
Command: "generate",
3636
ProjectId: "cloud-sdk-librarian-prod",
37+
Push: true,
3738
},
3839
},
3940
{
@@ -43,6 +44,7 @@ func TestParseArgs(t *testing.T) {
4344
want: &runOptions{
4445
Command: "generate",
4546
ProjectId: "some-project-id",
47+
Push: true,
4648
},
4749
},
4850
{
@@ -52,6 +54,17 @@ func TestParseArgs(t *testing.T) {
5254
want: &runOptions{
5355
Command: "stage-release",
5456
ProjectId: "cloud-sdk-librarian-prod",
57+
Push: true,
58+
},
59+
},
60+
{
61+
name: "sets command",
62+
args: []string{"--command=stage-release", "--push=false"},
63+
wantErr: false,
64+
want: &runOptions{
65+
Command: "stage-release",
66+
ProjectId: "cloud-sdk-librarian-prod",
67+
Push: false,
5568
},
5669
},
5770
} {

internal/automation/trigger.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *wrappedCloudBuildClient) ListBuildTriggers(ctx context.Context, req *cl
5555
}
5656

5757
// RunCommand triggers a command for each registered repository that supports it.
58-
func RunCommand(ctx context.Context, command string, projectId string) error {
58+
func RunCommand(ctx context.Context, command string, projectId string, push bool) error {
5959
c, err := cloudbuild.NewClient(ctx)
6060
if err != nil {
6161
return fmt.Errorf("error creating cloudbuild client: %w", err)
@@ -64,10 +64,10 @@ func RunCommand(ctx context.Context, command string, projectId string) error {
6464
wrappedClient := &wrappedCloudBuildClient{
6565
client: c,
6666
}
67-
return runCommandWithClient(ctx, wrappedClient, command, projectId)
67+
return runCommandWithClient(ctx, wrappedClient, command, projectId, push)
6868
}
6969

70-
func runCommandWithClient(ctx context.Context, client CloudBuildClient, command string, projectId string) error {
70+
func runCommandWithClient(ctx context.Context, client CloudBuildClient, command string, projectId string, push bool) error {
7171
// validate command is allowed
7272
triggerName := triggerNameByCommandName[command]
7373
if triggerName == "" {
@@ -89,6 +89,7 @@ func runCommandWithClient(ctx context.Context, client CloudBuildClient, command
8989
substitutions := map[string]string{
9090
"_REPOSITORY": repository.Name,
9191
"_GITHUB_TOKEN_SECRET_NAME": repository.SecretName,
92+
"_PUSH": fmt.Sprintf("%v", push),
9293
}
9394
err = runCloudBuildTriggerByName(ctx, client, projectId, region, triggerName, substitutions)
9495
if err != nil {

internal/automation/trigger_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestRunCommandWithClient(t *testing.T) {
2626
for _, test := range []struct {
2727
name string
2828
command string
29+
push bool
2930
want string
3031
runError error
3132
wantErr bool
@@ -34,6 +35,7 @@ func TestRunCommandWithClient(t *testing.T) {
3435
{
3536
name: "runs generate trigger",
3637
command: "generate",
38+
push: true,
3739
wantErr: false,
3840
buildTriggers: []*cloudbuildpb.BuildTrigger{
3941
{
@@ -49,6 +51,7 @@ func TestRunCommandWithClient(t *testing.T) {
4951
{
5052
name: "runs prepare-release trigger",
5153
command: "stage-release",
54+
push: true,
5255
wantErr: false,
5356
buildTriggers: []*cloudbuildpb.BuildTrigger{
5457
{
@@ -64,6 +67,7 @@ func TestRunCommandWithClient(t *testing.T) {
6467
{
6568
name: "invalid command",
6669
command: "invalid-command",
70+
push: true,
6771
wantErr: true,
6872
buildTriggers: []*cloudbuildpb.BuildTrigger{
6973
{
@@ -79,6 +83,7 @@ func TestRunCommandWithClient(t *testing.T) {
7983
{
8084
name: "error triggering",
8185
command: "generate",
86+
push: true,
8287
runError: fmt.Errorf("some-error"),
8388
wantErr: true,
8489
buildTriggers: []*cloudbuildpb.BuildTrigger{
@@ -99,7 +104,7 @@ func TestRunCommandWithClient(t *testing.T) {
99104
runError: test.runError,
100105
buildTriggers: test.buildTriggers,
101106
}
102-
err := runCommandWithClient(ctx, client, test.command, "some-project")
107+
err := runCommandWithClient(ctx, client, test.command, "some-project", test.push)
103108
if test.wantErr && err == nil {
104109
t.Errorf("expected error, but did not return one")
105110
} else if !test.wantErr && err != nil {

0 commit comments

Comments
 (0)