# Deploy Keys API > 原文:[https://docs.gitlab.com/ee/api/deploy_keys.html](https://docs.gitlab.com/ee/api/deploy_keys.html) * [List all deploy keys](#list-all-deploy-keys) * [List project deploy keys](#list-project-deploy-keys) * [Single deploy key](#single-deploy-key) * [Add deploy key](#add-deploy-key) * [Update deploy key](#update-deploy-key) * [Delete deploy key](#delete-deploy-key) * [Enable a deploy key](#enable-a-deploy-key) * [Adding deploy keys to multiple projects](#adding-deploy-keys-to-multiple-projects) # Deploy Keys API[](#deploy-keys-api "Permalink") ## List all deploy keys[](#list-all-deploy-keys "Permalink") 获取 GitLab 实例的所有项目中所有部署密钥的列表. 该端点需要管理员访问权限,并且在 GitLab.com 上不可用. ``` GET /deploy_keys ``` ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/deploy_keys" ``` 响应示例: ``` [ { "id": 1, "title": "Public key", "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", "created_at": "2013-10-02T10:12:29Z" }, { "id": 3, "title": "Another Public key", "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", "created_at": "2013-10-02T11:12:29Z" } ] ``` ## List project deploy keys[](#list-project-deploy-keys "Permalink") 获取项目的部署密钥的列表. ``` GET /projects/:id/deploy_keys ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/5/deploy_keys" ``` 响应示例: ``` [ { "id": 1, "title": "Public key", "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", "created_at": "2013-10-02T10:12:29Z", "can_push": false }, { "id": 3, "title": "Another Public key", "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", "created_at": "2013-10-02T11:12:29Z", "can_push": false } ] ``` ## Single deploy key[](#single-deploy-key "Permalink") Get a single key. ``` GET /projects/:id/deploy_keys/:key_id ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | | `key_id` | integer | yes | 部署密钥的 ID | ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/5/deploy_keys/11" ``` 响应示例: ``` { "id": 1, "title": "Public key", "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", "created_at": "2013-10-02T10:12:29Z", "can_push": false } ``` ## Add deploy key[](#add-deploy-key "Permalink") 为项目创建一个新的部署密钥. 如果部署密钥已经存在于另一个项目中,则只有在同一用户可以访问原始密钥的情况下,它才会加入到当前项目中. ``` POST /projects/:id/deploy_keys ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | | `title` | string | yes | 新部署密钥的标题 | | `key` | string | yes | 新的部署密钥 | | `can_push` | boolean | no | 可以将按键推送部署到项目的存储库 | ``` curl --request POST --header "PRIVATE-TOKEN: " --header "Content-Type: application/json" --data '{"title": "My deploy key", "key": "ssh-rsa AAAA...", "can_push": "true"}' "https://gitlab.example.com/api/v4/projects/5/deploy_keys/" ``` 响应示例: ``` { "key" : "ssh-rsa AAAA...", "id" : 12, "title" : "My deploy key", "can_push": true, "created_at" : "2015-08-29T12:44:31.550Z" } ``` ## Update deploy key[](#update-deploy-key "Permalink") 更新项目的部署密钥. ``` PUT /projects/:id/deploy_keys/:key_id ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | | `title` | string | no | 新部署密钥的标题 | | `can_push` | boolean | no | 可以将按键推送部署到项目的存储库 | ``` curl --request PUT --header "PRIVATE-TOKEN: " --header "Content-Type: application/json" --data '{"title": "New deploy key", "can_push": true}' "https://gitlab.example.com/api/v4/projects/5/deploy_keys/11" ``` 响应示例: ``` { "id": 11, "title": "New deploy key", "key": "ssh-rsa AAAA...", "created_at": "2015-08-29T12:44:31.550Z", "can_push": true } ``` ## Delete deploy key[](#delete-deploy-key "Permalink") 从项目中删除部署密钥. 如果部署密钥仅用于该项目,则它将从系统中删除. ``` DELETE /projects/:id/deploy_keys/:key_id ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | | `key_id` | integer | yes | 部署密钥的 ID | ``` curl --request DELETE --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/5/deploy_keys/13" ``` ## Enable a deploy key[](#enable-a-deploy-key "Permalink") 为项目启用部署密钥,以便可以使用它. 返回成功的密钥,成功时返回状态码 201. ``` curl --request POST --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/5/deploy_keys/13/enable" ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) | | `key_id` | integer | yes | 部署密钥的 ID | 响应示例: ``` { "key" : "ssh-rsa AAAA...", "id" : 12, "title" : "My deploy key", "created_at" : "2015-08-29T12:44:31.550Z" } ``` ## Adding deploy keys to multiple projects[](#adding-deploy-keys-to-multiple-projects "Permalink") 如果您想轻松地将相同的部署密钥添加到同一组中的多个项目中,则可以使用 API​​轻松实现. 首先,通过列出所有项目来找到您感兴趣的项目的 ID: ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects" ``` 或查找组的 ID: ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/groups" ``` 然后列出该组中的所有项目(例如,组 1234): ``` curl --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/groups/1234" ``` 使用这些 ID,将相同的部署密钥添加到所有: ``` for project_id in 321 456 987; do curl --request POST --header "PRIVATE-TOKEN: " --header "Content-Type: application/json" \ --data '{"title": "my key", "key": "ssh-rsa AAAA..."}' "https://gitlab.example.com/api/v4/projects/${project_id}/deploy_keys" done ```