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

群组级受保护环境 API

  • Tier: 付费版, 旗舰版
  • Offering: GitLab.com, GitLab 私有化部署, GitLab 专属版

使用此 API 与群组级受保护环境进行交互。

关于受保护环境,请参阅受保护环境 API

有效的访问级别

访问级别在 ProtectedEnvironments::DeployAccessLevel::ALLOWED_ACCESS_LEVELS 方法定义。 目前,系统识别以下级别:

30 => 开发者权限
40 => 维护者权限
60 => 管理员权限

列出群组级受保护环境

获取一个群组中的受保护环境列表。

GET /groups/:id/protected_environments
属性 类型 必需 描述
id integer/string 经身份验证的用户所维护的群组的 ID 或 URL 编码的路径
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/"

示例响应:

[
   {
      "name":"production",
      "deploy_access_levels":[
         {
            "id": 12,
            "access_level": 40,
            "access_level_description": "维护者",
            "user_id": null,
            "group_id": null
         }
      ],
      "required_approval_count": 0
   }
]

获取单个受保护环境

获取单个受保护环境。

GET /groups/:id/protected_environments/:name
属性 类型 必需 描述
id integer/string 经身份验证的用户所维护的群组的 ID 或 URL 编码的路径
name string 受保护环境的部署层级。可选值为 productionstagingtestingdevelopmentother。了解更多关于部署层级的信息。
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/production"

示例响应:

{
   "name":"production",
   "deploy_access_levels":[
      {
         "id": 12,
         "access_level":40,
         "access_level_description":"维护者",
         "user_id":null,
         "group_id":null
      }
   ],
   "required_approval_count": 0
}

保护单个环境

保护单个环境。

POST /groups/:id/protected_environments
属性 类型 必需 描述
id integer/string 经身份验证的用户所维护的群组的 ID 或 URL 编码的路径
name string 受保护环境的部署层级。可选值为 productionstagingtestingdevelopmentother。了解更多关于部署层级的信息。
deploy_access_levels array 允许部署的访问级别数组,每个级别由一个哈希值描述。可以是 user_idgroup_idaccess_level 之一。格式为 {user_id: integer}{group_id: integer}{access_level: integer}
approval_rules array 允许审批的访问级别数组,每个级别由一个哈希值描述。可以是 user_idgroup_idaccess_level 之一。格式为 {user_id: integer}{group_id: integer}{access_level: integer}。您还可以使用 required_approvals 字段指定来自指定实体的所需审批数量。更多信息请参阅多重审批规则

可分配的 user_id 是属于给定群组并拥有维护者角色(或更高级别)的用户。 可分配的 group_id 是给定群组下的子群组。

curl --header 'Content-Type: application/json' --request POST --data '{"name": "production", "deploy_access_levels": [{"group_id": 9899826}]}' --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/22034114/protected_environments"

示例响应:

{
   "name":"production",
   "deploy_access_levels":[
      {
         "id": 12,
         "access_level": 40,
         "access_level_description": "protected-access-group",
         "user_id": null,
         "group_id": 9899826
      }
   ],
   "required_approval_count": 0
}

一个包含多重审批规则的示例:

curl --header 'Content-Type: application/json' --request POST \
     --data '{"name": "production", "deploy_access_levels": [{"group_id": 138}], "approval_rules": [{"group_id": 134}, {"group_id": 135, "required_approvals": 2}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/128/protected_environments"

在此配置下,操作员群组 "group_id": 138 只有在 QA 群组 "group_id": 134 和安全群组 "group_id": 135 都批准了部署后,才能执行到 production 环境的部署作业。

更新受保护环境

更新单个环境。

PUT /groups/:id/protected_environments/:name
属性 类型 必需 描述
id integer/string 经身份验证的用户所维护的群组的 ID 或 URL 编码的路径
name string 受保护环境的部署层级。可选值为 productionstagingtestingdevelopmentother。了解更多关于部署层级的信息。
deploy_access_levels array 允许部署的访问级别数组,每个级别由一个哈希值描述。可以是 user_idgroup_idaccess_level 之一。格式为 {user_id: integer}{group_id: integer}{access_level: integer}
required_approval_count integer 部署到此环境所需的审批数量。
approval_rules array 允许审批的访问级别数组,每个级别由一个哈希值描述。可以是 user_idgroup_idaccess_level 之一。格式为 {user_id: integer}{group_id: integer}{access_level: integer}。您还可以使用 required_approvals 字段指定来自指定实体的所需审批数量。更多信息请参阅多重审批规则

更新时:

  • user_id:确保更新的用户属于给定群组并拥有维护者角色(或更高级别)。您还必须在相应的哈希中传递 deploy_access_levelapproval_ruleid
  • group_id:确保更新的群组是此受保护环境所属群组的子群组。您还必须在相应的哈希中传递 deploy_access_levelapproval_ruleid

删除时:

  • 您必须传递设置为 true_destroy。请参阅以下示例。

示例:创建 deploy_access_level 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"deploy_access_levels": [{"group_id": 9899829, access_level: 40}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"

示例响应:

{
   "name": "production",
   "deploy_access_levels": [
      {
         "id": 12,
         "access_level": 40,
         "access_level_description": "protected-access-group",
         "user_id": null,
         "group_id": 9899829,
         "group_inheritance_type": 1
      }
   ],
   "required_approval_count": 0
}

示例:更新 deploy_access_level 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"deploy_access_levels": [{"id": 12, "group_id": 22034120}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"
{
   "name": "production",
   "deploy_access_levels": [
      {
         "id": 12,
         "access_level": 40,
         "access_level_description": "protected-access-group",
         "user_id": null,
         "group_id": 22034120,
         "group_inheritance_type": 0
      }
   ],
   "required_approval_count": 2
}

示例:删除 deploy_access_level 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"deploy_access_levels": [{"id": 12, "_destroy": true}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"

示例响应:

{
   "name": "production",
   "deploy_access_levels": [],
   "required_approval_count": 0
}

示例:创建 approval_rule 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"approval_rules": [{"group_id": 134, "required_approvals": 1}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"

示例响应:

{
   "name": "production",
   "approval_rules": [
      {
         "id": 38,
         "user_id": null,
         "group_id": 134,
         "access_level": null,
         "access_level_description": "qa-group",
         "required_approvals": 1,
         "group_inheritance_type": 0
      }
   ]
}

示例:更新 approval_rule 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"approval_rules": [{"id": 38, "group_id": 135, "required_approvals": 2}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"
{
   "name": "production",
   "approval_rules": [
      {
         "id": 38,
         "user_id": null,
         "group_id": 135,
         "access_level": null,
         "access_level_description": "security-group",
         "required_approvals": 2,
         "group_inheritance_type": 0
      }
   ]
}

示例:删除 approval_rule 记录

curl --header 'Content-Type: application/json' --request PUT \
     --data '{"approval_rules": [{"id": 38, "_destroy": true}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/22034114/protected_environments/production"

示例响应:

{
   "name": "production",
   "approval_rules": []
}

取消保护单个环境

取消保护指定的受保护环境。

DELETE /groups/:id/protected_environments/:name
属性 类型 必需 描述
id integer/string 经身份验证的用户所维护的群组的 ID 或 URL 编码的路径
name string 受保护环境的部署层级。可选值为 productionstagingtestingdevelopmentother。了解更多关于部署层级的信息。
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/staging"

响应应返回 200 状态码。