Resolve named deploys over all branches and sources before filtering
- pick env winners over every branch, then select, so a named deploy matches a full deploy - match env names across sources first, raw branch names only when no env matches - report collision and invalid-name errors only for the named args - add tests for collision losers, all-corrected collisions, prefixed sources and unrelated invalid branches
This commit is contained in:
+104
-34
@@ -225,25 +225,26 @@ func (d *Deployer) Sources() []*Source {
|
||||
// Deploy builds every environment, or only those named by env or branch; a named deploy skips purge and GC.
|
||||
func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) error {
|
||||
ctx, end := d.phase(ctx, "run")
|
||||
want := map[string]bool{}
|
||||
for _, e := range only {
|
||||
want[e] = true
|
||||
}
|
||||
d.mu.Lock()
|
||||
clear(d.flights)
|
||||
clear(d.skips)
|
||||
clear(d.counts)
|
||||
d.mu.Unlock()
|
||||
found := map[string]bool{}
|
||||
var errs []error
|
||||
var plans []*srcPlan
|
||||
for _, s := range d.Sources() {
|
||||
if err := d.deploySource(ctx, s, want, found, modules); err != nil {
|
||||
p, err := d.planSource(ctx, s)
|
||||
if err != nil {
|
||||
d.count("failed")
|
||||
errs = append(errs, fmt.Errorf("source %s: %w", s.Name, err))
|
||||
continue
|
||||
}
|
||||
plans = append(plans, p)
|
||||
}
|
||||
for e := range want {
|
||||
if !found[e] {
|
||||
errs = append(errs, fmt.Errorf("environment %s not found", e))
|
||||
errs = append(errs, d.selectEnvs(plans, only)...)
|
||||
for _, p := range plans {
|
||||
if err := d.deploySource(ctx, p, modules); err != nil {
|
||||
errs = append(errs, fmt.Errorf("source %s: %w", p.s.Name, err))
|
||||
}
|
||||
}
|
||||
errs = append(errs, d.gc(ctx, len(only) == 0))
|
||||
@@ -260,6 +261,68 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// srcPlan is a source's resolved envs, the branches that got none and why, and the envs to build (nil = all).
|
||||
type srcPlan struct {
|
||||
s *Source
|
||||
envs map[string]envRef
|
||||
skipped map[string]error
|
||||
want map[string]bool
|
||||
}
|
||||
|
||||
// selectEnvs resolves args to envs as a full deploy would: env names in any source first, else raw branch names.
|
||||
func (d *Deployer) selectEnvs(plans []*srcPlan, only []string) []error {
|
||||
if len(only) == 0 {
|
||||
return nil
|
||||
}
|
||||
var errs []error
|
||||
for _, p := range plans {
|
||||
p.want = map[string]bool{}
|
||||
}
|
||||
for _, a := range only {
|
||||
isEnv := slices.ContainsFunc(plans, func(p *srcPlan) bool { _, ok := p.envs[a]; return ok })
|
||||
hit := false
|
||||
var why []error
|
||||
for _, p := range plans {
|
||||
if isEnv {
|
||||
if _, ok := p.envs[a]; ok {
|
||||
p.want[a], hit = true, true
|
||||
}
|
||||
continue
|
||||
}
|
||||
for name, ref := range p.envs {
|
||||
if ref.branch == a {
|
||||
p.want[name], hit = true, true
|
||||
}
|
||||
}
|
||||
for _, err := range p.skipErrs(func(b string) bool {
|
||||
name, _, _ := p.s.EnvName(b)
|
||||
return b == a || name == a
|
||||
}) {
|
||||
d.count("failed")
|
||||
why = append(why, fmt.Errorf("source %s: %w", p.s.Name, err))
|
||||
}
|
||||
}
|
||||
if !hit && len(why) == 0 {
|
||||
why = append(why, fmt.Errorf("environment %s not found", a))
|
||||
}
|
||||
errs = append(errs, why...)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// skipErrs returns the distinct reasons, in branch order, for skipped branches match accepts.
|
||||
func (p *srcPlan) skipErrs(match func(string) bool) []error {
|
||||
var out []error
|
||||
seen := map[error]bool{}
|
||||
for _, b := range slices.Sorted(maps.Keys(p.skipped)) {
|
||||
if err := p.skipped[b]; match(b) && !seen[err] {
|
||||
seen[err] = true
|
||||
out = append(out, err)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (d *Deployer) count(outcome string) {
|
||||
d.mu.Lock()
|
||||
d.counts[outcome]++
|
||||
@@ -268,11 +331,11 @@ func (d *Deployer) count(outcome string) {
|
||||
|
||||
type envRef struct{ branch, sha string }
|
||||
|
||||
// Environments maps env name -> branch and sha for every branch, or only those want names by branch or env.
|
||||
func (d *Deployer) Environments(ctx context.Context, s *Source, want map[string]bool) (map[string]envRef, []error) {
|
||||
// Environments maps env name -> branch and sha, and each branch that gets no env to why.
|
||||
func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envRef, map[string]error, error) {
|
||||
all, err := d.branches(ctx, d.srcMirror(s))
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
return nil, nil, err
|
||||
}
|
||||
names := make([]string, 0, len(all))
|
||||
for b := range all {
|
||||
@@ -281,17 +344,14 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, want map[string]
|
||||
sort.Strings(names)
|
||||
claims := map[string][]string{}
|
||||
exact := map[string]string{}
|
||||
var errs []error
|
||||
skipped := map[string]error{}
|
||||
for _, b := range names {
|
||||
if s.Ignored(b) {
|
||||
continue
|
||||
}
|
||||
name, corrected, err := s.EnvName(b)
|
||||
if len(want) > 0 && !want[b] && (name == "" || !want[name]) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
skipped[b] = err
|
||||
continue
|
||||
}
|
||||
if !corrected {
|
||||
@@ -307,12 +367,15 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, want map[string]
|
||||
win = bs[0]
|
||||
}
|
||||
if win == "" {
|
||||
errs = append(errs, fmt.Errorf("branches %s all correct to environment %s, skipped", strings.Join(bs, ", "), name))
|
||||
err := fmt.Errorf("branches %s all correct to environment %s, skipped", strings.Join(bs, ", "), name)
|
||||
for _, b := range bs {
|
||||
skipped[b] = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
for _, b := range bs {
|
||||
if b != win {
|
||||
errs = append(errs, fmt.Errorf("branch %s collides with environment %s, skipped", b, name))
|
||||
skipped[b] = fmt.Errorf("branch %s collides with environment %s, skipped", b, name)
|
||||
}
|
||||
}
|
||||
if !isExact && s.InvalidBranches == "correct_and_warn" {
|
||||
@@ -320,33 +383,40 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, want map[string]
|
||||
}
|
||||
envs[name] = envRef{win, all[win]}
|
||||
}
|
||||
return envs, errs
|
||||
return envs, skipped, nil
|
||||
}
|
||||
|
||||
func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[string]bool, modules bool) error {
|
||||
func (d *Deployer) planSource(ctx context.Context, s *Source) (*srcPlan, error) {
|
||||
fctx, end := d.phase(ctx, "source.fetch", "source", s.Name)
|
||||
err := d.syncMirror(fctx, d.srcMirror(s), s.Remote, nil, envRefspecs)
|
||||
end()
|
||||
if err != nil {
|
||||
d.count("failed")
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
bctx, end := d.phase(ctx, "source.branches", "source", s.Name)
|
||||
envs, errs := d.Environments(bctx, s, want)
|
||||
envs, skipped, err := d.Environments(bctx, s)
|
||||
end("envs", len(envs))
|
||||
for range errs {
|
||||
d.count("failed")
|
||||
}
|
||||
if envs == nil {
|
||||
return errors.Join(errs...)
|
||||
return &srcPlan{s: s, envs: envs, skipped: skipped}, err
|
||||
}
|
||||
|
||||
func (d *Deployer) deploySource(ctx context.Context, p *srcPlan, modules bool) error {
|
||||
s, full := p.s, p.want == nil
|
||||
var errs []error
|
||||
if full {
|
||||
errs = p.skipErrs(func(string) bool { return true })
|
||||
for range errs {
|
||||
d.count("failed")
|
||||
}
|
||||
}
|
||||
if err := os.MkdirAll(s.Basedir, 0o755); err != nil {
|
||||
return err
|
||||
return errors.Join(append(errs, err)...)
|
||||
}
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
for name, ref := range envs {
|
||||
found[name], found[ref.branch] = true, true
|
||||
for name, ref := range p.envs {
|
||||
if !full && !p.want[name] {
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
@@ -358,8 +428,8 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if len(want) == 0 {
|
||||
errs = append(errs, d.purge(s, envs))
|
||||
if full {
|
||||
errs = append(errs, d.purge(s, p.envs))
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user