Help us learn about your current experience with the documentation. Take the survey.
项目级 CI/CD 变量 API
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
使用此 API 与项目的 CI/CD 变量 进行交互。
列出项目变量
获取项目变量列表。使用 page 和 per_page 分页
参数来控制结果的分页。
GET /projects/:id/variables| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | ID 或 项目的 URL 编码路径 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables"示例响应:
[
{
"variable_type": "env_var",
"key": "TEST_VARIABLE_1",
"value": "TEST_1",
"protected": false,
"masked": true,
"hidden": false,
"raw": false,
"environment_scope": "*",
"description": null
},
{
"variable_type": "env_var",
"key": "TEST_VARIABLE_2",
"value": "TEST_2",
"protected": false,
"masked": false,
"hidden": false,
"raw": false,
"environment_scope": "*",
"description": null
}
]获取单个变量
获取单个变量的详细信息。如果有多个具有相同键的变量,
使用 filter 来选择正确的 environment_scope。
GET /projects/:id/variables/:key| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | ID 或 项目的 URL 编码路径 |
key |
string | 是 | 变量的 key |
filter |
hash | 否 | 可用过滤器:[environment_scope]。请参阅 filter 参数详情。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/TEST_VARIABLE_1"示例响应:
{
"key": "TEST_VARIABLE_1",
"variable_type": "env_var",
"value": "TEST_1",
"protected": false,
"masked": true,
"hidden": false,
"raw": false,
"environment_scope": "*",
"description": null
}创建变量
创建新变量。如果已存在具有相同 key 的变量,
新变量必须具有不同的 environment_scope。否则,GitLab 会返回类似的消息:
VARIABLE_NAME has already been taken。
POST /projects/:id/variables| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | ID 或 项目的 URL 编码路径 |
key |
string | 是 | 变量的 key;不能超过 255 个字符;只允许 A-Z、a-z、0-9 和 _ |
value |
string | 是 | 变量的 value |
description |
string | 否 | 变量的描述。默认值:null。在 GitLab 16.2 中引入。 |
environment_scope |
string | 否 | 变量的 environment_scope。默认值:* |
masked |
boolean | 否 | 变量是否被遮盖。默认值:false |
masked_and_hidden |
boolean | 否 | 变量是否被遮盖和隐藏。默认值:false |
protected |
boolean | 否 | 变量是否受保护。默认值:false |
raw |
boolean | 否 | 变量是否被视为原始字符串。默认值:false。当为 true 时,值中的变量不会 展开。 |
variable_type |
string | 否 | 变量的类型。可用类型有:env_var(默认)和 file |
示例请求:
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"示例响应:
{
"variable_type": "env_var",
"key": "NEW_VARIABLE",
"value": "new value",
"protected": false,
"masked": false,
"hidden": false,
"raw": false,
"environment_scope": "*",
"description": null
}更新变量
更新项目的变量。如果有多个具有相同键的变量,
使用 filter 来选择正确的 environment_scope。
PUT /projects/:id/variables/:key| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | ID 或 项目的 URL 编码路径 |
key |
string | 是 | 变量的 key |
value |
string | 是 | 变量的 value |
description |
string | 否 | 变量的描述。默认值:null。在 GitLab 16.2 中引入。 |
environment_scope |
string | 否 | 变量的 environment_scope |
filter |
hash | 否 | 可用过滤器:[environment_scope]。请参阅 filter 参数详情。 |
masked |
boolean | 否 | 变量是否被遮盖 |
protected |
boolean | 否 | 变量是否受保护 |
raw |
boolean | 否 | 变量是否被视为原始字符串。默认值:false。当为 true 时,值中的变量不会 展开。 |
variable_type |
string | 否 | 变量的类型。可用类型有:env_var(默认)和 file |
示例请求:
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE" --form "value=updated value"示例响应:
{
"variable_type": "env_var",
"key": "NEW_VARIABLE",
"value": "updated value",
"protected": true,
"masked": false,
"hidden": false,
"raw": false,
"environment_scope": "*",
"description": "null"
}删除变量
删除项目的变量。如果有多个具有相同键的变量,
使用 filter 来选择正确的 environment_scope。
DELETE /projects/:id/variables/:key| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | ID 或 项目的 URL 编码路径 |
key |
string | 是 | 变量的 key |
filter |
hash | 否 | 可用过滤器:[environment_scope]。请参阅 filter 参数详情。 |
示例请求:
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/VARIABLE_1"filter 参数
当多个变量具有相同的 key 时,GET、PUT
或 DELETE 请求可能会返回:
There are multiple variables with provided parameters. Please use 'filter[environment_scope]'.使用 filter[environment_scope] 来选择具有匹配 environment_scope 属性的变量。
例如:
-
GET:
curl --globoff --header "PRIVATE-TOKEN: <your_access_token>" \ "https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production" -
PUT:
curl --request PUT --globoff --header "PRIVATE-TOKEN: <your_access_token>" \ "https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?value=scoped-variable-updated-value&environment_scope=production&filter[environment_scope]=production" -
DELETE:
curl --request DELETE --globoff --header "PRIVATE-TOKEN: <your_access_token>" \ "https://gitlab.example.com/api/v4/projects/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production"