正式なドキュメントは英語版であり、この日本語訳はAI支援翻訳により作成された参考用のものです。日本語訳の一部の内容は人間によるレビューがまだ行われていないため、翻訳のタイミングにより英語版との間に差異が生じることがあります。最新かつ正確な情報については、英語版をご参照ください。

保護環境API

  • プラン: Premium、Ultimate
  • 提供形態: GitLab.com、GitLab Self-Managed、GitLab Dedicated

このAPIを使用して、保護環境とやり取りします。

グループレベルのgroup-level protected environments APIについては、こちらを参照してください

有効なアクセスレベル

アクセスレベルは、ProtectedEnvironments::DeployAccessLevel::ALLOWED_ACCESS_LEVELSメソッドで定義されています。現在、次のレベルが認識されています:

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

グループメンバーシップの継承の種類

グループメンバーシップの継承により、デプロイアクセスレベルと承認ルールは、継承されたグループメンバーシップを考慮に入れることができます。グループメンバーシップの継承の種類は、ProtectedEnvironments::Authorizable::GROUP_INHERITANCE_TYPEで定義されます。次の種類が認識されます:

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

保護された環境の一覧

プロジェクトから保護環境の一覧を取得します:

GET /projects/:id/protected_environments
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_environments/"

レスポンス例:

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

単一の保護環境を取得

単一の保護環境を取得します:

GET /projects/:id/protected_environments/:name
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
name文字列はい保護環境の名前
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_environments/production"

レスポンス例:

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

単一の環境を保護

単一の環境を保護します:

POST /projects/:id/protected_environments
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
name文字列はい環境の名前。
deploy_access_levels配列はいデプロイを許可されたアクセスレベルの配列。それぞれはハッシュで記述されます。
approval_rules配列いいえ承認を許可されたアクセスレベルの配列。それぞれはハッシュで記述されます。複数の承認ルールを参照してください。

deploy_access_levelsおよびapproval_rules配列内の要素は、user_idgroup_id、またはaccess_levelのいずれかで、形式は{user_id: integer}{group_id: integer}、または{access_level: integer}になります。オプションで、有効なグループメンバーシップの継承タイプのいずれかとして、各group_inheritance_typeを指定できます。

各ユーザーはプロジェクトへのアクセス権を持ち、各グループはこのプロジェクトを共有する必要があります。

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>" \
     --url "https://gitlab.example.com/api/v4/projects/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,
         "group_inheritance_type": 0
      }
   ],
   "required_approval_count": 0,
   "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
      },
      {
         "id": 39,
         "user_id": null,
         "group_id": 135,
         "access_level": null,
         "access_level_description": "security-group",
         "required_approvals": 2,
         "group_inheritance_type": 0
      }
   ]
}

保護環境を更新

単一の環境を更新します。

PUT /projects/:id/protected_environments/:name
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
name文字列はい環境の名前。
deploy_access_levels配列いいえデプロイを許可されたアクセスレベルの配列。それぞれはハッシュで記述されます。
approval_rules配列いいえ承認を許可されたアクセスレベルの配列。それぞれはハッシュで記述されます。詳細については、複数の承認ルールを参照してください。

deploy_access_levelsおよびapproval_rules配列内の要素は、user_idgroup_id、またはaccess_levelのいずれかで、形式は{user_id: integer}{group_id: integer}、または{access_level: integer}になります。オプションで、有効なグループメンバーシップの継承タイプのいずれかとして、各group_inheritance_typeを指定できます。

更新するには:

  • user_id: 更新されたユーザーがプロジェクトへのアクセス権を持っていることを確認します。それぞれのハッシュで、deploy_access_levelデプロイアクセスレベルまたはapproval_rule承認ルールのidも渡す必要があります。
  • group_id: 更新されたグループがこのプロジェクトを共有していることを確認します。それぞれのハッシュで、deploy_access_levelデプロイアクセスレベルまたはapproval_rule承認ルールのidも渡す必要があります。

削除するには:

  • _destroytrueに設定して渡す必要があります。次の例を参照してください。

例: 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>" \
     --url "https://gitlab.example.com/api/v4/projects/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>" \
     --url "https://gitlab.example.com/api/v4/projects/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>" \
     --url "https://gitlab.example.com/api/v4/projects/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>" \
     --url "https://gitlab.example.com/api/v4/projects/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>" \
     --url "https://gitlab.example.com/api/v4/projects/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>" \
     --url "https://gitlab.example.com/api/v4/projects/22034114/protected_environments/production"

レスポンス例:

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

単一の環境の保護を解除

指定された保護環境の保護を解除します:

DELETE /projects/:id/protected_environments/:name
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
name文字列はい保護環境の名前。
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_environments/staging"