Help us learn about your current experience with the documentation. Take the survey.

Pipeline 触发器令牌 API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

使用此 API 来触发管道

列出项目触发器令牌

获取项目的管道触发器令牌列表。

GET /projects/:id/triggers
属性 类型 必需 描述
id integer/string 项目 ID 或URL 编码的项目路径
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/triggers"
[
    {
        "id": 10,
        "description": "my trigger",
        "created_at": "2016-01-07T09:53:58.235Z",
        "last_used": null,
        "token": "6d056f63e50fe6f8c5f8f4aa10edb7",
        "updated_at": "2016-01-07T09:53:58.235Z",
        "owner": null
    }
]

如果触发器令牌是由认证用户创建的,则会完整显示触发器令牌。其他用户创建的触发器令牌会缩短为四个字符。

获取触发器令牌详情

获取项目的管道触发器令牌详情。

GET /projects/:id/triggers/:trigger_id
属性 类型 必需 描述
id integer/string 项目 ID 或URL 编码的项目路径
trigger_id integer 触发器 ID
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/triggers/5"
{
    "id": 10,
    "description": "my trigger",
    "created_at": "2016-01-07T09:53:58.235Z",
    "last_used": null,
    "token": "6d056f63e50fe6f8c5f8f4aa10edb7",
    "updated_at": "2016-01-07T09:53:58.235Z",
    "owner": null
}

创建触发器令牌

为项目创建管道触发器令牌。

POST /projects/:id/triggers
属性 类型 必需 描述
description string 触发器名称
id integer/string 项目 ID 或URL 编码的项目路径
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --form description="my description" \
  --url "https://gitlab.example.com/api/v4/projects/1/triggers"
{
    "id": 10,
    "description": "my trigger",
    "created_at": "2016-01-07T09:53:58.235Z",
    "last_used": null,
    "token": "6d056f63e50fe6f8c5f8f4aa10edb7",
    "updated_at": "2016-01-07T09:53:58.235Z",
    "owner": null
}

更新管道触发器令牌

更新项目的管道触发器令牌。

PUT /projects/:id/triggers/:trigger_id
属性 类型 必需 描述
id integer/string 项目 ID 或URL 编码的项目路径
trigger_id integer 触发器 ID
description string 触发器名称
curl --request PUT \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --form description="my description" \
  --url "https://gitlab.example.com/api/v4/projects/1/triggers/10"
{
    "id": 10,
    "description": "my trigger",
    "created_at": "2016-01-07T09:53:58.235Z",
    "last_used": null,
    "token": "6d056f63e50fe6f8c5f8f4aa10edb7",
    "updated_at": "2016-01-07T09:53:58.235Z",
    "owner": null
}

删除管道触发器令牌

删除项目的管道触发器令牌。

DELETE /projects/:id/triggers/:trigger_id
属性 类型 必需 描述
id integer/string 项目 ID 或URL 编码的项目路径
trigger_id integer 触发器 ID
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/triggers/5"

使用令牌触发管道

使用管道触发器令牌CI/CD 作业令牌进行身份验证来触发管道。

使用 CI/CD 作业令牌时,触发的管道是多项目管道。认证请求的作业会与上游管道关联,这在管道图中可见。

如果在作业中使用触发器令牌,该作业不会与上游管道关联。

POST /projects/:id/trigger/pipeline

支持的属性:

属性 类型 必需 描述
id integer/string 项目 ID 或URL 编码的项目路径
ref string 运行管道的分支或标签。
token string 触发器令牌或 CI/CD 作业令牌。
variables hash 包含管道变量的键值字符串映射。例如:{ VAR1: "value1", VAR2: "value2" }
inputs hash 创建管道时使用的输入,作为键值对映射。

使用变量的示例请求:

curl --request POST \
  --form "variables[VAR1]=value1" \
  --form "variables[VAR2]=value2" \
  --url "https://gitlab.example.com/api/v4/projects/123/trigger/pipeline?token=2cb1840fb9dfc9fb0b7b1609cd29cb&ref=main"

使用输入的示例请求:

curl --request POST \
  --header "Content-Type: application/json" \
  --data '{"inputs": {"environment": "environment", "scan_security": false, "level": 3}}' \
  --url "https://gitlab.example.com/api/v4/projects/123/trigger/pipeline?token=2cb1840fb9dfc9fb0b7b1609cd29cb&ref=main"

示例响应:

{
  "id": 257,
  "iid": 118,
  "project_id": 123,
  "sha": "91e2711a93e5d9e8dddfeb6d003b636b25bf6fc9",
  "ref": "main",
  "status": "created",
  "source": "trigger",
  "created_at": "2022-03-31T01:12:49.068Z",
  "updated_at": "2022-03-31T01:12:49.068Z",
  "web_url": "http://127.0.0.1:3000/test-group/test-project/-/pipelines/257",
  "before_sha": "0000000000000000000000000000000000000000",
  "tag": false,
  "yaml_errors": null,
  "user": {
    "id": 1,
    "username": "root",
    "name": "Administrator",
    "state": "active",
    "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://127.0.0.1:3000/root"
  },
  "started_at": null,
  "finished_at": null,
  "committed_at": null,
  "duration": null,
  "queued_duration": null,
  "coverage": null,
  "detailed_status": {
    "icon": "status_created",
    "text": "created",
    "label": "created",
    "group": "created",
    "tooltip": "created",
    "has_details": true,
    "details_path": "/test-group/test-project/-/pipelines/257",
    "illustration": null,
    "favicon": "/assets/ci_favicons/favicon_status_created-4b975aa976d24e5a3ea7cd9a5713e6ce2cd9afd08b910415e96675de35f64955.png"
  }
}