• Home
  • Platform modules
  • Cloud Automation
  • Partner integrations
  • GitLab

Integrate GitLab with Dynatrace Cloud Automation

Integrating Dynatrace Cloud Automation with GitLab allows you to automatically trigger a GitLab CI/CD pipeline as part of a sequence. For example, the sequence could run a quality check in Dynatrace before triggering the GitLab deployment, and then perform additional load testing after the application is deployed. This saves engineers time and increases confidence in their deployment workflow.

To integrate Dynatrace Cloud Automation with GitLab, follow the instructions below.

Prerequisites

GitLab

Dynatrace

GitLab

  • Create a GitLab account (no license is required).

  • Create a GitLab project configured with a CI/CD pipeline.

  • Create a personal access token or a project access token configured with the API permission.

  • Create two custom CI variables for the Cloud Automation URL and token.

    How to create the custom CI variables

    In your project's settings, go to CI/CD and create the following variables:

    • KEPTN_BASE_URL - The API URL of your Cloud Automation instance (for example, https://XXXXX.cloudautomation.live.dynatrace.com).
    • KEPTN_API_TOKEN - The Cloud Automation API token available from the Cloud Automation UI. Configure it as a masked variable.

Dynatrace

  • Create a Cloud Automation project with a shipyard file that has a custom task (for example, deployment) to trigger the GitLab webhook subscription.

  • Add the GitLab personal/project token obtained above as a Cloud Automation secret.

    How to add the token as a Cloud Automation secret
    1. On the Cloud Automation bridge, select your Cloud Automation project.

    2. Select Settings, and then select Secrets.

    3. Select Add Secret and enter the following values:

      • Name: Enter a name for the secret (for example, gitlab).
      • Scope: Select keptn-webhook-service.
      • Key/Value: Enter your values for the key/value pair (for example, key: access-token, value: <your_personal/project_access_token>).
    4. Select Add secret.

Set up the GitLab webhook subscription

On the Cloud Automation bridge

In GitLab

Use case: The Cloud Automation sequence will wait for GitLab to send back a Cloud Automation sequence task finished event.

On the Cloud Automation bridge

  1. On the Cloud Automation bridge, select your Cloud Automation project.

  2. Select Settings, and then select Integrations.

  3. Select webhook-service from the list, and then select Add subscription.

  4. Enter the following values:

    • Task: Select the task associated with your shipyard file (in this case, deployment).

    • Task suffix: Select triggered.

    • Request method: Select POST.

    • URL: Enter your URL endpoint. Example: https://gitlab.com/api/v4/projects/<project_ID>/pipeline, where

      • gitlab.com can be replaced by your instance URL
      • <project_ID> must be replaced by your project ID. You can find it at the top of the home page of your project
  5. Select Add custom header and enter the following values:

    • Name: Enter Private-Token.
    • Value: Enter {{.secret.<gitlab>.<access-token>}}, making sure to replace <gitlab> with the name of your secret and <access-token> with the key name you entered when creating the Cloud Automation secret (see Prerequisites).
  6. Select Add custom header again and enter the following values:

    • Name: Enter Content-Type.
    • Value: Enter application/json
  7. Enter a Custom payload.

    Note: You must specify at least the ref parameter, which is the branch name that should be used to run the pipeline. To be able to send back an event to Dynatrace from the pipeline, you need to add the required values as CI variables.

    Example:

    json
    { "ref": "main", "variables": [ { "key": "KEPTN_PROJECT", "value": "{{.data.project}}" }, { "key": "KEPTN_STAGE", "value": "{{.data.stage}}" }, { "key": "KEPTN_SERVICE", "value": "{{.data.service}}" }, { "key": "KEPTN_SHKEPTNCONTEXT", "value": "{{.shkeptncontext}}" }, { "key": "KEPTN_ID", "value": "{{.id}}" } ] }

    For a list of available parameters, consult the GitLab documentation.

  8. Under Send started event, select automatically.

  9. Under Send finished event, select by webhook receiver.

  10. Select Create subscription.

In GitLab

In your .gitlab-ci.yml file, add a job entry that will

  1. Receive the Cloud Automation subscription event
  2. Perform a task (such as deployment)
  3. Call the Cloud Automation API to indicate that the task is finished

The example below defines

  • A notify job that only runs if the deployment job succeeds
  • A KEPTN_SHKEPTNCONTEXT variable that is passed to the pipeline

You can customize this example according to your needs. For more information, consult the GitLab documentation.

yaml
stages: - build - test - deploy - notify deploy: stage: deploy script: - echo "Running deployment logic here" notify: stage: notify rules: - if: $KEPTN_SHKEPTNCONTEXT script: - | payload=$(cat <<-END { "data": { "project": "$KEPTN_PROJECT", "stage": "$KEPTN_STAGE", "service": "$KEPTN_SERVICE", "status": "succeeded", "result": "pass" }, "source": "gitlab", "specversion": "1.0", "type": "sh.keptn.event.deployment.finished", "shkeptncontext": "$KEPTN_SHKEPTNCONTEXT", "triggeredid": "$KEPTN_ID" } END ) curl -X POST "$KEPTN_BASE_URL/api/v1/event" -H "Content-Type: application/json" -H "accept: application/json" -H "x-token: $KEPTN_API_TOKEN" -d "$payload"

You can use the same approach to trigger any other API endpoint in GitLab by changing the Request method, URL, and Custom payload accordingly. For a list of available endpoints and required parameters, see API docs.

Trigger a sequence

Cloud Automation triggers GitLab whenever the defined task in the sequence is triggered:

To trigger the sequence, you can send the event command in the Keptn CLI.

  1. Create a file called triggered-event.json.

    Example:

    json
    { "type": "sh.keptn.event.mystage.mysequence.triggered", "specversion":"1.0", "source":"manual-trigger", "data":{ "project":"myproject", "stage":"mystage", "service":"myservice" } }
  2. Send the event.

    bash
    keptn send event --file triggered-event.json

After sending the event, you can

  • Monitor the sequence progress on the project’s Cloud Automation sequence page
  • Monitor the GitLab pipeline execution
Related topics
  • Cloud Automation

    The capabilities of Dynatrace Cloud Automation