Search code examples
gitlabgitlab-api

Use Gitlab API to create variables on project


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?


Solution

  • 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"