GitLab source

GitLab Source example shows how to wire GitLab events for consumption by a Knative Service.

Gitlab source deployment

Prerequisites

You will need:

  1. An internet-accessible Kubernetes cluster with Knative Serving installed. Follow the installation instructions if you need to create one.
  2. Ensure Knative Serving is configured with a domain name that allows GitLab to call into the cluster.
  3. If you’re using GKE, you’ll also want to assign a static IP address.
  4. Install Knative Eventing.

Install GitLab Event Source

GitLab Event source lives in the knative-sandbox/eventing-gitlab. Head to the releases page, find the latest release with gitlab.yaml artifact and replace the <RELEASE> with version tag:

  1. kubectl apply -f https://github.com/knative-sandbox/eventing-gitlab/releases/download/<RELEASE>/gitlab.yaml

Check that the manager is running:

  1. 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

Create a Knative Service

The event-display.yaml file shown below defines the basic service which will receive events from the GitLab source.

  1. apiVersion: serving.knative.dev/v1
  2. kind: Service
  3. metadata:
  4. name: gitlab-event-display
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display

Create the service:

  1. kubectl -n default apply -f event-display.yaml

Create GitLab Tokens

  1. 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.

  2. Update a secret values in secret.yaml defined below:

    accessToken is the personal access token created in step 1 and secretToken is any token of your choosing.

    Hint: you can generate a random secretToken with:

    1. head -c 8 /dev/urandom | base64

    secret.yaml:

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: gitlabsecret
    5. type: Opaque
    6. stringData:
    7. accessToken: <personal_access_token_value>
    8. secretToken: <random_string>
  3. Create the secret using kubectl.

    1. kubectl -n default apply -f secret.yaml

Create Event Source for GitLab Events

  1. In order to receive GitLab events, you have to create a concrete Event Source for a specific namespace. Replace the projectUrl value in the gitlabsource.yaml file with your GitLab project URL, for example https://gitlab.com/knative-examples/functions.

    gitlabsource.yaml:

    1. apiVersion: sources.knative.dev/v1alpha1
    2. kind: GitLabSource
    3. metadata:
    4. name: gitlabsource-sample
    5. spec:
    6. eventTypes:
    7. - push_events
    8. - issues_events
    9. projectUrl: <project url>
    10. accessToken:
    11. secretKeyRef:
    12. name: gitlabsecret
    13. key: accessToken
    14. secretToken:
    15. secretKeyRef:
    16. name: gitlabsecret
    17. key: secretToken
    18. sink:
    19. ref:
    20. apiVersion: serving.knative.dev/v1
    21. kind: Service
    22. name: gitlab-event-display
  2. Apply the yaml file using kubectl:

    1. kubectl -n default apply -f gitlabsource.yaml

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:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 0.3
  5. type: dev.knative.sources.gitlabsource.Push Hook
  6. source: https://gitlab.com/<user>/<project>
  7. id: f83c080f-c2af-48ff-8d8b-fd5b21c5938e
  8. time: 2020-03-12T11:08:41.414572482Z
  9. datacontenttype: application/json
  10. Data,
  11. {
  12. <Event payload>
  13. }

Cleanup

You can remove the GitLab webhook by deleting the GitLab source:

  1. kubectl --namespace default delete --filename gitlabsource.yaml

Similarly, you can remove the Service and Secret via:

  1. kubectl --namespace default delete --filename event-display.yaml
  2. kubectl --namespace default delete --filename secret.yaml