Project-level Variables API

原文:https://docs.gitlab.com/ee/api/project_level_variables.html

Project-level Variables API

List project variables

获取项目变量的列表.

  1. GET /projects/:id/variables
Attribute Type required Description
id integer/string yes 一个项目的 ID 或经过身份验证的用户拥有的该项目的 Urlencoded NAMESPACE / PROJECT_NAME
  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables"
  1. [ { "key": "TEST_VARIABLE_1", "variable_type": "env_var", "value": "TEST_1" }, { "key": "TEST_VARIABLE_2", "variable_type": "env_var", "value": "TEST_2" } ]

Show variable details

获取项目特定变量的详细信息.

  1. GET /projects/:id/variables/:key
Attribute Type required Description
id integer/string yes 一个项目的 ID 或经过身份验证的用户拥有的该项目的 Urlencoded NAMESPACE / PROJECT_NAME
key string yes 变量的key
filter hash no 可用的过滤器: [environment_scope] . 请参阅filter参数详细信息 .
  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/TEST_VARIABLE_1"
  1. { "key": "TEST_VARIABLE_1", "variable_type": "env_var", "value": "TEST_1", "protected": false, "masked": true }

Create variable

创建一个新变量.

  1. POST /projects/:id/variables
Attribute Type required Description
id integer/string yes 一个项目的 ID 或经过身份验证的用户拥有的该项目的 Urlencoded NAMESPACE / PROJECT_NAME
key string yes The key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed
value string yes 变量的value
variable_type string no 变量的类型. 可用类型为: env_var (默认)和file
protected boolean no 变量是否受保护
masked boolean no 变量是否被屏蔽
environment_scope string no 变量的environment_scope
  1. 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"
  1. { "key": "NEW_VARIABLE", "value": "new value", "protected": false, "variable_type": "env_var", "masked": false, "environment_scope": "*" }

Update variable

更新项目的变量.

  1. PUT /projects/:id/variables/:key
Attribute Type required Description
id integer/string yes 一个项目的 ID 或经过身份验证的用户拥有的该项目的 Urlencoded NAMESPACE / PROJECT_NAME
key string yes 变量的key
value string yes 变量的value
variable_type string no 变量的类型. 可用类型为: env_var (默认)和file
protected boolean no 变量是否受保护
masked boolean no 变量是否被屏蔽
environment_scope string no 变量的environment_scope
filter hash no 可用的过滤器: [environment_scope] . 请参阅filter参数详细信息 .
  1. curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE" --form "value=updated value"
  1. { "key": "NEW_VARIABLE", "value": "updated value", "variable_type": "env_var", "protected": true, "masked": false, "environment_scope": "*" }

Remove variable

删除项目的变量.

  1. DELETE /projects/:id/variables/:key
Attribute Type required Description
id integer/string yes 一个项目的 ID 或经过身份验证的用户拥有的该项目的 Urlencoded NAMESPACE / PROJECT_NAME
key string yes 变量的key
filter hash no 可用的过滤器: [environment_scope] . 请参阅filter参数详细信息 .
  1. curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/VARIABLE_1"

The filter parameter

版本历史

  • 在 GitLab 13.2 中引入 .
  • 它部署在功能标记后面,默认情况下处于禁用状态.
  • 在 GitLab.com 上已禁用.
  • 要在 GitLab 自管实例中使用它,请让 GitLab 管理员启用它.

此参数用于按属性(例如environment_scope进行过滤.

用法示例:

  1. curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/VARIABLE_1?filter[environment_scope]=production"

Enable or disable

有权访问 GitLab Rails 控制台的 GitLab 管理员可以为您的实例启用它.

要启用它:

  1. Feature.enable(:ci_variables_api_filter_environment_scope)

禁用它:

  1. Feature.disable(:ci_variables_api_filter_environment_scope)