diff --git a/docs/eventlisteners.md b/docs/eventlisteners.md index 9e7f5df73..cdfd230f7 100644 --- a/docs/eventlisteners.md +++ b/docs/eventlisteners.md @@ -296,10 +296,7 @@ accept to the `eventTypes` field. The body/header of the incoming request will be preserved in this Interceptor's response. - - -```YAML ---- +```yaml apiVersion: tekton.dev/v1alpha1 kind: EventListener metadata: diff --git a/examples/eventlisteners/gitlab-eventlistener-interceptor.yaml b/examples/eventlisteners/gitlab-eventlistener-interceptor.yaml deleted file mode 100644 index a88b3c60c..000000000 --- a/examples/eventlisteners/gitlab-eventlistener-interceptor.yaml +++ /dev/null @@ -1,20 +0,0 @@ ---- -apiVersion: tekton.dev/v1alpha1 -kind: EventListener -metadata: - name: gitlab-listener-interceptor -spec: - serviceAccountName: tekton-triggers-example-sa - triggers: - - name: foo-trig - interceptors: - - gitlab: - secretRef: - secretName: foo - secretKey: bar - eventTypes: - - Push Hook - bindings: - - name: pipeline-binding - template: - name: pipeline-template diff --git a/examples/gitlab/README.md b/examples/gitlab/README.md new file mode 100644 index 000000000..b24457910 --- /dev/null +++ b/examples/gitlab/README.md @@ -0,0 +1,49 @@ +## GitLab Push EventListener + +Creates an EventListener that listens for Gitlab webhook events. + +### Try it out locally: + +1. Create the service account: + + ```shell script + kubectl apply -f examples/role-resources/triggerbinding-roles + kubectl apply -f examples/role-resources/ + ``` + +1. Create the Gitlab EventListener: + + ```shell script + kubectl apply -f examples/gitlab/gitlab-push-listener.yaml + ``` + +1. Port forward: + + ```shell script + kubectl port-forward \ + "$(kubectl get pod --selector=eventlistener=gitlab-listener -oname)" \ + 8080 + ``` + + **Note**: Instead of port forwarding, you can set the + [`serviceType`](https://github.com/tektoncd/triggers/blob/master/docs/eventlisteners.md#serviceType) + to `LoadBalancer` to expose the EventListener with a public IP. + +1. Test by sending the sample payload. + + ```shell script + curl -v \ + -H 'X-GitLab-Token: abcde' \ + -H 'X-Gitlab-Event: Push Hook' \ + -H 'Content-Type: application/json' \ + --data-binary "@examples/gitlab/gitlab-push-event.json" \ + http://localhost:8080 + ``` + + The response status code should be `201 Created` + +1. You should see a new TaskRun that got created: + + ```shell script + kubectl get taskruns | grep gitlab-run- + ``` diff --git a/examples/gitlab/gitlab-push-event.json b/examples/gitlab/gitlab-push-event.json new file mode 100644 index 000000000..ee54d078a --- /dev/null +++ b/examples/gitlab/gitlab-push-event.json @@ -0,0 +1,59 @@ +{ + "object_kind": "push", + "event_name": "push", + "before": "1a1736ec3d7b03349b31218a2f2c572c7c7206d6", + "after": "1a1736ec3d7b03349b31218a2f2c572c7c7206d6", + "ref": "refs/heads/master", + "checkout_sha": "1a1736ec3d7b03349b31218a2f2c572c7c7206d6", + "message": null, + "user_id": 111448, + "user_name": "Dibyo Mukherjee", + "user_username": "dibyom", + "user_email": "", + "user_avatar": "https://secure.gravatar.com/avatar/1d56773f447d86b8ffa33efb7a5d0cb5?s=80&d=identicon", + "project_id": 16507326, + "project": { + "id": 16507326, + "name": "triggers", + "description": "", + "web_url": "https://gitlab.com/dibyom/triggers", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:dibyom/triggers.git", + "git_http_url": "https://gitlab.com/dibyom/triggers.git", + "namespace": "Dibyo Mukherjee", + "visibility_level": 20, + "path_with_namespace": "dibyom/triggers", + "default_branch": "master", + "ci_config_path": null, + "homepage": "https://gitlab.com/dibyom/triggers", + "url": "git@gitlab.com:dibyom/triggers.git", + "ssh_url": "git@gitlab.com:dibyom/triggers.git", + "http_url": "https://gitlab.com/dibyom/triggers.git" + }, + "commits": [ + { + "id": "1a1736ec3d7b03349b31218a2f2c572c7c7206d6", + "message": "Add new file", + "timestamp": "2020-01-24T17:05:48+00:00", + "url": "https://gitlab.com/dibyom/triggers/-/commit/1a1736ec3d7b03349b31218a2f2c572c7c7206d6", + "author": { + "name": "Dibyo Mukherjee", + "email": "foo@bar.com" + }, + "added": ["Readme.md"], + "modified": [], + "removed": [] + } + ], + "total_commits_count": 1, + "push_options": {}, + "repository": { + "name": "triggers", + "url": "git@gitlab.com:dibyom/triggers.git", + "description": "", + "homepage": "https://gitlab.com/dibyom/triggers", + "git_http_url": "https://gitlab.com/dibyom/triggers.git", + "git_ssh_url": "git@gitlab.com:dibyom/triggers.git", + "visibility_level": 20 + } +} diff --git a/examples/gitlab/gitlab-push-listener.yaml b/examples/gitlab/gitlab-push-listener.yaml new file mode 100644 index 000000000..89d6545b5 --- /dev/null +++ b/examples/gitlab/gitlab-push-listener.yaml @@ -0,0 +1,74 @@ +apiVersion: tekton.dev/v1alpha1 +kind: TriggerTemplate +metadata: + name: gitlab-echo-template +spec: + params: + - name: gitrevision + - name: gitrepositoryurl + resourcetemplates: + - apiVersion: tekton.dev/v1alpha1 + kind: TaskRun + metadata: + generateName: gitlab-run- + spec: + taskSpec: + inputs: + resources: + - name: source + type: git + steps: + - image: ubuntu + script: | + #! /bin/bash + ls -al $(inputs.resources.source.path) + inputs: + resources: + - name: source + resourceSpec: + type: git + params: + - name: revision + value: $(params.gitrevision) + - name: url + value: $(params.gitrepositoryurl) +--- +apiVersion: tekton.dev/v1alpha1 +kind: TriggerBinding +metadata: + name: gitlab-push-binding +spec: + params: + - name: gitrevision + value: $(body.checkout_sha) + - name: gitrepositoryurl + value: $(body.repository.git_http_url) +--- +apiVersion: v1 +kind: Secret +metadata: + name: gitlab-secret +type: Opaque +stringData: + gitlabToken: abcde +--- +apiVersion: tekton.dev/v1alpha1 +kind: EventListener +metadata: + name: gitlab-listener +spec: + # from examples/role-resources/servicaccount.yaml + serviceAccountName: tekton-triggers-example-sa + triggers: + - name: gitlab-push-events-trigger + interceptors: + - gitlab: + secretRef: + secretName: gitlab-secret + secretKey: gitlabToken + eventTypes: + - Push Hook # Only push events + bindings: + - name: gitlab-push-binding + template: + name: gitlab-echo-template