Skip to content

Commit 645fd37

Browse files
infernus01tekton-robot
authored andcommitted
Add tkn triggers bootstrap command for automated setup
Signed-off-by: Shubham Bhardwaj <shubbhar@redhat.com>
1 parent 70eaf7e commit 645fd37

File tree

15 files changed

+1757
-0
lines changed

15 files changed

+1757
-0
lines changed

cmd/tkn-triggers/cmd/bootstrap.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
Copyright 2025 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"log"
23+
"os"
24+
25+
"github.com/spf13/cobra"
26+
"github.com/tektoncd/triggers/pkg/bootstrap"
27+
"k8s.io/client-go/rest"
28+
"k8s.io/client-go/tools/clientcmd"
29+
)
30+
31+
var (
32+
// Bootstrap flags
33+
kubeconfig string
34+
35+
// GitHub integration flags
36+
githubRepo string
37+
githubToken string
38+
publicDomain string
39+
webhookSecret string
40+
)
41+
42+
// bootstrapCmd represents the bootstrap command
43+
var bootstrapCmd = &cobra.Command{
44+
Use: "bootstrap",
45+
Short: "Bootstrap Tekton Triggers with all necessary resources",
46+
Long: `Bootstrap sets up Tekton Triggers with all necessary resources following
47+
the getting-started guide. This includes:
48+
49+
- Installing Tekton Pipelines (if not present)
50+
- Installing Tekton Triggers (if not present)
51+
- Creating the getting-started namespace
52+
- Setting up RBAC (ServiceAccounts, Roles, RoleBindings)
53+
- Creating EventListener, TriggerTemplate, TriggerBinding
54+
- Creating example Pipeline and Tasks
55+
56+
Examples:
57+
# Basic bootstrap (auto-detects kubeconfig, creates all resources)
58+
tkn triggers bootstrap`,
59+
RunE: bootstrapRun,
60+
}
61+
62+
func init() {
63+
bootstrapCmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to kubeconfig file")
64+
bootstrapCmd.Flags().StringVar(&githubRepo, "github-repo", "", "GitHub repository (owner/repo)")
65+
bootstrapCmd.Flags().StringVar(&githubToken, "github-token", "", "GitHub personal access token")
66+
bootstrapCmd.Flags().StringVar(&publicDomain, "domain", "", "Public domain for webhooks (e.g. myapp.com)")
67+
bootstrapCmd.Flags().StringVar(&webhookSecret, "webhook-secret", "", "Webhook secret (auto-generated if empty)")
68+
}
69+
70+
func bootstrapRun(cmd *cobra.Command, args []string) error {
71+
ctx := context.Background()
72+
73+
// Create kubernetes config
74+
var config *rest.Config
75+
var err error
76+
if kubeconfig != "" {
77+
// Use kubeconfig
78+
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
79+
} else {
80+
// try ~/.kube/config first, then in-cluster
81+
kubeconfigPath := os.Getenv("HOME") + "/.kube/config"
82+
config, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath)
83+
if err != nil {
84+
config, err = rest.InClusterConfig()
85+
}
86+
}
87+
if err != nil {
88+
return fmt.Errorf("failed to build kubeconfig: %w", err)
89+
}
90+
91+
// Create bootstrap configuration for getting-started resources
92+
log.Println("== Setting up Tekton Dependencies...")
93+
cfg := &bootstrap.Config{
94+
Namespace: "getting-started",
95+
Provider: "github",
96+
InstallDeps: true,
97+
CreateExamples: true,
98+
KubeConfig: config,
99+
100+
GitHubRepo: "",
101+
GitHubToken: "",
102+
PublicDomain: "",
103+
WebhookSecret: "",
104+
}
105+
106+
// Create bootstrapper
107+
bootstrapper, err := bootstrap.New(cfg)
108+
if err != nil {
109+
return fmt.Errorf("failed to create bootstrapper: %w", err)
110+
}
111+
112+
// Run bootstrap (create namespace, RBAC, EventListener, Pipeline)
113+
if err := bootstrapper.Run(ctx); err != nil {
114+
return fmt.Errorf("bootstrap failed: %w", err)
115+
}
116+
117+
log.Println("Tekton Dependencies are ready!")
118+
119+
// run GitHub integration setup
120+
log.Println("\n== Configuring GitHub Integration ==")
121+
if err := runInteractiveSetup(); err != nil {
122+
log.Printf("⚠️ GitHub setup failed: %v\n", err)
123+
return nil
124+
}
125+
126+
// GitHub webhook creation if have the required info
127+
if githubRepo != "" && githubToken != "" && publicDomain != "" {
128+
log.Println("== Creating GitHub webhook ==")
129+
githubConfig := &bootstrap.Config{
130+
Namespace: "getting-started",
131+
GitHubRepo: githubRepo,
132+
GitHubToken: githubToken,
133+
PublicDomain: publicDomain,
134+
WebhookSecret: webhookSecret,
135+
KubeConfig: config,
136+
}
137+
138+
// Create GitHub manager and setup webhook
139+
githubManager := bootstrap.NewGitHubManager(githubConfig)
140+
if err := githubManager.SetupWebhook(ctx); err != nil {
141+
return fmt.Errorf("GitHub webhook setup failed: %w", err)
142+
}
143+
log.Println("GitHub webhook created!")
144+
} else {
145+
log.Println("⚠️ Missing required information")
146+
}
147+
return nil
148+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2025 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"bufio"
21+
"log"
22+
"os"
23+
"strings"
24+
)
25+
26+
func runInteractiveSetup() error {
27+
reader := bufio.NewReader(os.Stdin)
28+
if githubRepo != "" && publicDomain != "" && githubToken != "" {
29+
log.Println("GitHub configuration already there")
30+
return nil
31+
}
32+
// Get GitHub repository
33+
if githubRepo == "" {
34+
log.Print("\nEnter your GitHub repository (owner/repo): ")
35+
repo, _ := reader.ReadString('\n')
36+
githubRepo = strings.TrimSpace(repo)
37+
if githubRepo == "" {
38+
log.Println("Skipping GitHub integration.")
39+
return nil
40+
}
41+
}
42+
43+
// Get public domain
44+
if publicDomain == "" {
45+
log.Print("Enter your public route URL (e.g. myapp.example.com): ")
46+
domain, _ := reader.ReadString('\n')
47+
publicDomain = strings.TrimSpace(domain)
48+
if publicDomain == "" {
49+
log.Println("Skipping GitHub integration - public domain required.")
50+
return nil
51+
}
52+
}
53+
54+
// Get GitHub token
55+
if githubToken == "" {
56+
log.Print("Enter your GitHub personal access token: ")
57+
token, _ := reader.ReadString('\n')
58+
githubToken = strings.TrimSpace(token)
59+
if githubToken == "" {
60+
log.Println("Skipping GitHub integration - token required for webhook creation.")
61+
return nil
62+
}
63+
}
64+
return nil
65+
}

