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 <[email protected]>
  • Loading branch information
dibyom committed Feb 14, 2020
1 parent 7b1d5e1 commit 72033fd
Show file tree
Hide file tree
Showing 5 changed files with 190 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 @@ -212,10 +212,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.

46 changes: 46 additions & 0 deletions examples/gitlab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## GitLab Push EventListener

Creates an EventListener that listens for Gitlab webhook events. To expose the
EventListener, add a `serviceType: LoadBalancer`.

### 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
```

2. 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 "@gitlab-push-event.json" \
http://localhost:8080
```

The response status code should be `201 Created`

3. You should see a new TaskRun that got created:

```shell script
kubectl get taskruns |grep gitlab-run-
```
68 changes: 68 additions & 0 deletions examples/gitlab/gitlab-push-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master",
"checkout_sha": "1a1736ec3d7b03349b31218a2f2c572c7c7206d6",
"user_id": 4,
"user_name": "John Smith",
"user_username": "jsmith",
"user_email": "[email protected]",
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
"project_id": 15,
"project":{
"id": 15,
"name":"Diaspora",
"description":"",
"web_url":"http://example.com/mike/diaspora",
"avatar_url":null,
"git_ssh_url":"[email protected]:mike/diaspora.git",
"git_http_url":"http://example.com/mike/diaspora.git",
"namespace":"Mike",
"visibility_level":0,
"path_with_namespace":"mike/diaspora",
"default_branch":"master",
"homepage":"http://example.com/mike/diaspora",
"url":"[email protected]:mike/diaspora.git",
"ssh_url":"[email protected]:mike/diaspora.git",
"http_url":"http://example.com/mike/diaspora.git"
},
"repository":{
"name": "Diaspora",
"url": "[email protected]:mike/diaspora.git",
"description": "",
"homepage": "http://example.com/mike/diaspora",
"git_http_url":"https://gitlab.com/dibyom/triggers.git",
"git_ssh_url":"[email protected]:mike/diaspora.git",
"visibility_level":0
},
"commits": [
{
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31+02:00",
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": {
"name": "Jordi Mallach",
"email": "[email protected]"
},
"added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"],
"removed": []
},
{
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
},
"added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"],
"removed": []
}
],
"total_commits_count": 4
}
75 changes: 75 additions & 0 deletions examples/gitlab/gitlab-push-listener.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 72033fd

Please sign in to comment.