From f98d82042402f9922ac59ad0823690af305672f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Svantesson?= Date: Wed, 26 Jul 2023 23:16:33 +0200 Subject: [PATCH] feat: trigger on deploy status and periodically --- pkg/config/job/periodic.go | 2 -- pkg/plugins/trigger/periodic.go | 9 ++++++--- pkg/webhook/events.go | 7 ++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/config/job/periodic.go b/pkg/config/job/periodic.go index f5bc496d1..471d7d99e 100644 --- a/pkg/config/job/periodic.go +++ b/pkg/config/job/periodic.go @@ -20,8 +20,6 @@ package job type Periodic struct { Base Reporter - // The branch to build - Branch string `json:"branch"` // Cron representation of job trigger time Cron string `json:"cron"` } diff --git a/pkg/plugins/trigger/periodic.go b/pkg/plugins/trigger/periodic.go index b58dbb681..b7a9541e6 100644 --- a/pkg/plugins/trigger/periodic.go +++ b/pkg/plugins/trigger/periodic.go @@ -37,6 +37,10 @@ func (p PeriodicAgent) UpdatePeriodics(org string, repo string, agent plugins.Ag // Probably better then to just create CronJobs that create LighthouseJobs/PipelineRuns using kubectl/tkn. // Possibly with the Pipeline stored as separate resource and then either have the LighthouseJob refer to it or do tkn pipeline start. // With proper labels these CronJobs/Pipelines could be handled fairly efficiently + // So with LighthouseJobs it could be rendered and put in a configmap which is mounted in the cronjob. + // Then kubectl apply -f to create the job and then to set the status kubectl patch LighthouseJob myresource --type=merge --subresource status --patch 'status: {state: triggered}' + // Would really only need to run StartPeriodics when in a new cluster. How do I know when it is needed? I would + // need to store in cluster when StartPeriodics has been run. repoPeriodics := maps.Clone(p.Periodics[fullName]) if repoPeriodics == nil { repoPeriodics = make(map[string]PeriodicExec) @@ -87,9 +91,8 @@ func (p *PeriodicExec) Run() { labels[k] = v } refs := v1alpha1.Refs{ - Org: p.Owner, - Repo: p.Repo, - BaseRef: p.Branch, + Org: p.Owner, + Repo: p.Repo, } l := logrus.WithField(scmprovider.RepoLogField, p.Repo).WithField(scmprovider.OrgLogField, p.Owner) diff --git a/pkg/webhook/events.go b/pkg/webhook/events.go index eb3cf9316..ac5f08816 100644 --- a/pkg/webhook/events.go +++ b/pkg/webhook/events.go @@ -22,6 +22,7 @@ import ( "net/url" "regexp" "strconv" + "strings" "sync" lru "github.com/hashicorp/golang-lru" @@ -218,7 +219,11 @@ func (s *Server) handlePushEvent(l *logrus.Entry, pe *scm.PushHook) { }(p, h.PushEventHandler) } } - s.PeriodicAgent.UpdatePeriodics(repo.Namespace, repo.Name, agent, pe) + // Update periodics from the default branch + refBranch := strings.TrimPrefix(pe.Ref, "refs/heads/") + if refBranch == pe.Repository().Branch { + s.PeriodicAgent.UpdatePeriodics(repo.Namespace, repo.Name, agent, pe) + } l.WithField("count", strconv.Itoa(c)).Info("number of push handlers") }() }