群组级别的受保护环境 API
- Tier: 专业版, 旗舰版
- Offering: JihuLab.com, 私有化部署
History
- 在极狐GitLab 14.0 中引入。使用名为 group_level_protected_environments 的功能标志,默认禁用。
- 在极狐GitLab 14.3 中移除 group_level_protected_environments 功能标志。
- 在极狐GitLab 14.3 中 GA。
阅读更多关于群组级别的受保护环境信息。
有效访问级别
访问级别在 ProtectedEnvironments::DeployAccessLevel::ALLOWED_ACCESS_LEVELS 方法中定义。目前,这些级别被识别:
plaintext30 => Developer access 40 => Maintainer access 60 => Admin access
列出群组级别的受保护环境
从一个群组获取受保护环境的列表。
plaintextGET /groups/:id/protected_environments
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer/string | yes | 由认证用户维护的群组的 ID 或 URL 编码路径。 |
shellcurl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/"
示例响应:
json1[ 2 { 3 "name":"production", 4 "deploy_access_levels":[ 5 { 6 "id": 12, 7 "access_level": 40, 8 "access_level_description": "Maintainers", 9 "user_id": null, 10 "group_id": null 11 } 12 ], 13 "required_approval_count": 0 14 } 15]
获取单个受保护环境
获取单个受保护环境。
plaintextGET /groups/:id/protected_environments/:name
shellcurl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/production"
示例响应:
json1{ 2 "name":"production", 3 "deploy_access_levels":[ 4 { 5 "id": 12, 6 "access_level":40, 7 "access_level_description":"Maintainers", 8 "user_id":null, 9 "group_id":null 10 } 11 ], 12 "required_approval_count": 0 13}
保护单个环境
保护单个环境。
plaintextPOST /groups/:id/protected_environments
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer/string | yes | 由认证用户维护的群组的 ID 或 URL 编码路径。 |
| name | string | yes | 受保护环境的部署等级。可以是 production、staging、testing、development 或 other。阅读更多关于部署等级的信息。 |
| deploy_access_levels | array | yes | 允许部署的访问级别数组,每个由一个哈希描述。可以是 user_id、group_id 或 access_level。它们的形式分别为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。 |
| approval_rules | array | no | 允许批准的访问级别数组,每个由一个哈希描述。可以是 user_id、group_id 或 access_level。它们的形式分别为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。您还可以通过 required_approvals 字段指定来自指定实体所需的批准数量。查看更多多重批准规则的信息。 |
可分配的 user_id 是属于给定群组且具有 Maintainer 角色(或以上)的用户。可分配的 group_id 是给定群组下的子群组。
shellcurl --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"
示例响应:
json1{ 2 "name":"production", 3 "deploy_access_levels":[ 4 { 5 "id": 12, 6 "access_level": 40, 7 "access_level_description": "protected-access-group", 8 "user_id": null, 9 "group_id": 9899826 10 } 11 ], 12 "required_approval_count": 0 13}
一个具有多个批准规则的示例:
shellcurl --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 的部署任务。
更新受保护环境
History
- 引入于极狐GitLab 15.4。
更新单个环境。
plaintextPUT /groups/:id/protected_environments/:name
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer/string | yes | 由认证用户维护的群组的 ID 或 URL 编码路径。 |
| name | string | yes | 受保护环境的部署等级。可以是 production、staging、testing、development 或 other。阅读更多关于部署等级的信息。 |
| deploy_access_levels | array | no | 允许部署的访问级别数组,每个由一个哈希描述。可以是 user_id、group_id 或 access_level。它们的形式分别为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。 |
| required_approval_count | integer | no | 部署到此环境所需的批准数量。 |
| approval_rules | array | no | 允许批准的访问级别数组,每个由一个哈希描述。可以是 user_id、group_id 或 access_level。它们的形式分别为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。您还可以通过 required_approvals 字段指定来自指定实体所需的批准数量。查看更多多重批准规则的信息。 |
要更新:
- user_id: 确保更新的用户属于给定群组且具有 Maintainer 角色(或以上)。您还必须在相应的哈希中传递 deploy_access_level 或 approval_rule 的 id。
- group_id: 确保更新的群组是该受保护环境所属群组的子群组。您还必须在相应的哈希中传递 deploy_access_level 或 approval_rule 的 id。
要删除:
- 您必须传递 _destroy 设置为 true。请参见以下示例。
示例:创建一个 deploy_access_level 记录
shellcurl --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"
示例响应:
json1{ 2 "name": "production", 3 "deploy_access_levels": [ 4 { 5 "id": 12, 6 "access_level": 40, 7 "access_level_description": "protected-access-group", 8 "user_id": null, 9 "group_id": 9899829, 10 "group_inheritance_type": 1 11 } 12 ], 13 "required_approval_count": 0 14}
示例:更新一个 deploy_access_level 记录
shellcurl --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"
json1{ 2 "name": "production", 3 "deploy_access_levels": [ 4 { 5 "id": 12, 6 "access_level": 40, 7 "access_level_description": "protected-access-group", 8 "user_id": null, 9 "group_id": 22034120, 10 "group_inheritance_type": 0 11 } 12 ], 13 "required_approval_count": 2 14}
示例:删除一个 deploy_access_level 记录
shellcurl --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"
示例响应:
json{ "name": "production", "deploy_access_levels": [], "required_approval_count": 0 }
示例:创建一个 approval_rule 记录
shellcurl --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"
示例响应:
json1{ 2 "name": "production", 3 "approval_rules": [ 4 { 5 "id": 38, 6 "user_id": null, 7 "group_id": 134, 8 "access_level": null, 9 "access_level_description": "qa-group", 10 "required_approvals": 1, 11 "group_inheritance_type": 0 12 } 13 ] 14}
示例:更新一个 approval_rule 记录
shellcurl --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"
json1{ 2 "name": "production", 3 "approval_rules": [ 4 { 5 "id": 38, 6 "user_id": null, 7 "group_id": 135, 8 "access_level": null, 9 "access_level_description": "security-group", 10 "required_approvals": 2, 11 "group_inheritance_type": 0 12 } 13 ] 14}
示例:删除一个 approval_rule 记录
shellcurl --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"
示例响应:
json{ "name": "production", "approval_rules": [] }
取消保护单个环境
取消保护给定的受保护环境。
plaintextDELETE /groups/:id/protected_environments/:name
shellcurl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/5/protected_environments/staging"
响应应该返回 200 代码。