cmd/tkn-triggers/cmd/root.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2025 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
var rootCmd = &cobra.Command{
24+
Use: "tkn-triggers",
25+
Short: "Tekton Triggers CLI plugin",
26+
Long: `tkn-triggers is a CLI plugin for Tekton Triggers that provides
27+
command for bootstrapping Tekton Triggers resources.`,
28+
}
29+
30+
func Execute() error {
31+
return rootCmd.Execute()
32+
}
33+
34+
func init() {
35+
rootCmd.AddCommand(bootstrapCmd)
36+
}

cmd/tkn-triggers/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"os"
21+
22+
"github.com/tektoncd/triggers/cmd/tkn-triggers/cmd"
23+
)
24+
25+
func main() {
26+
if err := cmd.Execute(); err != nil {
27+
os.Exit(1)
28+
}
29+
}

docs/getting-started/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@
33
The following tutorial walks you through building and deploying a Docker image using
44
Tekton Triggers to detect a GitHub webhook request and execute a `Pipeline`.
55

6+
## Quick Start with CLI (Automated)
7+
8+
You can now automate this entire getting started guide with a single CLI command:
9+
10+
```bash
11+
tkn triggers bootstrap
12+
```
13+
14+
This command will:
15+
- Check and install Tekton Pipelines (if not present)
16+
- Check and install Tekton Triggers (if not present)
17+
- Create the `getting-started` namespace
18+
- Set up RBAC (ServiceAccounts, Roles, RoleBindings)
19+
- Create EventListener, TriggerTemplate, TriggerBinding
20+
- Create example Pipeline and Tasks
21+
- Configure GitHub webhook integration
22+
23+
### Supported Providers
24+
25+
Currently, only **GitHub** webhooks are supported. Support for GitLab and Bitbucket may be added in the future.
26+
27+
### Usage
28+
29+
```bash
30+
tkn triggers bootstrap
31+
32+
# Specify custom kubeconfig
33+
tkn triggers bootstrap --kubeconfig /path/to/kubeconfig
34+
35+
# Provide GitHub configuration via flags
36+
tkn triggers bootstrap \
37+
--github-repo <owner/repo> \
38+
--github-token <ghp_xxxxx> \
39+
--domain <myapp.example.com>
40+
```
41+
42+
The command will prompt you for GitHub repository, personal access token, and public domain if not provided via flags.
43+
44+
**Note:** If you prefer to set up everything manually, continue with the step-by-step tutorial below.
45+
646
## Overview
747

848
In this tutorial, you will:

0 commit comments

Comments
 (0)