跳至主要内容
REST API 现在已版本化。 有关更多信息,请参阅 "关于 API 版本控制."

用于部署环境的 REST API 端点

使用 REST API 创建、配置和删除部署环境。

关于部署环境

有关环境的更多信息,请参阅 "使用环境进行部署." 要管理环境机密,请参阅 "用于 GitHub Actions 机密的 REST API 端点."

环境、环境机密和部署保护规则在所有当前 GitHub 计划的公共存储库中可用。它们在旧计划(如 Bronze、Silver 或 Gold)中不可用。要访问私有或内部存储库中的环境、环境机密和部署分支,您必须使用 GitHub Pro、GitHub Team 或 GitHub Enterprise。如果您使用的是 GitHub Free、GitHub Pro 或 GitHub Team 计划,其他部署保护规则(例如等待计时器或必需的审阅者)仅适用于公共存储库。

列出环境

列出存储库的环境。

任何具有存储库读取权限的人都可以使用此端点。

OAuth 应用程序令牌和个人访问令牌(经典)需要 repo 范围才能将此端点与私有存储库一起使用。

用于“列出环境”的细粒度访问令牌

此端点适用于以下细粒度令牌类型

细粒度令牌必须具有以下权限集

  • “操作”存储库权限(读取)

如果仅请求公共资源,则无需身份验证或上述权限即可使用此端点。

“列出环境”的参数

标头
名称、类型、描述
accept string

建议设置为 application/vnd.github+json

路径参数
名称、类型、描述
owner string 必需

存储库的帐户所有者。名称不区分大小写。

repo string 必需

存储库的名称,不带 .git 扩展名。名称不区分大小写。

查询参数
名称、类型、描述
per_page integer

每页结果数(最大 100)。有关更多信息,请参阅“在 REST API 中使用分页”。

默认: 30

page integer

要获取的结果的页码。有关更多信息,请参阅“在 REST API 中使用分页”。

默认: 1

“列出环境”的 HTTP 响应状态代码

状态代码描述
200

OK

“列出环境”的代码示例

请求示例

获取/repos/{owner}/{repo}/environments
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments

响应

