Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not run clone step if no pipeline step will run #877

Merged
merged 14 commits into from
May 18, 2022
10 changes: 9 additions & 1 deletion pipeline/frontend/yaml/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config {
config.Stages = append(config.Stages, stage)
}

var stages []*backend.Stage
// add pipeline steps. 1 pipeline step per stage, at the moment
var stage *backend.Stage
var group string
Expand All @@ -202,14 +203,21 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config {
stage = new(backend.Stage)
stage.Name = fmt.Sprintf("%s_stage_%v", c.prefix, i)
stage.Alias = container.Name
config.Stages = append(config.Stages, stage)
stages = append(stages, stage)
}

name := fmt.Sprintf("%s_step_%d", c.prefix, i)
step := c.createProcess(name, container, namePipeline)
stage.Steps = append(stage.Steps, step)
}

if len(stages) == 0 {
// nothing will run, remove services and clone step
config.Stages = []*backend.Stage{}
} else {
config.Stages = append(config.Stages, stages...)
}

c.setupCacheRebuild(conf, config)

return config
Expand Down
26 changes: 18 additions & 8 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,19 @@ func PostHook(c *gin.Context) {
return
}

if zeroSteps(build, remoteYamlConfigs) {
// TODO: move global pipeline filters into own check functions ...
if z, steps := zeroSteps(build, remoteYamlConfigs); z {
6543 marked this conversation as resolved.
Show resolved Hide resolved
msg := "ignoring hook: step conditions yield zero runnable steps"
log.Debug().Str("repo", repo.FullName).Msg(msg)
c.String(http.StatusOK, msg)
build.Status = model.StatusSuccess
for _, step := range steps {
step.Proc.State = model.StatusSuccess
build.Procs = append(build.Procs, step.Proc)
}
if err := updateBuildStatus(c, build, repo, repoUser); err != nil {
6543 marked this conversation as resolved.
Show resolved Hide resolved
log.Error().Err(err).Msg("updateBuildStatus")
}
return
}

Expand Down Expand Up @@ -292,7 +301,7 @@ func branchFiltered(build *model.Build, remoteYamlConfigs []*remote.FileMeta) (b
return true, nil
}

func zeroSteps(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
func zeroSteps(build *model.Build, remoteYamlConfigs []*remote.FileMeta) (bool, []*shared.BuildItem) {
b := shared.ProcBuilder{
Repo: &model.Repo{},
Curr: build,
Expand All @@ -304,15 +313,16 @@ func zeroSteps(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
Yamls: remoteYamlConfigs,
}

buildItems, err := b.Build()
buildItemsNoEmpty, err := b.Build()
if err != nil {
return false
return false, []*shared.BuildItem{}
}
if len(buildItems) == 0 {
return true
b.IncludeEmpty = true
buildItems, err := b.Build()
if err != nil {
return false, []*shared.BuildItem{}
}

return false
return len(buildItemsNoEmpty) == 0, buildItems
}

func findOrPersistPipelineConfig(store store.Store, build *model.Build, remoteYamlConfig *remote.FileMeta) (*model.Config, error) {
Expand Down
22 changes: 11 additions & 11 deletions server/shared/procBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ import (

// ProcBuilder Takes the hook data and the yaml and returns in internal data model
type ProcBuilder struct {
Repo *model.Repo
Curr *model.Build
Last *model.Build
Netrc *model.Netrc
Secs []*model.Secret
Regs []*model.Registry
Link string
Yamls []*remote.FileMeta
Envs map[string]string
Repo *model.Repo
Curr *model.Build
Last *model.Build
Netrc *model.Netrc
Secs []*model.Secret
Regs []*model.Registry
Link string
Yamls []*remote.FileMeta
Envs map[string]string
IncludeEmpty bool
}

type BuildItem struct {
Expand Down Expand Up @@ -114,7 +115,7 @@ func (b *ProcBuilder) Build() ([]*BuildItem, error) {

ir := b.toInternalRepresentation(parsed, environ, metadata, proc.ID)

if len(ir.Stages) == 0 {
if len(ir.Stages) == 0 && !b.IncludeEmpty {
continue
}

Expand All @@ -138,7 +139,6 @@ func (b *ProcBuilder) Build() ([]*BuildItem, error) {
items = filterItemsWithMissingDependencies(items)

// check if at least one proc can start, if list is not empty
procListContainsItemsToRun(items)
if len(items) > 0 && !procListContainsItemsToRun(items) {
return nil, fmt.Errorf("build has no startpoint")
}
Expand Down