4

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?

1 Answer 1

5

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"
Sign up to request clarification or add additional context in comments.

2 Comments

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 From what I see, you would need multiple POST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.