Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/trigger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Lighthouse custome triggers

You can enable use LH_CUSTOM_TRIGGER_COMMAND environment variable to add custom triggers. It takes in a comma seperated list of commands.

When these commands are triggered using ChatOps Lighthouse will convert the command arg into TRIGGER_COMMAND_ARG enviroment variable which you can refer from the pipeline.

eg:
`
LH_CUSTOM_TRIGGER_COMMAND=deploy,trigger
`
```
apiVersion: config.lighthouse.jenkins-x.io/v1alpha1
kind: TriggerConfig
spec:
presubmits:
- name: pr
context: "pr"
always_run: true
optional: false
source: "pullrequest.yaml"
- name: trigger
context: "trigger"
always_run: false
optional: false
rerun_command: /trigger this
trigger: (?m)^/trigger( all| this),?(\s+|$)
source: "trigger.yaml"
- name: deploy
context: "deployr"
always_run: false
optional: false
rerun_command: /deploy dev
trigger: (?m)^/deploy(?:[ \t]+([-\w]+(?:,[-\w]+)*))?(?:[ \t]+([-\w]+(?:,[-\w]+)*))?
source: "deploy.yaml"
postsubmits:
- name: release
context: "release"
source: "release.yaml"
branches:
- ^main$
```
you can use ChatOps command to trigger each pipeline
eg:
***/deploy qa***
14 changes: 13 additions & 1 deletion pkg/plugins/trigger/generic-comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
)

func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.GenericCommentEvent) error {
func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.GenericCommentEvent) error{
return handleGenericCommentWithArg(c, trigger, gc, "")

}
func handleGenericCommentWithArg(c Client, trigger *plugins.Trigger, gc scmprovider.GenericCommentEvent, arg string) error {
org := gc.Repo.Namespace
repo := gc.Repo.Name
number := gc.Number
Expand Down Expand Up @@ -94,6 +98,14 @@ func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.Gen
if err != nil {
return err
}
if arg != "" {
for i := range toTest {
toTest[i].Base.PipelineRunParams = append(toTest[i].Base.PipelineRunParams , job.PipelineRunParam{
Name: "TRIGGER_COMMAND_ARG",
ValueTemplate: arg,
})
}
}
Comment on lines +97 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature should be documented and unit test added

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

836cdca adding in unit tests. please let me know if I have missed anything. I updated the code so that handleGenericComment take in the command arg.
documentation for this feature is in

When these commands are triggered using ChatOps, Lighthouse will store the command arg in TRIGGER_COMMAND_ARG PipelineRunPara which you can refer from the pipeline.

return RunAndSkipJobs(c, pr, toTest, toSkip, gc.GUID, trigger.ElideSkippedContexts)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/plugins/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ var plugin = plugins.Plugin{

func init() {
customTriggerCommand := os.Getenv(customerTriggerCommandEnvVar)
if customTriggerCommand != "" {
for _, trigger := range strings.Split(customTriggerCommand, ","){
Copy link
Contributor

@dippynark dippynark Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not already do this using a regex pattern for the customDeploymentTriggerCommand like: build|plan|apply?

We are doing this currently to trigger builds with /build commands and Terraform plans/applies with /plan and /apply commands. Perhaps the docs just need to be updated to make this clearer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. This is interpreted as a regexp, so I think you should revert this change and update the documentation and tests accordingly.

customCommand := plugins.Command{
Name: customTriggerCommand,
Name: trigger,
Arg: &plugins.CommandArg{
Pattern: `[-\w]+(?:,[-\w]+)*`,
},
Description: fmt.Sprintf("Manually trigger /%s chatops commands.", customTriggerCommand),
Description: fmt.Sprintf("Manually trigger /%s chatops commands.", trigger),
Featured: true,
Action: plugins.
Invoke(handleGenericCommentEvent).
Expand Down Expand Up @@ -170,8 +170,8 @@ func handlePullRequest(pc plugins.Agent, pr scm.PullRequestHook) error {
return handlePR(getClient(pc), pc.PluginConfig.TriggerFor(org, repo), pr)
}

func handleGenericCommentEvent(_ plugins.CommandMatch, pc plugins.Agent, gc scmprovider.GenericCommentEvent) error {
return handleGenericComment(getClient(pc), pc.PluginConfig.TriggerFor(gc.Repo.Namespace, gc.Repo.Name), gc)
func handleGenericCommentEvent(cm plugins.CommandMatch, pc plugins.Agent, gc scmprovider.GenericCommentEvent) error {
return handleGenericCommentWithArg(getClient(pc), pc.PluginConfig.TriggerFor(gc.Repo.Namespace, gc.Repo.Name), gc, cm.Arg)
}

func handlePush(pc plugins.Agent, pe scm.PushHook) error {
Expand Down