Integrate GitLab CI with CTO.ai
The CTO.ai GitLab CI/CD Template lets you integrate your existing development workflows from GitLab CI with the CTO.ai platform. However, you choose to make GitLab CI part of your development process, our template gives you granular control of the events collected by the CTO.ai platform and allows you to send Insights Events at the points in your existing workflows where it makes the most sense.
Before you begin, you may want to review our documentation on Insights Events to better understand the concepts implemented by our platform.
GitLab CI/CD Template
This integration is available as a public repository on GitHub.
To make use of this template, you can drop the following job snippet into the definition of your GitLab CI/CD Pipeline, after the job in the pipeline responsible for the deployment of your application:
include:
- remote: 'https://raw.githubusercontent.com/cto-ai/events-gitlab-template/v0.0.2/cto.gitlab-ci.yml'
deploy_success:
stage: cto_event
variables:
TEAM_ID: "${CTOAI_TEAM_ID}"
TOKEN: "${CTOAI_EVENTS_API_TOKEN}"
EVENT_NAME: "deployment"
EVENT_ACTION: "success"
when: on_success # Trigger if all jobs in pipeline were successful
environment:
name: production
extends:
- .cto-notify-event
deploy_failure:
stage: cto_event
variables:
TEAM_ID: "${CTOAI_TEAM_ID}"
TOKEN: "${CTOAI_EVENTS_API_TOKEN}"
EVENT_NAME: "deployment"
EVENT_ACTION: "failure"
# Trigger if any of jobs in pipeline failed.
# Jobs with `allow_failure` will not be considered failed regardless.
when: on_failure
environment:
name: production
extends:
- .cto-notify-event
stages:
# Previous Jobs
- cto_event
You can use rules to control when your success and failure events are triggered.
We recommend adding the environment value for each job to help differentiate multiple deployment events from the same repository to different environments or workflows. This value will be reflected on your Insights Dashboard.
The above snippet expects that you have set environment variables for CTOAI_EVENTS_API_TOKEN and CTOAI_TEAM_ID in your GitLab CI/CD settings. Be sure to mask and protect the variables you set.
Instructions for finding these values can be found on our Collect Events via API page.
Deployment Events
For more information on the expected fields for Deployment Events, please see the Event Schema Reference page.
$TOKEN- Your API token for the CTO.ai platform.
$TEAM_IDThe ID of the Team on the CTO.ai platform to associate with this event. This environment variable corresponds to the
team_idfield in thedeploymentevent schema.$EVENT_NAMEFor deployment events, this value must always be
deployment. This environment variable corresponds to theevent_namefield in thedeploymentevent schema.$EVENT_ACTIONThis should be one of the Deployment Lifecycle States defined on the Event Types page. This environment variable corresponds to the
event_actionfield in thedeploymentevent schema.$BRANCHThe name of the branch that triggered the deployment. This environment variable corresponds to the
branchfield in thedeploymentevent schema.This environment variable is optional and defaults to the value of
$CI_COMMIT_BRANCHif not specified.$COMMITThe SHA hash of the commit that triggered the deployment. This environment variable corresponds to the
commitfield in thedeploymentevent schema.This environment variable is optional and defaults to the value of
$CI_COMMIT_SHAif not specified.$REPOThe name of the repository that triggered the deployment. This environment variable corresponds to the
repofield in thedeploymentevent schema.This environment variable is optional and defaults to the value of
$CI_PROJECT_PATHif not specified.$ENVIRONMENTA string that uniquely represents the environment that the deployment is targeting. This environment variable corresponds to the
environmentfield in thedeploymentevent schema.This environment variable is optional and defaults to the value of
$CI_ENVIRONMENT_NAMEif not specified.$IMAGEOptionally provide an identifier for the container image that is associated with the current deployment. This environment variable corresponds to the
imagefield in thedeploymentevent schema.