GitLab source
You will need:
- An internet-accessible Kubernetes cluster with Knative Serving installed. Follow the installation instructions if you need to create one.
- Ensure Knative Serving is that allows GitLab to call into the cluster.
- If you’re using GKE, you’ll also want to assign a static IP address.
- Install .
Install GitLab Event Source
GitLab Event source lives in the . Head to the releases page, find the latest release with artifact and replace the <RELEASE>
with version tag:
Check that the manager is running:
kubectl -n knative-sources get pods --selector control-plane=gitlab-controller-manager
With the controller running you can now move on to a user persona and setup a GitLab webhook as well as a function that will consume GitLab events.
Using the GitLab Event Source
You are now ready to use the Event Source and trigger functions based on GitLab projects events.
We will:
- Create a Knative service which will receive the events. To keep things simple this service will simply dump the events to
stdout
, this is the so-called: event_display - Create a GitLab access token and a random secret token used to secure the webhooks
- Create the event source by posting a GitLab source object manifest to Kubernetes
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: gitlab-event-display
spec:
template:
spec:
containers:
Create the service:
kubectl -n default apply -f event-display.yaml
Create GitLab Tokens
Create a personal access token which the GitLab source will use to register webhooks with the GitLab API. The token must have an “api” access scope in order to create repository webhooks. Also decide on a secret token that your source will use to authenticate the incoming webhooks from GitLab.
Update a secret values in
secret.yaml
defined below:accessToken
is the personal access token created in step 1 andsecretToken
is any token of your choosing.Hint: you can generate a random secretToken with:
secret.yaml
:apiVersion: v1
kind: Secret
metadata:
name: gitlabsecret
type: Opaque
accessToken: <personal_access_token_value>
secretToken: <random_string>
-
kubectl -n default apply -f secret.yaml
In order to receive GitLab events, you have to create a concrete Event Source for a specific namespace. Replace the
projectUrl
value in thegitlabsource.yaml
file with your GitLab project URL, for examplehttps://gitlab.com/knative-examples/functions
.gitlabsource.yaml
:apiVersion: sources.knative.dev/v1alpha1
kind: GitLabSource
metadata:
name: gitlabsource-sample
spec:
eventTypes:
- push_events
- issues_events
projectUrl: <project url>
accessToken:
secretKeyRef:
key: accessToken
secretToken:
secretKeyRef:
key: secretToken
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: gitlab-event-display
Apply the yaml file using
kubectl
:
Verify
Verify that GitLab webhook was created by looking at the list of webhooks under Settings » Integrations in your GitLab project. A hook should be listed that points to your Knative cluster.
Create a push event and check the logs of the Pod backing the gitlab-event-display
knative service. You will see the event:
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 0.3
type: dev.knative.sources.gitlabsource.Push Hook
source: https://gitlab.com/<user>/<project>
id: f83c080f-c2af-48ff-8d8b-fd5b21c5938e
time: 2020-03-12T11:08:41.414572482Z
datacontenttype: application/json
Data,
{
<Event payload>
}
You can remove the GitLab webhook by deleting the GitLab source:
kubectl --namespace default delete --filename gitlabsource.yaml