Skip to content

Commit

Permalink
Merge pull request #1563 from jenkins-x/newtriggers
Browse files Browse the repository at this point in the history
feat: trigger on deploy status and periodically
  • Loading branch information
jenkins-x-bot authored Feb 25, 2024
2 parents 552e99c + 12192a8 commit 6e58825
Show file tree
Hide file tree
Showing 29 changed files with 1,226 additions and 117 deletions.
23 changes: 23 additions & 0 deletions .lighthouse/jenkins-x/pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ spec:
- name: jx-variables
- name: build-make-linux
resources: {}
- name: protect-credentials
securityContext:
runAsUser: 0
resources: {}
image: ghcr.io/distroless/busybox
script: |
#!/bin/sh
cp -a /tekton/home/.git-credentials /tekton/home/.git-credentials.bak
- name: build-make-test
resources: {}
- name: build-container-build:webhooks
Expand Down Expand Up @@ -73,6 +81,21 @@ spec:
source .jx/variables.sh
cp /tekton/creds-secrets/tekton-container-registry-auth/.dockerconfigjson /kaniko/.docker/config.json
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/gc/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-gc-jobs:$VERSION --build-arg=VERSION=$VERSION
- image: ghcr.io/jenkins-x/jx-boot:3.10.131
name: release-chart
resources: {}
script: |
#!/usr/bin/env sh
source .jx/variables.sh
if [ -d "charts/$REPO_NAME" ]; then
jx gitops yset -p version -v "$VERSION" -f ./charts/$REPO_NAME/Chart.yaml
jx gitops yset -p appVersion -v "$VERSION" -f ./charts/$REPO_NAME/Chart.yaml
jx gitops yset -p image.tag -v "$VERSION" -f ./charts/$REPO_NAME/values.yaml;
else echo no charts; fi
mv /tekton/home/.git-credentials.bak /tekton/home/.git-credentials
jx gitops helm release
podTemplate: {}
serviceAccountName: tekton-bot
timeout: 240h0m0s
Expand Down
2 changes: 1 addition & 1 deletion .lighthouse/jenkins-x/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ spec:
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/gc/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-gc-jobs:$VERSION --destination=ghcr.io/jenkins-x/lighthouse-gc-jobs:latest --build-arg=VERSION=$VERSION
- name: chart-docs
resources: {}
- image: ghcr.io/jenkins-x/jx-boot:3.10.73
- image: ghcr.io/jenkins-x/jx-boot:3.10.131
name: changelog
resources: {}
script: |
Expand Down
4 changes: 4 additions & 0 deletions charts/lighthouse/templates/webhooks-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ spec:
args:
- "--namespace={{ .Release.Namespace }}"
env:
- name: SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
- name: "GIT_KIND"
value: "{{ .Values.git.kind }}"
- name: "LH_CUSTOM_TRIGGER_COMMAND"
Expand Down
14 changes: 14 additions & 0 deletions charts/lighthouse/templates/webhooks-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ rules:
- create
- list
- watch
- patch
- delete
- apiGroups:
- ""
resources:
Expand All @@ -25,6 +27,18 @@ rules:
- get
- list
- watch
- apiGroups:
- batch
resources:
- cronjobs
verbs:
- get
- update
- create
- list
- watch
- patch
- delete
- apiGroups:
- lighthouse.jenkins.io
resources:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/tektoncd/pipeline v0.41.0
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
golang.org/x/oauth2 v0.15.0
gopkg.in/robfig/cron.v2 v2.0.0-20150107220207-be2e0b0deed5
k8s.io/api v0.25.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
6 changes: 1 addition & 5 deletions pkg/apis/lighthouse/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ func (s *LighthouseJobSpec) GetEnvVars() map[string]string {

env[JobSpecEnv] = fmt.Sprintf("type:%s", s.Type)

if s.Type == job.PeriodicJob {
return env
}

if s.Refs != nil {
env[RepoOwnerEnv] = s.Refs.Org
env[RepoNameEnv] = s.Refs.Repo
Expand All @@ -203,7 +199,7 @@ func (s *LighthouseJobSpec) GetEnvVars() map[string]string {
env[PullRefsEnv] = s.Refs.String()
}

if s.Type == job.PostsubmitJob || s.Type == job.BatchJob {
if s.Type != job.PresubmitJob {
return env
}

Expand Down
16 changes: 13 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ func (c *Config) GetPostsubmits(repository scm.Repository) []job.Postsubmit {
return answer
}

// GetDeployments lets return all the deployments
func (c *Config) GetDeployments(repository scm.Repository) []job.Deployment {
fullNames := util.FullNames(repository)
var answer []job.Deployment
for _, fn := range fullNames {
answer = append(answer, c.Deployments[fn]...)
}
return answer
}

// GetPresubmits lets return all the pre submits for the given repo
func (c *Config) GetPresubmits(repository scm.Repository) []job.Presubmit {
fullNames := util.FullNames(repository)
Expand All @@ -262,9 +272,9 @@ func (c *Config) GetPresubmits(repository scm.Repository) []job.Presubmit {
}

// BranchRequirements partitions status contexts for a given org, repo branch into three buckets:
// - contexts that are always required to be present
// - contexts that are required, _if_ present
// - contexts that are always optional
// - contexts that are always required to be present
// - contexts that are required, _if_ present
// - contexts that are always optional
func BranchRequirements(org, repo, branch string, presubmits map[string][]job.Presubmit) ([]string, []string, []string) {
jobs, ok := presubmits[org+"/"+repo]
if !ok {
Expand Down
46 changes: 23 additions & 23 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,29 @@ periodics:
- image: alpine`,
},
},
{
name: "duplicated periodics",
prowConfig: ``,
jobConfigs: []string{
`
periodics:
- cron: '* * * * *'
agent: tekton
name: foo
spec:
containers:
- image: alpine`,
`
periodics:
- cron: '* * * * *'
agent: tekton
name: foo
spec:
containers:
- image: alpine`,
},
expectError: true,
},
// {
// name: "duplicated periodics",
// prowConfig: ``,
// jobConfigs: []string{
// `
//periodics:
//- cron: '* * * * *'
// agent: tekton
// name: foo
// spec:
// containers:
// - image: alpine`,
// `
//periodics:
//- cron: '* * * * *'
// agent: tekton
// name: foo
// spec:
// containers:
// - image: alpine`,
// },
// expectError: true,
// },
{
name: "one presubmit no context should default",
prowConfig: ``,
Expand Down
9 changes: 5 additions & 4 deletions pkg/config/job/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type Config struct {
Presubmits map[string][]Presubmit `json:"presubmits,omitempty"`
Postsubmits map[string][]Postsubmit `json:"postsubmits,omitempty"`
// Periodics are not associated with any repo.
Periodics []Periodic `json:"periodics,omitempty"`
Periodics []Periodic `json:"periodics,omitempty"`
Deployments map[string][]Deployment `json:"deployments,omitempty"`
}

func resolvePresets(name string, labels map[string]string, spec *v1.PodSpec, presets []Preset) error {
Expand Down Expand Up @@ -160,9 +161,9 @@ func (c *Config) Validate(lh lighthouse.Config) error {
validPeriodics := sets.NewString()
// Ensure that the periodic durations are valid and specs exist.
for _, p := range c.Periodics {
if validPeriodics.Has(p.Name) {
return fmt.Errorf("duplicated periodic job : %s", p.Name)
}
//if validPeriodics.Has(p.Name) {
// return fmt.Errorf("duplicated periodic job : %s", p.Name)
//}
validPeriodics.Insert(p.Name)
if err := p.Base.Validate(PeriodicJob, lh.PodNamespace); err != nil {
return fmt.Errorf("invalid periodic job %s: %v", p.Name, err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/job/deployment.go
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"`
}
5 changes: 3 additions & 2 deletions pkg/config/job/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package job
// Periodic runs on a timer.
type Periodic struct {
Base
Reporter
// Cron representation of job trigger time
Cron string `json:"cron"`
// Tags for config entries
Tags []string `json:"tags,omitempty"`
// Branch to run job on. If not set default branch for repository is used
Branch string `json:"branch,omitempty"`
}

// SetDefaults initializes default values
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/job/pipelinekind.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
PostsubmitJob PipelineKind = "postsubmit"
// Periodic job means it runs on a time-basis, unrelated to git changes.
PeriodicJob PipelineKind = "periodic"
// Deployment job means it runs on deployment status event
DeploymentJob PipelineKind = "deployment"
// BatchJob tests multiple unmerged PRs at the same time.
BatchJob PipelineKind = "batch"
)
14 changes: 13 additions & 1 deletion pkg/jobutil/jobutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,22 @@ func PostsubmitSpec(logger *logrus.Entry, p job.Postsubmit, refs v1alpha1.Refs)
return pjs
}

// DeploymentSpec initializes a PipelineOptionsSpec for a given deployment job.
func DeploymentSpec(logger *logrus.Entry, p job.Deployment, refs v1alpha1.Refs) v1alpha1.LighthouseJobSpec {
pjs := specFromJobBase(logger, p.Base)
pjs.Type = job.DeploymentJob
pjs.Context = p.Context
pjs.Refs = completePrimaryRefs(refs, p.Base)

return pjs
}

// PeriodicSpec initializes a PipelineOptionsSpec for a given periodic job.
func PeriodicSpec(logger *logrus.Entry, p job.Periodic) v1alpha1.LighthouseJobSpec {
func PeriodicSpec(logger *logrus.Entry, p job.Periodic, refs v1alpha1.Refs) v1alpha1.LighthouseJobSpec {
pjs := specFromJobBase(logger, p.Base)
pjs.Type = job.PeriodicJob
pjs.Context = p.Context
pjs.Refs = completePrimaryRefs(refs, p.Base)

return pjs
}
Expand Down
21 changes: 11 additions & 10 deletions pkg/plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (

// Plugin defines a plugin and its handlers
type Plugin struct {
Description string
ExcludedProviders sets.String
ConfigHelpProvider ConfigHelpProvider
IssueHandler IssueHandler
PullRequestHandler PullRequestHandler
PushEventHandler PushEventHandler
ReviewEventHandler ReviewEventHandler
StatusEventHandler StatusEventHandler
GenericCommentHandler GenericCommentHandler
Commands []Command
Description string
ExcludedProviders sets.String
ConfigHelpProvider ConfigHelpProvider
IssueHandler IssueHandler
PullRequestHandler PullRequestHandler
PushEventHandler PushEventHandler
ReviewEventHandler ReviewEventHandler
StatusEventHandler StatusEventHandler
DeploymentStatusHandler DeploymentStatusHandler
GenericCommentHandler GenericCommentHandler
Commands []Command
}

// InvokeCommandHandler calls InvokeHandler on all commands
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type PullRequestHandler func(Agent, scm.PullRequestHook) error
// StatusEventHandler defines the function contract for a scm.Status handler.
type StatusEventHandler func(Agent, scm.Status) error

type DeploymentStatusHandler func(Agent, scm.DeploymentStatusHook) error

// PushEventHandler defines the function contract for a scm.PushHook handler.
type PushEventHandler func(Agent, scm.PushHook) error

Expand Down
42 changes: 42 additions & 0 deletions pkg/plugins/trigger/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package trigger

import (
"strings"

"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 != "" && !strings.EqualFold(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.")
lj, err := c.LauncherClient.Launch(&pj)
if err != nil {
return err
}
c.Logger.WithFields(jobutil.LighthouseJobFields(lj)).Debug("LighthouseJob created")
}
return nil
}
Loading

0 comments on commit 6e58825

Please sign in to comment.