TriggerBindings 概述
TriggerBinding的功能主要用于将Event中特定属性的值传递给TriggerTemplate上的参数从而完成其resourcetemplates中模板资源的实例化。
注意:Trigger使用参数名称将 TriggerBinding 参数与 TriggerTemplate 参数匹配。为了传递信息,绑定中使用的参数名称必须与模板中使用的参数名称匹配。
TriggerBindings 资源规范
spec字段中,主要定义params,每个Parameters主要是name和value两个字段组成
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
name: pipeline-binding
spec:
params:
- name: gitrevision # 为同一Trigger当中引用的TriggerTemplate上声明的某个参数的名称
value: $(body.head_commit.id) # 通常要引用Event中的特定属性,例如$(body.head_commit.id)
TriggerBindings 声明方式
内联绑定
声明绑定的最简单方法是在Trigger本身内
apiVersion: triggers.tekton.dev/v1beta1
kind: Trigger
metadata:
name: push-trigger
spec:
bindings:
- name: gitrevision
value: $(body.head_commit.id)
- name: gitrepositoryurl
value: $(body.repository.url)
template:
ref: git-clone-template
TriggerBindings
定义 TriggerBindings
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
name: pipeline-binding
spec:
params:
- name: gitrevision
value: $(body.head_commit.id)
- name: gitrepositoryurl
value: $(body.repository.url)
- name: contenttype
value: $(header.Content-Type)
引用 TriggerBindings
apiVersion: triggers.tekton.dev/v1beta1
kind: Trigger
metadata:
name: push-trigger
spec:
bindings:
- ref: pipeline-binding
template:
ref: git-clone-template
ClusterTriggerBindings
ClusterTriggerBinding 是集群范围内的 TriggerBinding,您可以在整个集群中重复使用它。可以在任何命名空间的任何Trigger中引用 ClusterTriggerBinding。可以在Trigger中指定多个 ClusterTriggerBinding,也可以在多个Trigger中指定相同的 ClusterTriggerBinding
定义 ClusterTriggerBindings
apiVersion: triggers.tekton.dev/v1beta1
kind: ClusterTriggerBinding
metadata:
name: pipeline-clusterbinding
spec:
params:
- name: gitrevision
value: $(body.head_commit.id)
- name: gitrepositoryurl
value: $(body.repository.url)
- name: contenttype
value: $(header.Content-Type)
引用 ClusterTriggerBindings
引用 ClusterTriggerBinding 时,必须在绑定字段中指定种类值。默认值是 TriggerBinding,它表示命名空间的 TriggerBinding。
---
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
name: listener-clustertriggerbinding
spec:
serviceAccountName: tekton-triggers-example-sa
triggers:
- name: foo-trig
bindings:
- ref: pipeline-clusterbinding
kind: ClusterTriggerBinding
- ref: message-clusterbinding
kind: ClusterTriggerBinding
template:
ref: pipeline-template
访问 HTTP JSON 格式数据
方法一
$(body.key1)
$(.body.key)
方法二
如果 $() 包装器嵌入到另一个 $() 包装器中,Tekton 会将最内层包装器的内容解析为 JSONPath 表达式
$($(body.b)) # Parsed as $(body.b)
$($($(body.b))) # Parsed as $(body.b)
Interceptors 访问overlays数据
interceptor可以添加可供 TriggerBinding 使用的其他有用数据。interceptor添加的数据可以在顶级extensions字段下访问,例如$(extensions.field-name).在以下示例中,CEL 拦截器添加一个字段,然后由trigger binding访问该字段
apiVersion: triggers.tekton.dev/v1beta1
kind: Trigger
metadata:
name: push-trigger
spec:
interceptors:
- name: add-truncated-sha
ref:
name: "cel"
params:
- name: "overlays"
value:
- key: truncated_sha
expression: "body.pull_request.head.sha.truncate(7)"
bindings:
- name: truncated_sha
value: $(extensions.truncated_sha)
template:
ref: git-clone-template
访问 EventListener Event Context
EventListener 有一组内部数据点,这些数据点是为单个事件的完整处理而维护的。这些值可用于TriggerBinding对象。可以通过context参数访问此数据。
$(context.eventID) # access the internal eventID of the request
访问带有特殊字符的 json
要访问包含句点 (.) 的 JSON 键,必须使用反斜杠 (\.) 转义句点。
# Body contains a `tekton.dev` field: {"body": {"dev.tekton.dev/foo": "triggers"}}
$(body.dev\.tekton\.dev\/foo) -> "triggers"
TriggerBindings 示例
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
name: event-binding
spec:
params:
- name: gitrevision
value: $(body.head_commit.id)
- name: gitrepositoryurl
value: $(body.repository.url)
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
name: prod-env
spec:
params:
- name: environment
value: prod
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
name: staging-env
spec:
params:
- name: environment
value: staging
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
metadata:
name: listener
spec:
triggers:
- name: prod-trigger
bindings:
- ref: event-binding
- ref: prod-env
template:
ref: pipeline-template
- name: staging-trigger
bindings:
- ref: event-binding
- ref: staging-env
template:
ref: pipeline-template
参考文档
https://tekton.dev/docs/triggers/triggerbindings/
标签:body,name,triggers,dev,Tekton,Trigger,value,TriggerBindings,tekton From: https://www.cnblogs.com/wangguishe/p/17876606.html