Skip to content

Commit

Permalink
Replace gitlab-eventlistener with a working example
Browse files Browse the repository at this point in the history
I moved this to a separate folder so that it was easy to add the sample
payload as well as instructions for trying it out locally.

Signed-off-by: Dibyo Mukherjee <dibyo@google.com>
  • Loading branch information
dibyom committed Feb 20, 2020
1 parent fe097d1 commit b0ba712
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 24 deletions.
5 changes: 1 addition & 4 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ accept to the `eventTypes` field.
The body/header of the incoming request will be preserved in this Interceptor's
response.

<!-- FILE: examples/eventlisteners/gitlab-eventlistener-interceptor.yaml -->

```YAML
---
```yaml
apiVersion: tekton.dev/v1alpha1
kind: EventListener
metadata:
Expand Down
20 changes: 0 additions & 20 deletions examples/eventlisteners/gitlab-eventlistener-interceptor.yaml

This file was deleted.

49 changes: 49 additions & 0 deletions examples/gitlab/README.md
Original file line number Diff line number Diff line change
@@ -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-
```
59 changes: 59 additions & 0 deletions examples/gitlab/gitlab-push-event.json
Original file line number Diff line number Diff line change
@@ -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
}
}
74 changes: 74 additions & 0 deletions examples/gitlab/gitlab-push-listener.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b0ba712

Please sign in to comment.