Branch conditions on pipelines

This commit is contained in:
Laszlo Fogas 2019-06-19 09:36:54 +02:00
parent fded153328
commit eed92421c9
3 changed files with 74 additions and 8 deletions

View file

@ -33,6 +33,7 @@ import (
"github.com/laszlocph/drone-oss-08/shared/token" "github.com/laszlocph/drone-oss-08/shared/token"
"github.com/laszlocph/drone-oss-08/store" "github.com/laszlocph/drone-oss-08/store"
"github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/frontend/yaml"
"github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/rpc" "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/rpc"
"github.com/laszlocph/drone-oss-08/cncd/pubsub" "github.com/laszlocph/drone-oss-08/cncd/pubsub"
"github.com/laszlocph/drone-oss-08/cncd/queue" "github.com/laszlocph/drone-oss-08/cncd/queue"
@ -150,14 +151,10 @@ func PostHook(c *gin.Context) {
return return
} }
// verify that pipeline can be built at all if branchFiltered(build, remoteYamlConfigs) {
// parsedPipelineConfig, err := yaml.ParseString(conf.Data) c.String(200, "Branch does not match restrictions defined in yaml")
// if err == nil { return
// if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy { }
// c.String(200, "Branch does not match restrictions defined in yaml")
// return
// }
// }
if repo.IsGated { // This feature is not clear to me. Reenabling once better understood if repo.IsGated { // This feature is not clear to me. Reenabling once better understood
build.Status = model.StatusBlocked build.Status = model.StatusBlocked
@ -262,6 +259,19 @@ func PostHook(c *gin.Context) {
queueBuild(build, repo, buildItems) queueBuild(build, repo, buildItems)
} }
func branchFiltered(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
for _, remoteYamlConfig := range remoteYamlConfigs {
parsedPipelineConfig, err := yaml.ParseString(string(remoteYamlConfig.Data))
if err == nil {
if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy {
} else {
return false
}
}
}
return true
}
func findOrPersistPipelineConfig(build *model.Build, remoteYamlConfig *remote.FileMeta) (*model.Config, error) { func findOrPersistPipelineConfig(build *model.Build, remoteYamlConfig *remote.FileMeta) (*model.Config, error) {
sha := shasum(remoteYamlConfig.Data) sha := shasum(remoteYamlConfig.Data)
conf, err := Config.Storage.Config.ConfigFindIdentical(build.RepoID, sha) conf, err := Config.Storage.Config.ConfigFindIdentical(build.RepoID, sha)
@ -315,6 +325,9 @@ func publishToTopic(c *gin.Context, build *model.Build, repo *model.Repo) {
func queueBuild(build *model.Build, repo *model.Repo, buildItems []*buildItem) { func queueBuild(build *model.Build, repo *model.Repo, buildItems []*buildItem) {
var tasks []*queue.Task var tasks []*queue.Task
for _, item := range buildItems { for _, item := range buildItems {
if item.Proc.State == model.StatusSkipped {
continue
}
task := new(queue.Task) task := new(queue.Task)
task.ID = fmt.Sprint(item.Proc.ID) task.ID = fmt.Sprint(item.Proc.ID)
task.Labels = map[string]string{} task.Labels = map[string]string{}

View file

@ -103,6 +103,10 @@ func (b *procBuilder) Build() ([]*buildItem, error) {
return nil, lerr return nil, lerr
} }
if !parsed.Branches.Match(b.Curr.Branch) {
proc.State = model.StatusSkipped
}
metadata.SetPlatform(parsed.Platform) metadata.SetPlatform(parsed.Platform)
ir := b.toInternalRepresentation(parsed, environ, metadata, proc.ID) ir := b.toInternalRepresentation(parsed, environ, metadata, proc.ID)
@ -220,6 +224,9 @@ func setBuildSteps(build *model.Build, buildItems []*buildItem) {
PGID: gid, PGID: gid,
State: model.StatusPending, State: model.StatusPending,
} }
if item.Proc.State == model.StatusSkipped {
proc.State = model.StatusSkipped
}
build.Procs = append(build.Procs, proc) build.Procs = append(build.Procs, proc)
} }
} }

View file

@ -158,3 +158,49 @@ runs_on:
t.Fatal("Should run on failure") t.Fatal("Should run on failure")
} }
} }
func TestBranchFilter(t *testing.T) {
b := procBuilder{
Repo: &model.Repo{},
Curr: &model.Build{Branch: "dev"},
Last: &model.Build{},
Netrc: &model.Netrc{},
Secs: []*model.Secret{},
Regs: []*model.Registry{},
Link: "",
Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(`
pipeline:
xxx:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
branches: master
`)},
&remote.FileMeta{Data: []byte(`
pipeline:
build:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
},
}
buildItems, err := b.Build()
if err != nil {
t.Fatal(err)
}
if len(buildItems) != 2 {
t.Fatal("Should have generated 2 buildItems")
}
if buildItems[0].Proc.State != model.StatusSkipped {
t.Fatal("Should not run on dev branch")
}
for _, child := range buildItems[0].Proc.Children {
if child.State != model.StatusSkipped {
t.Fatal("Children should skipped status too")
}
}
if buildItems[1].Proc.State != model.StatusPending {
t.Fatal("Should not run on dev branch")
}
}