跳转至

GitLab CI 集成配置

1. 背景

GitLab 是一款 DevOps 平台,通过集成的 CI/CD 功能实现软件开发生命周期的自动化。

本文介绍了如何使用 Dataflux Func 收集 CI Pipeline 执行数据,上报至观测云/TrueWatch 平台。通过观测云 CI 可视化功能分析瓶颈、优化部署流程。

2. 前置条件

  • 注册/登录 观测云/TrueWatch
  • 开通 Func 托管版/自行安装 DataFlux Func GSE 版

3. 配置流程

  1. 官方脚本市场安装 GitLab CI 集成
  2. 创建「同步 API」 (旧版:授权链接)
  3. Gitlab 平台配置 webhook

3.1 安装 GitLab CI 集成

通过官方脚本市场安装 GitLab CI 集成(脚本 ID:guance_gitlab_ci

3.2 创建「同步 API」 (旧版:授权链接)

3.2.1 创建函数

用户可自行创建脚本集、脚本,将下面代码复制进脚本,点击发布。

Python
1
2
3
4
5
6
import guance_gitlab_ci__main as main

@DFF.API('接收 GitLab CI 事件推送', timeout=10)
def runner(**kwargs):
    response = main.run(kwargs)
    return response

3.2.2 指定上报数据源

请提前在您的 Func 中提前创建好与观测云/TrueWatch 工作空间相关联的连接器(如果您开通 Func 托管版,忽略该步骤)

了解 连接器

用户可以指定连接器 ID ,将数据上报到与之关联的 Guance 工作空间,目前支持的连接器包括: Guance、DataKit、DataWay,举例:

Python
1
2
3
4
5
6
7
8
import guance_gitlab_ci__main as main

@DFF.API('接收 GitLab CI 事件推送', timeout=10)
def runner(**kwargs):
    # 将数据上报到 Guance 连接器 ID: xxx 相关联的工作空间
    response = main.run(kwargs, guance_id='xxx')
    # response = main.run(kwargs, datakit_id=None, dataway_id=None, guance_id=None, workspace_token=None)
    return response

注意:连接器只会生效一个,请不要指定多个

如果指定多个,只有一个会生效,优先级由高到低,按照如下顺序:

  1. Guance 连接器,参数:guance_id
  2. DataKit 连接器,参数:datakit_id
  3. DataWay 连接器,参数:dataway_id

连接器参数并不是必须指定的,如果没有指定,程序会自动选择连接器,优先级由高到低,按照如下顺序:

  1. ID 为 guance 的 Guance 连接器
  2. ID 为 datakit 的 DataKit 连接器
  3. ID 为 dataway的 DataWay 连接器

3.2.3 创建「同步 API」

创建「同步 API」 选择执行上一步创建的函数,点击保存。

点击该「同步 API」 示例

复制出GET 简化形式URL 备用

3.3 Gitlab 平台配置 webhook

进入目标代码仓库 Settings > Webhooks 添加新的 webhook:

  • URL: 粘贴上一步所复制的「同步 API」 URL
  • Trigger: 选择 Job events and Pipeline events.

额外配置项

配置自定义标签

注意:如果自定义的标签与 Pipeline、Job 推送的关键字段同名会被忽略掉,关键字段参考下文「数据描述」

通过config.extra_tags配置自定义标签,如下:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import guance_gitlab_ci__main as main

config = {
    'extra_tags': {
        'key1': 'value1',
        'key2': 'value2'
    }
}

@DFF.API('接收 GitLab CI 事件推送', timeout=10)
def runner(**kwargs):
    response = main.run(kwargs, config=config)
    return response

数据描述

GitLab 可视化数据以日志形式存储,可以在观测云/TrueWatch 「CI 可视化」中查看,也可以在日志中指定数据源(source)查看

以下字段不包括用户自定义字段

GitLab Pipeline

数据源: gitlab_pipeline

Name Description Type Unit
author_email Author email string
ci_status CI type string
commit_sha The commit SHA of the most recent commit of the code that triggered the Pipeline string
object_kind Event type, in this case Pipeline string
operation_name Operation name string
pipeline_name Pipeline name string
pipeline_source Sources of Pipeline triggers string
pipeline_url Pipeline URL string
ref Branches involved string
repository_url Repository URL string
resource Project name
commit_message The message attached to the most recent commit of the code that triggered the Pipeline. string
created_at Millisecond timestamp of Pipeline creation int msec
duration Pipeline duration (microseconds) int μs
finished_at Millisecond timestamp of the end of the Pipeline int msec
message The message attached to the most recent commit of the code that triggered the Pipeline. Same as commit_message string
pipeline_id Pipeline id string
gitlab_version Gitlab Version string

GitLab Job

数据源gitlab_job

Name Description Type Unit
build_commit_sha The commit SHA corresponding to build string
build_failure_reason Build failure reason string
build_name Build name string
build_repo_name Repository name corresponding to build string
build_stage Build stage string
build_status Build status string
object_kind Event type, in this case Job string
project_name Project name string
sha The commit SHA corresponding to build string
user_email User email string
build_commit_message The message attached to the most recent commit of the code that triggered the build string -
build_duration Build duration (microseconds) int μs
build_finished_at Millisecond timestamp of the end of build int msec
build_id build id string -
build_started_at Millisecond timestamp of the start of build int msec
message The message attached to the most recent commit of the code that triggered the build. Same as build_commit_message string -
pipeline_id Pipeline id for build string -
project_id Project id for build string -
runner_id Runner id for build string -
gitlab_version Gitlab Version string

问题排查

GitLab CI/CD 执行一段时间后,Guance 工作空间没有相关数据。

排查思路:

  1. 首先确认连接器配置是否正确。
  2. 查看「同步 API」 相关日志是否有报错。 如果没有日志,说明没有成功触发,Gitlab 平台找到目标 webhook 点击 edit,编辑页面查看 Recent Deliveries 是否有记录,如有记录且无异常,请核对 URL 是否与「同步 API」 URL 相匹配。

如果存在日志,根据日志信息排查,必要时请联系脚本负责人。