状态: 200
{ "total_count": 1, "environments": [ { "id": 161088068, "node_id": "MDExOkVudmlyb25tZW50MTYxMDg4MDY4", "name": "staging", "url": "https://api.github.com/repos/github/hello-world/environments/staging", "html_url": "https://github.com/github/hello-world/deployments/activity_log?environments_filter=staging", "created_at": "2020-11-23T22:00:40Z", "updated_at": "2020-11-23T22:00:40Z", "protection_rules": [ { "id": 3736, "node_id": "MDQ6R2F0ZTM3MzY=", "type": "wait_timer", "wait_timer": 30 }, { "id": 3755, "node_id": "MDQ6R2F0ZTM3NTU=", "prevent_self_review": false, "type": "required_reviewers", "reviewers": [ { "type": "User", "reviewer": { "login": "octocat", "id": 1, "node_id": "MDQ6VXNlcjE=", "avatar_url": "https://github.com/images/error/octocat_happy.gif", "gravatar_id": "", "url": "https://api.github.com/users/octocat", "html_url": "https://github.com/octocat", "followers_url": "https://api.github.com/users/octocat/followers", "following_url": "https://api.github.com/users/octocat/following{/other_user}", "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", "organizations_url": "https://api.github.com/users/octocat/orgs", "repos_url": "https://api.github.com/users/octocat/repos", "events_url": "https://api.github.com/users/octocat/events{/privacy}", "received_events_url": "https://api.github.com/users/octocat/received_events", "type": "User", "site_admin": false } }, { "type": "Team", "reviewer": { "id": 1, "node_id": "MDQ6VGVhbTE=", "url": "https://api.github.com/teams/1", "html_url": "https://github.com/orgs/github/teams/justice-league", "name": "Justice League", "slug": "justice-league", "description": "A great team.", "privacy": "closed", "notification_setting": "notifications_enabled", "permission": "admin", "members_url": "https://api.github.com/teams/1/members{/member}", "repositories_url": "https://api.github.com/teams/1/repos", "parent": null } } ] }, { "id": 3756, "node_id": "MDQ6R2F0ZTM3NTY=", "type": "branch_policy" } ], "deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true } } ] }

获取环境

注意:要获取有关分支必须匹配的名称模式才能部署到此环境的信息,请参阅“获取部署分支策略”。

任何具有存储库读取权限的人都可以使用此端点。

OAuth 应用程序令牌和个人访问令牌(经典)需要 repo 范围才能将此端点与私有存储库一起使用。

用于“获取环境”的细粒度访问令牌

此端点适用于以下细粒度令牌类型

细粒度令牌必须具有以下权限集

  • “操作”存储库权限(读取)

如果仅请求公共资源,则无需身份验证或上述权限即可使用此端点。

用于“获取环境”的参数

标头
名称、类型、描述
accept string

建议设置为 application/vnd.github+json

路径参数
名称、类型、描述
owner string 必需

存储库的帐户所有者。名称不区分大小写。

repo string 必需

存储库的名称,不带 .git 扩展名。名称不区分大小写。

environment_name 字符串 必需

环境的名称。名称必须进行 URL 编码。例如,名称中的任何斜杠都必须替换为 %2F

用于“获取环境”的 HTTP 响应状态代码

状态代码描述
200

OK

用于“获取环境”的代码示例

请求示例

获取/repos/{owner}/{repo}/environments/{environment_name}
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME

响应

状态: 200
{ "id": 161088068, "node_id": "MDExOkVudmlyb25tZW50MTYxMDg4MDY4", "name": "staging", "url": "https://api.github.com/repos/github/hello-world/environments/staging", "html_url": "https://github.com/github/hello-world/deployments/activity_log?environments_filter=staging", "created_at": "2020-11-23T22:00:40Z", "updated_at": "2020-11-23T22:00:40Z", "protection_rules": [ { "id": 3736, "node_id": "MDQ6R2F0ZTM3MzY=", "type": "wait_timer", "wait_timer": 30 }, { "id": 3755, "node_id": "MDQ6R2F0ZTM3NTU=", "prevent_self_review": false, "type": "required_reviewers", "reviewers": [ { "type": "User", "reviewer": { "login": "octocat", "id": 1, "node_id": "MDQ6VXNlcjE=", "avatar_url": "https://github.com/images/error/octocat_happy.gif", "gravatar_id": "", "url": "https://api.github.com/users/octocat", "html_url": "https://github.com/octocat", "followers_url": "https://api.github.com/users/octocat/followers", "following_url": "https://api.github.com/users/octocat/following{/other_user}", "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", "organizations_url": "https://api.github.com/users/octocat/orgs", "repos_url": "https://api.github.com/users/octocat/repos", "events_url": "https://api.github.com/users/octocat/events{/privacy}", "received_events_url": "https://api.github.com/users/octocat/received_events", "type": "User", "site_admin": false } }, { "type": "Team", "reviewer": { "id": 1, "node_id": "MDQ6VGVhbTE=", "url": "https://api.github.com/teams/1", "html_url": "https://github.com/orgs/github/teams/justice-league", "name": "Justice League", "slug": "justice-league", "description": "A great team.", "privacy": "closed", "notification_setting": "notifications_enabled", "permission": "admin", "members_url": "https://api.github.com/teams/1/members{/member}", "repositories_url": "https://api.github.com/teams/1/repos", "parent": null } } ] }, { "id": 3756, "node_id": "MDQ6R2F0ZTM3NTY=", "type": "branch_policy" } ], "deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true } }

创建或更新环境

创建或更新具有保护规则的环境,例如必需的审阅者。有关环境保护规则的更多信息,请参阅“环境”。

注意:要创建或更新分支必须匹配的名称模式才能部署到此环境,请参阅“部署分支策略”。

注意:要创建或更新环境的机密,请参阅“GitHub Actions 机密”。

OAuth 应用令牌和个人访问令牌(经典)需要repo范围才能使用此端点。

用于“创建或更新环境”的细粒度访问令牌

此端点适用于以下细粒度令牌类型

细粒度令牌必须具有以下权限集

  • “管理”存储库权限(写入)

用于“创建或更新环境”的参数

标头
名称、类型、描述
accept string

建议设置为 application/vnd.github+json

路径参数
名称、类型、描述
owner string 必需

存储库的帐户所有者。名称不区分大小写。

repo string 必需

存储库的名称,不带 .git 扩展名。名称不区分大小写。

environment_name 字符串 必需

环境的名称。名称必须进行 URL 编码。例如,名称中的任何斜杠都必须替换为 %2F

主体参数
名称、类型、描述
wait_timer 整数

在作业最初触发后延迟作业的时间。时间(以分钟为单位)必须是 0 到 43,200(30 天)之间的整数。

prevent_self_review 布尔值

是否阻止创建作业的用户批准自己的作业。

reviewers 对象数组或 null

可以审阅引用该环境的作业的人员或团队。您可以列出最多六个用户或团队作为审阅者。审阅者必须至少对存储库具有读取权限。仅需一位必需的审阅者批准作业,该作业即可继续进行。

名称、类型、描述
type 字符串

审阅者的类型。

可以是以下之一: User, Team

id 整数

可以审阅部署的用户或团队的 ID

deployment_branch_policy 对象或 null

此环境的部署分支策略类型。要允许所有分支部署,请设置为null

名称、类型、描述
protected_branches 布尔值 必需

是否只有具有分支保护规则的分支才能部署到此环境。如果protected_branchestrue,则custom_branch_policies 必须为false;如果protected_branchesfalse,则custom_branch_policies 必须为true

custom_branch_policies 布尔值 必需

是否只有与指定名称模式匹配的分支才能部署到此环境。如果custom_branch_policiestrue,则protected_branches 必须为false;如果custom_branch_policiesfalse,则protected_branches 必须为true

创建或更新环境的 HTTP 响应状态码

状态代码描述
200

OK

422

当环境名称无效或 deployment_branch_policy 中的 protected_branchescustom_branch_policies 设置为相同值时,验证错误

创建或更新环境的代码示例

请求示例

put/repos/{owner}/{repo}/environments/{environment_name}
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME \ -d '{"wait_timer":30,"prevent_self_review":false,"reviewers":[{"type":"User","id":1},{"type":"Team","id":1}],"deployment_branch_policy":{"protected_branches":false,"custom_branch_policies":true}}'

响应

状态: 200
{ "id": 161088068, "node_id": "MDExOkVudmlyb25tZW50MTYxMDg4MDY4", "name": "staging", "url": "https://api.github.com/repos/github/hello-world/environments/staging", "html_url": "https://github.com/github/hello-world/deployments/activity_log?environments_filter=staging", "created_at": "2020-11-23T22:00:40Z", "updated_at": "2020-11-23T22:00:40Z", "protection_rules": [ { "id": 3736, "node_id": "MDQ6R2F0ZTM3MzY=", "type": "wait_timer", "wait_timer": 30 }, { "id": 3755, "node_id": "MDQ6R2F0ZTM3NTU=", "prevent_self_review": false, "type": "required_reviewers", "reviewers": [ { "type": "User", "reviewer": { "login": "octocat", "id": 1, "node_id": "MDQ6VXNlcjE=", "avatar_url": "https://github.com/images/error/octocat_happy.gif", "gravatar_id": "", "url": "https://api.github.com/users/octocat", "html_url": "https://github.com/octocat", "followers_url": "https://api.github.com/users/octocat/followers", "following_url": "https://api.github.com/users/octocat/following{/other_user}", "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", "organizations_url": "https://api.github.com/users/octocat/orgs", "repos_url": "https://api.github.com/users/octocat/repos", "events_url": "https://api.github.com/users/octocat/events{/privacy}", "received_events_url": "https://api.github.com/users/octocat/received_events", "type": "User", "site_admin": false } }, { "type": "Team", "reviewer": { "id": 1, "node_id": "MDQ6VGVhbTE=", "url": "https://api.github.com/teams/1", "html_url": "https://github.com/orgs/github/teams/justice-league", "name": "Justice League", "slug": "justice-league", "description": "A great team.", "privacy": "closed", "notification_setting": "notifications_enabled", "permission": "admin", "members_url": "https://api.github.com/teams/1/members{/member}", "repositories_url": "https://api.github.com/teams/1/repos", "parent": null } } ] }, { "id": 3756, "node_id": "MDQ6R2F0ZTM3NTY=", "type": "branch_policy" } ], "deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true } }

删除环境

OAuth 应用令牌和个人访问令牌(经典)需要repo范围才能使用此端点。

删除环境的细粒度访问令牌

此端点适用于以下细粒度令牌类型

细粒度令牌必须具有以下权限集

  • “管理”存储库权限(写入)

删除环境的参数

标头
名称、类型、描述
accept string

建议设置为 application/vnd.github+json

路径参数
名称、类型、描述
owner string 必需

存储库的帐户所有者。名称不区分大小写。

repo string 必需

存储库的名称,不带 .git 扩展名。名称不区分大小写。

environment_name 字符串 必需

环境的名称。名称必须进行 URL 编码。例如,名称中的任何斜杠都必须替换为 %2F

删除环境的 HTTP 响应状态码

状态代码描述
204

默认响应

删除环境的代码示例

请求示例

delete/repos/{owner}/{repo}/environments/{environment_name}
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME

默认响应

状态:204