Help us learn about your current experience with the documentation. Take the survey.

组级别变量接口

  • 版本:Free, Premium, Ultimate
  • 产品:GitLab.com, GitLab Self-Managed, GitLab Dedicated

使用此接口来管理组的 CI/CD 变量

列出组变量

获取组的变量列表。使用 pageper_page 分页 参数来控制结果的分页。

GET /groups/:id/variables
属性 类型 是否必需 描述
id integer/string 组的 ID 或组的 URL 编码路径
curl \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/1/variables"
[
    {
        "key": "TEST_VARIABLE_1",
        "variable_type": "env_var",
        "value": "TEST_1",
        "protected": false,
        "masked": false,
        "hidden": false,
        "raw": false,
        "environment_scope": "*",
        "description": null
    },
    {
        "key": "TEST_VARIABLE_2",
        "variable_type": "env_var",
        "value": "TEST_2",
        "protected": false,
        "masked": false,
        "hidden": false,
        "raw": false,
        "environment_scope": "*",
        "description": null
    }
]

显示变量详情

获取组中特定变量的详情。如果存在多个具有相同键的变量, 请使用 filter 来选择正确的 environment_scope

GET /groups/:id/variables/:key
属性 类型 是否必需 描述
id integer/string 组的 ID 或组的 URL 编码路径
key string 变量的 key
filter hash 可用过滤器:[environment_scope]。请参阅 filter 参数详情
curl \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/1/variables/TEST_VARIABLE_1"
{
    "key": "TEST_VARIABLE_1",
    "variable_type": "env_var",
    "value": "TEST_1",
    "protected": false,
    "masked": false,
    "hidden": false,
    "raw": false,
    "environment_scope": "*",
    "description": null
}

创建变量

创建一个新变量。

POST /groups/:id/variables
属性 类型 是否必需 描述
id integer/string 组的 ID 或组的 URL 编码路径
key string 变量的 key;不能超过 255 个字符;只允许使用 A-Za-z0-9_
value string 变量的 value
description string 变量的描述;不能超过 255 个字符。默认值:null
environment_scope string 变量的环境范围。仅限 Premium 和 Ultimate 版本。
masked boolean 变量是否为掩码变量。
masked_and_hidden boolean 变量是否同时为掩码和隐藏变量。默认值:false
protected boolean 变量是否受保护。
raw boolean 变量是否被视为原始字符串。默认值:false。当为 true 时,值中的变量不会被展开
variable_type string 变量的类型。可用类型为:env_var(默认)和 file
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/1/variables" \
  --form "key=NEW_VARIABLE" \
  --form "value=new value"
{
    "key": "NEW_VARIABLE",
    "value": "new value",
    "variable_type": "env_var",
    "protected": false,
    "masked": false,
    "hidden": false,
    "raw": false,
    "environment_scope": "*",
    "description": null
}

更新变量

更新组的变量。如果存在多个具有相同键的变量, 请使用 filter 来选择正确的 environment_scope

PUT /groups/:id/variables/:key
属性 类型 是否必需 描述
id integer/string 组的 ID 或组的 URL 编码路径
key string 变量的 key
value string 变量的 value
description string 变量的描述。默认值:null。在 GitLab 16.2 中引入
environment_scope string 变量的环境范围。仅限 Premium 和 Ultimate 版本。
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>" \
  --url "https://gitlab.example.com/api/v4/groups/1/variables/NEW_VARIABLE" \
  --form "value=updated value"
{
    "key": "NEW_VARIABLE",
    "value": "updated value",
    "variable_type": "env_var",
    "protected": true,
    "masked": true,
    "hidden": false,
    "raw": true,
    "environment_scope": "*",
    "description": null
}

移除变量

移除组的变量。如果存在多个具有相同键的变量, 请使用 filter 来选择正确的 environment_scope

DELETE /groups/: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>" \
  --url "https://gitlab.example.com/api/v4/groups/1/variables/VARIABLE_1"

filter 参数

  • 版本:Premium, Ultimate
  • 产品:GitLab.com, GitLab Self-Managed, GitLab Dedicated

当多个变量具有相同的 key 时,GETPUTDELETE 请求可能会返回:

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>" \
      --url "https://gitlab.example.com/api/v4/groups/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production"
  • PUT 请求:

    curl --request PUT \
      --globoff \
      --header "PRIVATE-TOKEN: <your_access_token>" \
      --url "https://gitlab.example.com/api/v4/groups/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>" \
      --url "https://gitlab.example.com/api/v4/groups/1/variables/SCOPED_VARIABLE_1?filter[environment_scope]=production"