Skip to main content
我们经常发布文档更新,此页面的翻译可能仍在进行中。 有关最新信息,请访问英语文档
REST API 现已经过版本控制。 有关详细信息,请参阅“关于 API 版本控制”。

部署密钥

使用 REST API 创建和管理部署密钥。

关于部署密钥

可以使用部署密钥(即授予对单个存储库的访问权限的 SSH 密钥)从 GitHub.com 上的存储库启动项目到服务器。 GitHub Enterprise Cloud 将密钥的公共部分直接附加到存储库而不是个人帐户,密钥的私有部分仍保留在服务器上。 有关详细信息,请参阅“交付部署”。

部署密钥可以使用以下 API 端点进行设置,也可以使用 GitHub 进行设置。 若要了解如何在 GitHub 中设置部署密钥,请参阅“管理部署密钥”。

List deploy keys

适用于 GitHub Apps

“List deploy keys”的参数

标头
名称, 类型, 说明
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. The name is not case sensitive.

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

The number of results per page (max 100).

默认: 30

page integer

Page number of the results to fetch.

默认: 1

“List deploy keys”的 HTTP 响应状态代码

状态代码说明
200

OK

“List deploy keys”的示例代码

get/repos/{owner}/{repo}/keys
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/keys

Response

Status: 200
[ { "id": 1, "key": "ssh-rsa AAA...", "url": "https://api.github.com/repos/octocat/Hello-World/keys/1", "title": "octocat@octomac", "verified": true, "created_at": "2014-12-10T15:53:42Z", "read_only": true, "added_by": "octocat", "last_used": "2022-01-10T15:53:42Z" } ]

Create a deploy key

适用于 GitHub Apps

You can create a read-only deploy key.

“Create a deploy key”的参数

标头
名称, 类型, 说明
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. The name is not case sensitive.

正文参数
名称, 类型, 说明
title string

A name for the key.

key string 必须

The contents of the key.

read_only boolean

If true, the key will only be able to read repository contents. Otherwise, the key will be able to read and write.

Deploy keys with write access can perform the same actions as an organization member with admin access, or a collaborator on a personal repository. For more information, see "Repository permission levels for an organization" and "Permission levels for a user account repository."

“Create a deploy key”的 HTTP 响应状态代码

状态代码说明
201

Created

422

Validation failed, or the endpoint has been spammed.

“Create a deploy key”的示例代码

post/repos/{owner}/{repo}/keys
curl -L \ -X POST \ -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/keys \ -d '{"title":"octocat@octomac","key":"ssh-rsa AAA...","read_only":true}'

Response

Status: 201
{ "id": 1, "key": "ssh-rsa AAA...", "url": "https://api.github.com/repos/octocat/Hello-World/keys/1", "title": "octocat@octomac", "verified": true, "created_at": "2014-12-10T15:53:42Z", "read_only": true, "added_by": "octocat", "last_used": "2022-01-10T15:53:42Z" }

Get a deploy key

适用于 GitHub Apps

“Get a deploy key”的参数

标头
名称, 类型, 说明
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. The name is not case sensitive.

key_id integer 必须

The unique identifier of the key.

“Get a deploy key”的 HTTP 响应状态代码

状态代码说明
200

OK

404

Resource not found

“Get a deploy key”的示例代码

get/repos/{owner}/{repo}/keys/{key_id}
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/keys/KEY_ID

Response

Status: 200
{ "id": 1, "key": "ssh-rsa AAA...", "url": "https://api.github.com/repos/octocat/Hello-World/keys/1", "title": "octocat@octomac", "verified": true, "created_at": "2014-12-10T15:53:42Z", "read_only": true, "added_by": "octocat", "last_used": "2022-01-10T15:53:42Z" }

Delete a deploy key

适用于 GitHub Apps

Deploy keys are immutable. If you need to update a key, remove the key and create a new one instead.

“Delete a deploy key”的参数

标头
名称, 类型, 说明
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. The name is not case sensitive.

key_id integer 必须

The unique identifier of the key.

“Delete a deploy key”的 HTTP 响应状态代码

状态代码说明
204

No Content

“Delete a deploy key”的示例代码

delete/repos/{owner}/{repo}/keys/{key_id}
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/keys/KEY_ID

Response

Status: 204