306.md 8.1 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
# 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: <your_access_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: <your_access_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: <your_access_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: <your_access_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: <your_access_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: <your_access_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: <your_access_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: <your_access_token>" "https://gitlab.example.com/api/v4/projects" 
```

或查找组的 ID:

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups" 
```

然后列出该组中的所有项目(例如,组 1234):

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/groups/1234" 
```

使用这些 ID,将相同的部署密钥添加到所有:

```
for project_id in 321 456 987; do curl --request POST --header "PRIVATE-TOKEN: <your_access_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 
```