Search code examples
gitlabgitlab-cigitlab-api

List project variables API doesn't show all GitLab project (repository) variables


I'm trying to list all the variables defined in my GitLab repository (project) using the following API: https://gitlab.com/api/v4/projects/[PROJECT-ID]/variables

But, I don't get all the variables. I can see more variables defined here: https://gitlab.com/[GROUP-NAME]/[PROJECT-NAME]/-/settings/ci_cd

I also checked the following API documentation but didn't find anything to figure out the reason: GitLab > Project-level CI/CD variables API > List project variables

Why is this happening? Is there any bug in GitLab?

Note: [PROJECT-ID], [GROUP-NAME] and [PROJECT-NAME] are placeholders and will be replaced with the actual values.


Solution

  • I have figured it out. It seems like the list project variables API doesn't show more than 20 variables by default. I just made a wild guess to try out passing a page variable with the API call and it worked. See this: https://gitlab.com/api/v4/projects/[PROJECT-ID]/variables?page=2

    On the second page, the remaining (missing) variables appeared which means that they do exist and are accessible.

    You can also pass per_page variable with the API call to set how many variables to show per page. See this: https://gitlab.com/api/v4/projects/[PROJECT-ID]/variables? page=1&per_page=100

    The details regarding these pagination variables page and per_page are available in the GitLab's official documentation about REST API here.

    Note: [PROJECT-ID] is a placeholder and will be replaced with the actual value.