2

I am trying to create a GitLab variable for a project on GitLab using a cURL command.

I am following the API and the relevant paragraph here.
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.

I tried adding a private token and runnnig the command and I ended up with a 403.

Could someone please explain how to use the GitLab API to add variables to a repo through a cURL command?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
berlin
  • 506
  • 4
  • 14

1 Answers1

4

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

But... the all idea behind "instance-level" variables is to be "global", not tied to a specific project.

See 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

A better API for your case would be:

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"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • this worked for me perfectly, thanks for the answer! I was looking in the wrong place. – berlin Jul 15 '21 at 07:14
  • I have one more follow up on this - would it be possible to add multiple variables using the Gitlab API? or do I need to make multiple POST requests for this? – berlin Jul 15 '21 at 08:12
  • @berlin From what I see, you would need multiple POST. – VonC Jul 15 '21 at 08:17