使用 Gitlab API 在项目上创建变量

Use Gitlab API to create variables on project

我正在尝试使用 cURL 命令为 GitLab 上的项目创建 GitLab 变量。

我正在关注 API 和相关段落 here
查看文档上的示例,我找不到任何证据表明 Gitlab 如何知道我试图将变量添加到哪个 repo。

我尝试添加一个私有令牌并运行命令,但结果是 403。

有人可以解释一下如何使用 GitLab API 通过 cURL 命令将变量添加到 repo 吗?

Looking at the example on the docs, I cannot find any evidence on how Gitlab knows which repo I am trying to add the variable to

但是...“实例级”变量背后的全部思想是“全局”的,不依赖于特定项目。

参见issue 14108

Global (instance-level) variables for CI builds

Less of an issue or more or a feature/question perhaps ... It would nice to support global variables in the CI builds; classic use-case being deployment keys / docker registry credentials.

Problem to solve

Without support for global variables users must manually enter the same credentials repeatedly for dozens of projects when migrating to GitLab for details like:

  • docker private registry credentials
  • kubernetes credentials)
  • deployment keys

Proposal

Implement support for global (instance-level) variables for CI builds by:

  • Re-use the refactor/re-design of CI variables in project/group settings
  • Place under CI/CD section in the admin settings

更适合您的情况的 API 是:

Create variable (project

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"