Skip to content

Latest commit

 

History

History

gitlab-source

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

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.
  2. Ensure Knative Serving is configured with a domain name.
  3. Ensure that your Knative cluster has a stable, externally reachable address that allows GitLab to call into the cluster. Refer to your provider's documentation.
  4. Install Knative Eventing.

Install GitLab Event Source

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

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

Wait for the controller and webhook to be ready:

kubectl -n knative-sources rollout status deployment/gitlab-controller-manager
kubectl -n knative-sources rollout status deployment/gitlab-webhook

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

Clone this repository and run the commands below from code-samples/eventing/gitlab-source.

Create a Knative Service which receives events from the GitLab source and prints them to stdout:

kubectl -n default apply -f - <<'EOF'
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: gitlab-event-display
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
EOF
kubectl -n default wait --for=condition=Ready kservice/gitlab-event-display --timeout=120s

Create GitLab Tokens

  1. Create a project 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. Replace the placeholder values in the Secret below. Keep real tokens out of version control:

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

    Tip: you can generate a random secretToken by running the command:

    head -c 8 /dev/urandom | base64
  3. Create the secret using kubectl.

    kubectl -n default apply -f - <<'EOF'
    apiVersion: v1
    kind: Secret
    metadata:
      name: gitlabsecret
    type: Opaque
    stringData:
      accessToken: <project_access_token_value>
      secretToken: <random_string>
    EOF

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 following manifest with your GitLab project URL, for example https://gitlab.com/knative-examples/functions.

    The manifest subscribes to push and issue events, reads both tokens from gitlabsecret, and sends CloudEvents to gitlab-event-display.

  2. Apply the edited manifest using kubectl:

    kubectl -n default apply -f - <<'EOF'
    apiVersion: sources.knative.dev/v1alpha1
    kind: GitLabSource
    metadata:
      name: gitlabsource-sample
    spec:
      eventTypes:
        - push_events
        - issues_events
      projectUrl: <project url>
      accessToken:
        secretKeyRef:
          name: gitlabsecret
          key: accessToken
      secretToken:
        secretKeyRef:
          name: gitlabsecret
          key: secretToken
      sink:
        ref:
          apiVersion: serving.knative.dev/v1
          kind: Service
          name: gitlab-event-display
    EOF
    kubectl -n default wait --for=condition=Ready gitlabsource/gitlabsource-sample --timeout=120s

Verify

Verify that GitLab webhook was created by looking at the list of webhooks under Settings >> Webhooks 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:

kubectl -n default logs -l serving.knative.dev/service=gitlab-event-display -c user-container --tail=100

With current GitLab source releases, you will see a CloudEvent similar to the following (the ID, timestamp, source, and payload will vary). Older releases may emit CloudEvents 0.3 with type dev.knative.sources.gitlabsource.Push Hook.

☁️  cloudevents.Event
Context Attributes,
  specversion: 1.0
  type: dev.knative.sources.gitlab.push
  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>
  }

Cleanup

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

kubectl --namespace default delete gitlabsource gitlabsource-sample

Similarly, you can remove the Service and Secret via:

kubectl --namespace default delete kservice gitlab-event-display
kubectl --namespace default delete secret gitlabsecret