Skip to content

Commit 1435999

Browse files
authored
Merge pull request #6 from etsy/ay/enable-create-gitops-pr-rule
Enable create_gitops_prs rule again
2 parents 0e0d46e + 9cb067e commit 1435999

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ For the full list of `create_gitops_prs` command line options, run:
372372
bazel run @rules_gitops//gitops/prer:create_gitops_prs
373373
```
374374

375+
Alternatively, you can use the `create_gitops_prs` rule that references `gitops` targets. You can run the target to template yaml & image push in parallel.
376+
375377
<a name="gitops-and-deployment-supported-git-servers"></a>
376378
### Supported Git Servers
377379

gitops/create_gitops_prs.tpl.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -x
33
set -e
44

5-
GIT_COMMIT=$(git rev-parse HEAD)
5+
CURR_GIT_COMMIT=$(git rev-parse HEAD)
6+
GIT_COMMIT=${BUILDKITE_COMMIT:-$CURR_GIT_COMMIT}
67

7-
%{prer} --git_commit=$GIT_COMMIT %{params} "${@}"
8+
%{prer} --git_commit $GIT_COMMIT %{params}

gitops/gitops.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ def __create_gitops_prs_impl(ctx):
4040
params += "--github_repo_owner {} ".format(ctx.attr.github_repo_owner)
4141
if ctx.attr.github_repo:
4242
params += "--github_repo {} ".format(ctx.attr.github_repo)
43+
# Etsy Custom params
44+
if ctx.attr.github_app_id:
45+
params += "--github_app_id {} ".format(ctx.attr.github_app_id)
46+
if ctx.attr.github_installation_id:
47+
params += "--github_installation_id {} ".format(ctx.attr.github_installation_id)
48+
if ctx.attr.github_app_repo_owner:
49+
params += "--github_app_repo_owner {} ".format(ctx.attr.github_app_repo_owner)
50+
if ctx.attr.private_key:
51+
params += "--private_key {} ".format(ctx.attr.private_key)
4352

4453
ctx.actions.expand_template(
4554
template = ctx.file._tpl,
@@ -127,6 +136,19 @@ create_gitops_prs = rule(
127136
cfg = "exec",
128137
executable = True,
129138
),
139+
# Etsy Custom params
140+
"github_app_id": attr.string(
141+
doc = "GitHub App Id"
142+
),
143+
"github_installation_id": attr.string(
144+
doc = "GitHub App Installation Id"
145+
),
146+
"github_app_repo_owner": attr.string(
147+
doc = "the owner user/organization to use for github api requests"
148+
),
149+
"private_key": attr.string(
150+
doc = "Private Key"
151+
),
130152
},
131153
executable = True,
132154
)

gitops/prer/create_gitops_prs.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ type Config struct {
5858
PRBody string
5959
DeploymentBranchSuffix string
6060

61+
// create_gitops_prs rule
62+
ResolvedBinaries SliceFlags
63+
ResolvedPushes SliceFlags
64+
6165
// Dependencies
6266
DependencyKinds []string
6367
DependencyNames []string
@@ -92,6 +96,10 @@ func initConfig() *Config {
9296
flag.StringVar(&cfg.PRBody, "gitops_pr_body", "", "PR body message")
9397
flag.StringVar(&cfg.DeploymentBranchSuffix, "deployment_branch_suffix", "", "Suffix for deployment branch names")
9498

99+
// create_gitops_prs rule sets these when used with `bazel run`
100+
flag.Var(&cfg.ResolvedBinaries, "resolved_binary", "list of resolved gitops binaries to run. Can be specified multiple times. format is releasetrain:cmd/binary/to/run/command. Default is empty")
101+
flag.Var(&cfg.ResolvedPushes, "resolved_push", "list of resolved push binaries to run. Can be specified multiple times. format is cmd/binary/to/run/command. Default is empty")
102+
95103
// Dependencies
96104
var kinds, names, attrs SliceFlags
97105
flag.Var(&kinds, "gitops_dependencies_kind", "Dependency kinds for GitOps phase")
@@ -145,6 +153,27 @@ func executeBazelQuery(query string) *analysis.CqueryResult {
145153
return result
146154
}
147155

156+
func processResolvedImages(cfg *Config) {
157+
resolvedPushChan := make(chan string)
158+
var wg sync.WaitGroup
159+
wg.Add(cfg.PushParallelism)
160+
161+
for i := 0; i < cfg.PushParallelism; i++ {
162+
go func() {
163+
defer wg.Done()
164+
for cmd := range resolvedPushChan {
165+
exec.Mustex("", cmd)
166+
}
167+
}()
168+
}
169+
170+
for _, r := range cfg.ResolvedPushes {
171+
resolvedPushChan <- r
172+
}
173+
close(resolvedPushChan)
174+
wg.Wait()
175+
}
176+
148177
func processImages(targets []string, cfg *Config) {
149178
deps := fmt.Sprintf("set('%s')", strings.Join(targets, "' '"))
150179
queries := []string{}
@@ -231,17 +260,30 @@ func main() {
231260
}
232261
}
233262

234-
// Find release trains
235-
query := fmt.Sprintf("attr(deployment_branch, \".+\", attr(release_branch_prefix, \"%s\", kind(gitops, %s)))",
236-
cfg.ReleaseBranch, cfg.Targets)
263+
trains := make(map[string][]string)
264+
if len(cfg.ResolvedBinaries) > 0 {
265+
// This condition is used when calling the script from create_gitops_pr rules
266+
// When you call `bazel run <create_gitops_pr target>`, you can't call another bazel query within a bazel run command
267+
// So we have to rely on resolved binaries that were passed in
268+
for _, rb := range cfg.ResolvedBinaries {
269+
releaseTrain, bin, found := strings.Cut(rb, ":")
270+
if !found {
271+
log.Fatalf("resolved_binaries: invalid resolved_binary format: %s", rb)
272+
}
273+
trains[releaseTrain] = append(trains[releaseTrain], bin)
274+
}
275+
} else {
276+
// Find release trains
277+
query := fmt.Sprintf("attr(deployment_branch, \".+\", attr(release_branch_prefix, \"%s\", kind(gitops, %s)))",
278+
cfg.ReleaseBranch, cfg.Targets)
237279

238-
result := executeBazelQuery(query)
280+
result := executeBazelQuery(query)
239281

240-
trains := make(map[string][]string)
241-
for _, t := range result.Results {
242-
for _, attr := range t.Target.GetRule().GetAttribute() {
243-
if attr.GetName() == "deployment_branch" {
244-
trains[attr.GetStringValue()] = append(trains[attr.GetStringValue()], t.Target.Rule.GetName())
282+
for _, t := range result.Results {
283+
for _, attr := range t.Target.GetRule().GetAttribute() {
284+
if attr.GetName() == "deployment_branch" {
285+
trains[attr.GetStringValue()] = append(trains[attr.GetStringValue()], t.Target.Rule.GetName())
286+
}
245287
}
246288
}
247289
}
@@ -317,7 +359,11 @@ func main() {
317359
return
318360
}
319361

320-
processImages(updatedTargets, cfg)
362+
if len(cfg.ResolvedPushes) > 0 {
363+
processResolvedImages(cfg)
364+
} else {
365+
processImages(updatedTargets, cfg)
366+
}
321367

322368
if !cfg.DryRun {
323369
slug := os.Getenv("BUILDKITE_PIPELINE_SLUG")

0 commit comments

Comments
 (0)