Skip to main content
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 计划,则其他部署保护规则(如等待计时器或需要审阅者)仅在公共存储库中提供。

List environments

Lists the environments for a repository.

Anyone with read access to the repository can use this endpoint.

OAuth app tokens and personal access tokens (classic) need the repo scope to use this endpoint with a private repository.

“List environments”的细粒度访问令牌

此终结点适用于以下令牌类型:

令牌必须具有以下权限集:

  • actions:read

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

“List environments”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

查询参数
名称, 类型, 说明
per_page integer

The number of results per page (max 100). For more information, see "Using pagination in the REST API."

默认: 30

page integer

The page number of the results to fetch. For more information, see "Using pagination in the REST API."

默认: 1

“List environments”的 HTTP 响应状态代码

状态代码说明
200

OK

“List environments”的示例代码

请求示例

get/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

Response

Status: 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 } } ] }

Get an environment

Note: To get information about name patterns that branches must match in order to deploy to this environment, see "Get a deployment branch policy."

Anyone with read access to the repository can use this endpoint.

OAuth app tokens and personal access tokens (classic) need the repo scope to use this endpoint with a private repository.

“Get an environment”的细粒度访问令牌

此终结点适用于以下令牌类型:

令牌必须具有以下权限集:

  • actions:read

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

“Get an environment”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

environment_name string 必须

The name of the environment. The name must be URL encoded. For example, any slashes in the name must be replaced with %2F.

“Get an environment”的 HTTP 响应状态代码

状态代码说明
200

OK

“Get an environment”的示例代码

请求示例

get/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

Response

Status: 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 } }

Create or update an environment

Create or update an environment with protection rules, such as required reviewers. For more information about environment protection rules, see "Environments."

Note: To create or update name patterns that branches must match in order to deploy to this environment, see "Deployment branch policies."

Note: To create or update secrets for an environment, see "GitHub Actions secrets."

OAuth app tokens and personal access tokens (classic) need the repo scope to use this endpoint.

“Create or update an environment”的细粒度访问令牌

此终结点适用于以下令牌类型:

令牌必须具有以下权限集:

  • administration:write

“Create or update an environment”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

environment_name string 必须

The name of the environment. The name must be URL encoded. For example, any slashes in the name must be replaced with %2F.

正文参数
名称, 类型, 说明
wait_timer integer

The amount of time to delay a job after the job is initially triggered. The time (in minutes) must be an integer between 0 and 43,200 (30 days).

prevent_self_review boolean

Whether or not a user who created the job is prevented from approving their own job.

reviewers array of objects or null

The people or teams that may review jobs that reference the environment. You can list up to six users or teams as reviewers. The reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.

名称, 类型, 说明
type string

The type of reviewer.

可以是以下选项之一: User, Team

id integer

The id of the user or team who can review the deployment

deployment_branch_policy object or null

The type of deployment branch policy for this environment. To allow all branches to deploy, set to null.

名称, 类型, 说明
protected_branches boolean 必须

Whether only branches with branch protection rules can deploy to this environment. If protected_branches is true, custom_branch_policies must be false; if protected_branches is false, custom_branch_policies must be true.

custom_branch_policies boolean 必须

Whether only branches that match the specified name patterns can deploy to this environment. If custom_branch_policies is true, protected_branches must be false; if custom_branch_policies is false, protected_branches must be true.

“Create or update an environment”的 HTTP 响应状态代码

状态代码说明
200

OK

422

Validation error when the environment name is invalid or when protected_branches and custom_branch_policies in deployment_branch_policy are set to the same value

“Create or update an environment”的示例代码

请求示例

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}}'

Response

Status: 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 } }

Delete an environment

OAuth app tokens and personal access tokens (classic) need the repo scope to use this endpoint.

“Delete an environment”的细粒度访问令牌

此终结点适用于以下令牌类型:

令牌必须具有以下权限集:

  • administration:write

“Delete an environment”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

environment_name string 必须

The name of the environment. The name must be URL encoded. For example, any slashes in the name must be replaced with %2F.

“Delete an environment”的 HTTP 响应状态代码

状态代码说明
204

Default response

“Delete an environment”的示例代码

请求示例

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

Default response

Status: 204