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 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.
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.
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
-
On the Cloud Automation bridge, select your Cloud Automation project.
-
Select Settings, and then select Integrations.
-
Select webhook-service from the list, and then select Add subscription.
-
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
, wheregitlab.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
-
-
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).
- Name: Enter
-
Select Add custom header again and enter the following values:
- Name: Enter
Content-Type
. - Value: Enter
application/json
- Name: Enter
-
Enter a Custom payload.
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:
{ "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.
-
Under Send started event, select
automatically
. -
Under Send finished event, select
by webhook receiver
. -
Select Create subscription.
In GitLab
In your .gitlab-ci.yml
file, add a job entry that will
- Receive the Cloud Automation subscription event
- Perform a task (such as
deployment
) - Call the Cloud Automation API to indicate that the task is finished
The example below defines
- A
notify
job that only runs if thedeployment
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.
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.
-
Create a file called
triggered-event.json
.Example:
{ "type": "sh.keptn.event.mystage.mysequence.triggered", "specversion":"1.0", "source":"manual-trigger", "data":{ "project":"myproject", "stage":"mystage", "service":"myservice" } }
-
Send the event.
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