Skip to content

Commit 8835c76

Browse files
committed
feat: Add GitBranch field to HeliosAppSpec and create Tekton templates for CI/CD
1 parent 5a6e839 commit 8835c76

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

api/v1/heliosapp_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type HeliosAppSpec struct {
3131
// +kubebuilder:validation:Required
3232
GitRepo string `json:"gitRepo"`
3333

34+
// +kubebuilder:validation:Optional
35+
// +kubebuilder:default="main"
36+
GitBranch string `json:"gitBranch,omitempty"`
37+
3438
// +kubebuilder:validation:Required
3539
ImageRepo string `json:"imageRepo"`
3640

application-template.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: Application
3+
metadata:
4+
# Operator sẽ điền tên app và namespace vào đây
5+
name: <app-name>
6+
namespace: argocd # Hoặc namespace nơi ArgoCD được cài đặt
7+
spec:
8+
project: default
9+
source:
10+
# Operator sẽ điền repo URL và branch từ HeliosApp CR
11+
repoURL: <git-repo>
12+
targetRevision: <git-branch>
13+
# Đường dẫn tới manifest trong repo. Bạn có thể để cố định hoặc thêm vào CRD nếu cần
14+
path: deploy/kubernetes
15+
destination:
16+
server: https://kubernetes.default.svc
17+
# Operator sẽ điền namespace nơi ứng dụng sẽ được deploy
18+
namespace: <app-namespace>
19+
syncPolicy:
20+
automated:
21+
prune: true
22+
selfHeal: true
23+
syncOptions:
24+
- CreateNamespace=true

config/crd/bases/platform.helios.io_heliosapps.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ spec:
4141
properties:
4242
gitRepo:
4343
type: string
44+
gitBranch:
45+
type: string
4446
imageRepo:
4547
type: string
4648
port:

tekton-pipeline-template.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# File này định nghĩa các bước trong một quy trình CI, ví dụ: clone code, build image, và đẩy image lên registry.
2+
apiVersion: tekton.dev/v1beta1
3+
kind: Pipeline
4+
metadata:
5+
# Operator sẽ điền tên pipeline
6+
name: <pipeline-name>
7+
spec:
8+
params:
9+
- name: git-repo
10+
type: string
11+
description: The git repository URL to clone from.
12+
- name: git-revision
13+
type: string
14+
description: The git revision to clone.
15+
- name: image-repo
16+
type: string
17+
description: The container image repository to push to.
18+
workspaces:
19+
- name: source-code
20+
description: The workspace where source code is cloned.
21+
tasks:
22+
- name: git-clone
23+
taskRef:
24+
name: git-clone # Sử dụng Task có sẵn từ Tekton Hub
25+
params:
26+
- name: url
27+
value: $(params.git-repo)
28+
- name: revision
29+
value: $(params.git-revision)
30+
workspaces:
31+
- name: output
32+
workspace: source-code
33+
34+
- name: build-and-push
35+
runAfter: ["git-clone"]
36+
taskRef:
37+
name: kaniko # Sử dụng Task kaniko để build image trong cluster
38+
params:
39+
- name: IMAGE
40+
value: $(params.image-repo):$(tasks.git-clone.results.commit) # Gắn tag image bằng commit hash
41+
workspaces:
42+
- name: source
43+
workspace: source-code

tekton-trigger-template.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Phần này bao gồm TriggerTemplate và EventListener để tự động kích hoạt Pipeline khi có sự kiện (ví dụ: git push).
2+
apiVersion: triggers.tekton.dev/v1beta1
3+
kind: TriggerTemplate
4+
metadata:
5+
# Operator sẽ điền tên
6+
name: <triggertemplate-name>
7+
spec:
8+
params:
9+
- name: git-repo-url
10+
description: The git repository url
11+
- name: git-revision
12+
description: The git revision
13+
- name: image-repo
14+
description: The image repository
15+
resourcetemplates:
16+
- apiVersion: tekton.dev/v1beta1
17+
kind: PipelineRun
18+
metadata:
19+
generateName: $(tt.params.image-repo)-run-
20+
spec:
21+
pipelineRef:
22+
# Tên của Pipeline đã tạo ở trên
23+
name: <pipeline-name>
24+
params:
25+
- name: git-repo
26+
value: $(tt.params.git-repo-url)
27+
- name: git-revision
28+
value: $(tt.params.git-revision)
29+
- name: image-repo
30+
value: $(tt.params.image-repo)
31+
workspaces:
32+
- name: source-code
33+
volumeClaimTemplate:
34+
spec:
35+
accessModes:
36+
- ReadWriteOnce
37+
resources:
38+
requests:
39+
storage: 1Gi
40+
41+
---
42+
# EventListener lắng nghe các sự kiện từ bên ngoài (ví dụ: webhook từ GitHub)
43+
apiVersion: triggers.tekton.dev/v1beta1
44+
kind: EventListener
45+
metadata:
46+
# Operator sẽ điền tên
47+
name: <eventlistener-name>
48+
spec:
49+
serviceAccountName: tekton-triggers-sa # Cần tạo service account này với quyền hạn phù hợp
50+
triggers:
51+
- name: git-push-trigger
52+
interceptors:
53+
- ref:
54+
name: "github"
55+
params:
56+
- name: "secretRef"
57+
value:
58+
secretName: github-webhook-secret
59+
secretKey: secretToken
60+
- name: "eventTypes"
61+
value: ["push"]
62+
bindings:
63+
# Binding này trích xuất thông tin từ payload của webhook
64+
- ref: "github-push"
65+
template:
66+
# Tên của TriggerTemplate đã tạo ở trên
67+
ref: <triggertemplate-name>

0 commit comments

Comments
 (0)