-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trigger on deploy status and periodically
- Loading branch information
Showing
18 changed files
with
357 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package job | ||
|
||
type Deployment struct { | ||
Base | ||
Reporter | ||
// The deployment state that trigger this pipeline | ||
// Can be one of: error, failure, inactive, in_progress, queued, pending, success | ||
// If not set all deployment state event triggers | ||
State string `json:"state,omitempty"` | ||
// Deployment for this environment trigger this pipeline | ||
// If not set deployments for all environments trigger | ||
Environment string `json:"environment,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package trigger | ||
|
||
import ( | ||
"github.com/jenkins-x/go-scm/scm" | ||
"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1" | ||
"github.com/jenkins-x/lighthouse/pkg/jobutil" | ||
"github.com/jenkins-x/lighthouse/pkg/scmprovider" | ||
) | ||
|
||
func handleDeployment(c Client, ds scm.DeploymentStatusHook) error { | ||
for _, j := range c.Config.GetDeployments(ds.Repo) { | ||
if j.State != "" && j.State != ds.DeploymentStatus.State { | ||
continue | ||
} | ||
if j.Environment != "" && j.Environment != ds.Deployment.Environment { | ||
continue | ||
} | ||
labels := make(map[string]string) | ||
for k, v := range j.Labels { | ||
labels[k] = v | ||
} | ||
refs := v1alpha1.Refs{ | ||
Org: ds.Repo.Namespace, | ||
Repo: ds.Repo.Name, | ||
BaseRef: ds.Deployment.Ref, | ||
BaseSHA: ds.Deployment.Sha, | ||
BaseLink: ds.Deployment.RepositoryLink, | ||
CloneURI: ds.Repo.Clone, | ||
} | ||
labels[scmprovider.EventGUID] = ds.DeploymentStatus.ID | ||
pj := jobutil.NewLighthouseJob(jobutil.DeploymentSpec(c.Logger, j, refs), labels, j.Annotations) | ||
c.Logger.WithFields(jobutil.LighthouseJobFields(&pj)).Info("Creating a new LighthouseJob.") | ||
if _, err := c.LauncherClient.Launch(&pj); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package trigger | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1" | ||
"github.com/jenkins-x/lighthouse/pkg/config" | ||
"github.com/jenkins-x/lighthouse/pkg/config/job" | ||
"github.com/jenkins-x/lighthouse/pkg/filebrowser" | ||
"github.com/jenkins-x/lighthouse/pkg/jobutil" | ||
"github.com/jenkins-x/lighthouse/pkg/scmprovider" | ||
"github.com/jenkins-x/lighthouse/pkg/triggerconfig/inrepo" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"gopkg.in/robfig/cron.v2" | ||
) | ||
|
||
type PeriodicExec struct { | ||
job.Periodic | ||
Owner, Repo string | ||
LauncherClient launcher | ||
} | ||
|
||
func (p *PeriodicExec) Run() { | ||
labels := make(map[string]string) | ||
for k, v := range p.Labels { | ||
labels[k] = v | ||
} | ||
refs := v1alpha1.Refs{ | ||
Org: p.Owner, | ||
Repo: p.Repo, | ||
BaseRef: p.Branch, | ||
} | ||
l := logrus.WithField(scmprovider.RepoLogField, p.Repo).WithField(scmprovider.OrgLogField, p.Owner) | ||
|
||
pj := jobutil.NewLighthouseJob(jobutil.PeriodicSpec(l, p.Periodic, refs), labels, p.Annotations) | ||
l.WithFields(jobutil.LighthouseJobFields(&pj)).Info("Creating a new LighthouseJob.") | ||
_, err := p.LauncherClient.Launch(&pj) | ||
if err != nil { | ||
l.WithError(err).Error("Failed to create lighthouse job for cron ") | ||
} | ||
} | ||
|
||
func StartPeriodics(configAgent *config.Agent, launcher launcher, fileBrowsers *filebrowser.FileBrowsers) { | ||
periodics := cron.New() | ||
periodicExists := false | ||
resolverCache := inrepo.NewResolverCache() | ||
fc := filebrowser.NewFetchCache() | ||
c := configAgent.Config() | ||
for fullName := range c.InRepoConfig.Enabled { | ||
repo := strings.SplitN(fullName, "/", 2) | ||
if len(repo) != 2 { | ||
logrus.Errorf("Wrong format of %s, not owner/repo", fullName) | ||
continue | ||
} | ||
// TODO Ensure that the repo clones are removed afterwards and deregistered | ||
cfg, err := inrepo.LoadTriggerConfig(fileBrowsers, fc, resolverCache, repo[0], repo[1], "") | ||
if err != nil { | ||
logrus.Error(errors.Wrapf(err, "failed to calculate in repo config")) | ||
continue | ||
} | ||
|
||
for i := range cfg.Spec.Periodics { | ||
periodic := cfg.Spec.Periodics[i] | ||
// TODO Support rereading periodics configuration | ||
_, err := periodics.AddJob(periodic.Cron, &PeriodicExec{ | ||
Periodic: periodic, | ||
Owner: repo[0], | ||
Repo: repo[1], | ||
LauncherClient: launcher, | ||
}) | ||
if err != nil { | ||
logrus.Error(errors.Wrapf(err, fmt.Sprintf("failed to schedule job %s in %s", periodic.Name, fullName))) | ||
} else { | ||
periodicExists = true | ||
} | ||
} | ||
} | ||
|
||
if periodicExists { | ||
periodics.Start() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.