受保护环境 API

  1. Tier: 专业版, 旗舰版
  2. Offering: JihuLab.com, 私有化部署

有效的访问级别#

访问级别在 ProtectedEnvironments::DeployAccessLevel::ALLOWED_ACCESS_LEVELS 方法中定义。目前,这些级别被识别:

plaintext
30 => Developer access 40 => Maintainer access 60 => Admin access

群组继承类型#

群组继承允许部署访问级别和访问规则考虑继承的群组成员资格。群组继承类型由 ProtectedEnvironments::Authorizable::GROUP_INHERITANCE_TYPE 定义。以下类型被识别:

plaintext
0 => Direct group membership only (default) 1 => All inherited groups

列出受保护的环境#

从一个项目中获取受保护环境的列表:

plaintext
GET /projects/:id/protected_environments
属性类型必需描述
idinteger/string项目的 ID 或 URL 编码的路径
shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_environments/"

示例响应:

json
1[ 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 "group_inheritance_type": 0 12 } 13 ], 14 "required_approval_count": 0 15 } 16]

获取单个受保护的环境#

获取一个单个受保护的环境:

plaintext
GET /projects/:id/protected_environments/:name
属性类型必需描述
idinteger/string项目的 ID 或 URL 编码的路径
namestring受保护环境的名称
shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_environments/production"

示例响应:

json
1{ 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 "group_inheritance_type": 0 11 } 12 ], 13 "required_approval_count": 0 14}

保护单个环境#

保护一个单个环境:

plaintext
POST /projects/:id/protected_environments
属性类型必需描述
idinteger/string项目的 ID 或 URL 编码的路径
namestring环境的名称。
deploy_access_levelsarray允许部署的访问级别数组,每个由哈希描述。
approval_rulesarray允许批准的访问级别数组,每个由哈希描述。参见 多个批准规则

deploy_access_levelsapproval_rules 数组中的元素应为 user_idgroup_idaccess_level 之一,并采用 {user_id: integer}{group_id: integer}{access_level: integer} 的形式。可以选择在每个上指定 group_inheritance_type,作为有效的群组继承类型之一。每个用户必须对项目有访问权限,每个群组必须共享此项目

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

示例响应:

json
1{ 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 "group_inheritance_type": 0 11 } 12 ], 13 "required_approval_count": 0, 14 "approval_rules": [ 15 { 16 "id": 38, 17 "user_id": null, 18 "group_id": 134, 19 "access_level": null, 20 "access_level_description": "qa-group", 21 "required_approvals": 1, 22 "group_inheritance_type": 0 23 }, 24 { 25 "id": 39, 26 "user_id": null, 27 "group_id": 135, 28 "access_level": null, 29 "access_level_description": "security-group", 30 "required_approvals": 2, 31 "group_inheritance_type": 0 32 } 33 ] 34}

更新受保护的环境#

History
    • 引入于极狐GitLab 15.4。

更新单个环境。

plaintext
PUT /projects/:id/protected_environments/:name
属性类型必需描述
idinteger/string项目的 ID 或 URL 编码的路径
namestring环境的名称。
deploy_access_levelsarray允许部署的访问级别数组,每个由哈希描述。
approval_rulesarray允许批准的访问级别数组,每个由哈希描述。参见 多个批准规则 以获取更多信息。

deploy_access_levelsapproval_rules 数组中的元素应为 user_idgroup_idaccess_level 之一,并采用 {user_id: integer}{group_id: integer}{access_level: integer} 的形式。可以选择在每个上指定 group_inheritance_type,作为有效的群组继承类型之一。

更新:

  1. user_id: 确保更新的用户对项目有访问权限。您还必须在相应的哈希中传递 deploy_access_levelapproval_ruleid
  2. group_id: 确保更新的群组共享此项目。您还必须在相应的哈希中传递 deploy_access_levelapproval_ruleid

删除:

  1. 您必须传递 _destroy 设置为 true。请参见以下示例。

示例:创建一个 deploy_access_level 记录#

shell
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/projects/22034114/protected_environments/production"

示例响应:

json
1{ 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 记录#

shell
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/projects/22034114/protected_environments/production"
json
1{ 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 记录#

shell
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/projects/22034114/protected_environments/production"

示例响应:

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

示例:创建一个 approval_rule 记录#

shell
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/projects/22034114/protected_environments/production"

示例响应:

json
1{ 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 记录#

shell
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/projects/22034114/protected_environments/production"
json
1{ 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 记录#

shell
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/projects/22034114/protected_environments/production"

示例响应:

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

取消保护单个环境#

取消保护给定的受保护环境:

plaintext
DELETE /projects/:id/protected_environments/:name
属性类型必需描述
idinteger/string项目的 ID 或 URL 编码的路径
namestring受保护环境的名称。
shell
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_environments/staging"