Chapter 6. Using ClusterTriggerBindings with OpenShift Pipelines
You can trigger pipeline runs automatically in response to Git repository events by using the ClusterTriggerBinding resources that OpenShift Pipelines installs. These cluster-scoped bindings extract parameters from webhook payloads for GitHub, GitLab, and Bitbucket.
6.1. ClusterTriggerBindings overview Copy linkLink copied to clipboard!
You can use ClusterTriggerBinding resources in OpenShift Pipelines to extract fields from webhook event payloads and pass them as parameters to your TriggerTemplate resources.
Unlike a TriggerBinding resource, which is scoped to a single namespace, a ClusterTriggerBinding resource is cluster-scoped and reusable in all namespaces. OpenShift Pipelines installs a set of ClusterTriggerBinding resources for common Git hosting provider events. You can reference these preinstalled bindings in your Trigger or EventListener resources without writing custom parameter extraction logic.
The following example shows the structure of a ClusterTriggerBinding resource that extracts fields from a GitHub push event:
apiVersion: triggers.tekton.dev/v1beta1
kind: ClusterTriggerBinding
metadata:
name: github-push
spec:
params:
- name: git-revision
value: $(body.head_commit.id)
- name: git-repo-url
value: $(body.repository.url)
- name: git-repo-name
value: $(body.repository.name)
To reference a ClusterTriggerBinding resource in a Trigger resource, set the kind field to ClusterTriggerBinding in the bindings list:
apiVersion: triggers.tekton.dev/v1
kind: Trigger
metadata:
name: my-trigger
spec:
bindings:
- ref: github-push
kind: ClusterTriggerBinding
template:
ref: my-trigger-template
Without the kind: ClusterTriggerBinding field, Tekton Triggers looks for a namespace-scoped TriggerBinding resource with that name instead.
You can list the ClusterTriggerBinding resources installed in your cluster by running the following command:
$ oc get clustertriggerbindings
6.2. Triggering a pipeline run from a GitHub push event Copy linkLink copied to clipboard!
Trigger a pipeline run automatically when code is pushed to a GitHub repository by using the preinstalled github-push ClusterTriggerBinding resource in OpenShift Pipelines.
Prerequisites
- You have access to an OpenShift Container Platform cluster with Red Hat OpenShift Pipelines installed.
-
You have installed the OpenShift Pipelines CLI (
tkn). -
You have
cluster-adminor equivalent permissions, or you have permission to createEventListener,TriggerTemplate,Route, andSecretresources in your namespace. - You have a GitHub repository to which you have admin access, so that you can configure webhooks.
-
You have a pipeline that accepts
git-urlandgit-revisionparameters, or you can adapt theTriggerTemplatein this procedure to match your pipeline.
Procedure
Verify that the
github-pushClusterTriggerBindingresource is installed:$ oc get clustertriggerbinding github-push -o yamlThe output shows the binding and its parameter definitions extracted from the GitHub push event payload.
Create a webhook secret to validate incoming GitHub events. Replace
<your_secret_token>with a secure, random string that you will also enter when configuring the GitHub webhook:apiVersion: v1 kind: Secret metadata: name: github-webhook-secret type: Opaque stringData: secretToken: "<your_secret_token>"Apply the manifest by running the following command:
$ oc apply -f github-webhook-secret.yamlCreate a
TriggerTemplateresource that defines how to create aPipelineRunfrom the parameters supplied by thegithub-pushClusterTriggerBinding. Replace<your_pipeline_name>with the name of your pipeline:apiVersion: triggers.tekton.dev/v1 kind: TriggerTemplate metadata: name: github-push-pipeline spec: params: - name: git-revision description: The SHA of the commit that triggered the push - name: git-repo-clone-url description: The HTTPS clone URL of the repository - name: git-repo-name description: The name of the repository resourcetemplates: - apiVersion: tekton.dev/v1 kind: PipelineRun metadata: generateName: github-push-run- spec: serviceAccountName: pipeline pipelineRef: name: <your_pipeline_name> params: - name: git-url value: $(tt.params.git-repo-clone-url) - name: git-revision value: $(tt.params.git-revision) - name: deployment-name value: $(tt.params.git-repo-name) workspaces: - name: shared-workspace volumeClaimTemplate: spec: accessModes: - ReadWriteOnce resources: requests: storage: 500MiImportantUse the
git-repo-clone-urlparameter, notgit-repo-url, when you need the repository URL for cloning. Thegit-repo-urlparameter maps to$(body.repository.url), which is the GitHub REST API URL and cannot be used for cloning. Thegit-repo-clone-urlparameter maps to$(body.repository.html_url), which is the correct HTTPS URL for cloning the repository withgit-cloneor similar tasks.Apply the manifest by running the following command:
$ oc apply -f github-push-pipeline-template.yamlCreate an
EventListenerresource that exposes an HTTP endpoint, references thegithub-pushClusterTriggerBinding, and uses the GitHub interceptor to validate the webhook signature and filter for push events only:apiVersion: triggers.tekton.dev/v1 kind: EventListener metadata: name: github-push-listener spec: serviceAccountName: pipeline triggers: - interceptors: - ref: name: github params: - name: secretRef value: secretName: github-webhook-secret secretKey: secretToken - name: eventTypes value: ["push"] bindings: - ref: github-push kind: ClusterTriggerBinding template: ref: github-push-pipelineThe
kind: ClusterTriggerBindingfield is required to distinguish thegithub-pushbinding from a namespace-scopedTriggerBindingresource with the same name.The
eventTypes: ["push"]interceptor parameter ensures the event listener processes only push events and ignores all other event types, such as pull requests or releases.Apply the manifest by running the following command:
$ oc apply -f github-push-listener.yamlExpose the
EventListenerservice as a publicly accessible route:$ oc expose svc el-github-push-listenerGet the route URL by running the following command:
$ oc get route el-github-push-listener --template='http://{{.spec.host}}'Save the URL displayed in the output. You provide this URL to GitHub when configuring the webhook.
NoteFor a production environment, create a secure HTTPS route instead. See "Securing webhooks with event listeners" in the Additional resources section.
Configure a webhook on your GitHub repository to send push events to the
EventListenerroute:- Open your GitHub repository in a browser.
-
Click Settings
Webhooks Add webhook. On the Add webhook page:
-
Enter the
EventListenerroute URL from the earlier step in the Payload URL field. - Select application/json for the Content type.
-
Enter the secret token you set in the
github-webhook-secretSecretresource in the Secret field. - Select Just the push event.
- Select Active.
- Click Add webhook.
-
Enter the
Verification
Push a commit to your GitHub repository by running the following commands:
$ git commit --allow-empty -m "Test pipeline trigger"$ git pushList the pipeline runs to confirm that a new run was created:
$ tkn pipelinerun listA new
PipelineRunwith a name prefixed bygithub-push-run-is displayed in the output within a few seconds of the push event.
6.3. ClusterTriggerBinding reference Copy linkLink copied to clipboard!
Red Hat OpenShift Pipelines installs the following ClusterTriggerBinding resources. Each binding extracts parameters from the webhook event payload of the corresponding Git hosting provider. You can reference these bindings in a Trigger or EventListener resource by setting kind: ClusterTriggerBinding.
6.3.1. GitHub ClusterTriggerBindings Copy linkLink copied to clipboard!
github-push
Extracts parameters from a GitHub push event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| SHA of the commit that triggered the push |
|
|
| Commit message of the pushed commit |
|
|
|
GitHub REST API URL of the repository. This is not a clone URL and cannot be used directly with |
|
|
|
HTTPS URL of the repository, suitable for cloning. Use this parameter with |
|
|
| Name of the repository |
|
|
| Content type of the HTTP request |
|
|
| GitHub username of the pusher |
|
github-pullreq
Extracts parameters from a GitHub pull request event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| HTML URL of the repository |
|
|
| SHA of the head commit of the pull request |
|
|
|
Action that triggered the event, for example |
|
|
| Pull request number |
|
|
|
Full name of the repository, in |
|
|
| HTML URL of the pull request |
|
|
| Title of the pull request |
|
|
| Issue API URL associated with the pull request |
|
|
| URL of the pull request author’s organizations |
|
|
|
Type of the pull request author, for example |
|
github-pullreq-review-comment
Extracts parameters from a GitHub pull request review comment event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Body of the review comment |
|
|
| GitHub username of the comment author |
|
|
| SHA of the merge commit for the pull request |
|
6.3.2. GitLab ClusterTriggerBindings Copy linkLink copied to clipboard!
gitlab-push
Extracts parameters from a GitLab push event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| SHA of the latest commit in the push |
|
|
| Commit message of the first commit in the push |
|
|
| HTTP clone URL of the repository |
|
|
| SSH clone URL of the repository |
|
|
| Name of the repository |
|
|
| GitLab username of the pusher |
|
gitlab-mergereq
Extracts parameters from a GitLab merge request event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| HTTP clone URL of the project |
|
|
| SSH clone URL of the repository |
|
|
| SHA of the last commit in the merge request |
|
|
|
Action that triggered the event, for example |
|
|
| Internal ID of the merge request |
|
|
| Name of the repository |
|
|
| URL of the merge request |
|
|
| Title of the merge request |
|
gitlab-review-comment-on-issues
Extracts parameters from a GitLab comment event on an issue.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| URL of the issue |
|
|
| Title of the issue |
|
|
| URL of the comment |
|
|
| Name of the comment author |
|
gitlab-review-comment-on-mergerequest
Extracts parameters from a GitLab comment event on a merge request.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| URL of the merge request |
|
|
| Body of the comment |
|
|
| URL of the comment |
|
|
| Name of the comment author |
|
gitlab-review-comment-on-commit
Extracts parameters from a GitLab comment event on a commit.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| URL of the commit |
|
|
| Body of the comment |
|
|
| URL of the comment |
|
|
| Name of the comment author |
|
gitlab-review-comment-on-snippet
Extracts parameters from a GitLab comment event on a snippet.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Body of the comment |
|
|
| URL of the comment |
|
|
| Title of the snippet |
|
|
| Type of the snippet |
|
|
| Name of the comment author |
|
6.3.3. Bitbucket Server ClusterTriggerBindings Copy linkLink copied to clipboard!
bitbucket-push
Extracts parameters from a Bitbucket Server push event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Display ID of the reference that was pushed, for example a branch name |
|
|
| Clone URL of the repository |
|
|
| Name of the repository |
|
|
| Username of the pusher |
|
bitbucket-pullreq
Extracts parameters from a Bitbucket Server pull request event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| SSH clone URL of the source repository |
|
|
| Latest commit SHA of the source branch |
|
|
|
State of the pull request, for example |
|
|
| ID of the pull request |
|
|
| Name of the target repository |
|
|
| Web URL of the pull request |
|
|
| Title of the pull request |
|
|
| Type of the pull request author |
|
bitbucket-pullreq-add-comment
Extracts parameters from a Bitbucket Server pull request comment event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Text of the comment |
|
|
| Username of the comment author |
|
6.3.4. Bitbucket Cloud ClusterTriggerBindings Copy linkLink copied to clipboard!
bitbucket-cloud-push
Extracts parameters from a Bitbucket Cloud push event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Name of the branch or tag that was pushed |
|
|
| HTML URL of the repository |
|
|
| Name of the repository |
|
|
| Display name of the pusher |
|
bitbucket-cloud-pullreq
Extracts parameters from a Bitbucket Cloud pull request event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| HTML URL of the source repository |
|
|
| Commit hash of the source branch head |
|
|
|
State of the pull request, for example |
|
|
| ID of the pull request |
|
|
| Name of the destination repository |
|
|
| HTML URL of the pull request |
|
|
| Title of the pull request |
|
|
| Display name of the pull request author |
|
bitbucket-cloud-pullreq-add-comment
Extracts parameters from a Bitbucket Cloud pull request comment event payload.
| Parameter name | Description | Source (JSONPath) |
|---|---|---|
|
| Raw text of the comment |
|
|
| Display name of the comment author |
|
|
| ID of the pull request the comment belongs to |
|