我在哪里可以找到 GitLab API 的项目 ID?

我在他们的服务器上用 GitLab。我想通过 API 下载我最新构建的工件(通过 GitLab CI 构建) ,如下所示:

curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.com/api/v3/projects/1/builds/8/artifacts"

我在哪里可以找到这个项目 ID?或者这种使用 API 的方式并不适用于托管的 GitLab 项目?

108645 次浏览

You can query for your owned projects:

curl -XGET --header "PRIVATE-TOKEN: XXXX" "https://gitlab.com/api/v4/projects?owned=true"

You will receive JSON with each owned project:

[
{
"id":48,
"description":"",
"default_branch":"master",
"tag_list":[
...

You are also able to get the project ID from the triggers configuration in your project which already has some sample code with your ID.

From the Triggers page:

curl -X POST \
-F token=TOKEN \
-F ref=REF_NAME \
https://<GitLab Installation>/api/v3/projects/<ProjectID>/trigger/builds

Just for the record, if someone else has the need to download artifacts from gitlab.com created via gitlab-ci

  1. Create a private token within your browser
  2. Get the project id via curl -XGET --header "PRIVATE-TOKEN: YOUR_AD_HERE?" "https://gitlab.com/api/v3/projects/owned"
  3. Download the last artifact from your master branch created via a gitlab-ci step called release curl -XGET --header "PRIVATE-TOKEN: YOUR_AD_HERE?" -o myapp.jar "https://gitlab.com/api/v3/projects/4711/builds/artifacts/master/download?job=release"

I am very impressed about the beauty of gitlab.

On the Edit Project page there is a Project ID field in the top right corner.

(You can also see the ID on the CI/CD pipelines page, in the exameple code of the Triggers section.)

In older versions, you can see it on the Triggers page, in the URLs of the example code.

I just found out an even easier way to get the project id: just see the HTML content of the gitlab page hosting your project. There is an input with a field called project_id, e.g:

<input type="hidden" name="project_id" id="project_id" value="335" />

As mentioned here, all the project scoped APIs expect either an ID or the project path (URL encoded). So just use https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-foss directly when you want to interact with a project.

Enter the project.

On the Left Hand menu click Settings -> General -> Expand General Settings

It has a label Project ID and is next to the project name.

This is on version GitLab 10.2

Provide the solution that actually solve the problem the api of getting the project id for specific gitlab project

curl -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" http://<YOUR-GITLAB-SERVER>/api/v3/projects/<YOUR-NAMESPACE>%2F<YOUR-PROJECT-NAME> | python -mjson.tool

Or maybe you just want the project id:

curl -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" http://<YOUR-GITLAB-SERVER>/api/v3/projects/<YOUR-NAMESPACE>%2F<YOUR-PROJECT-NAME> | python -c 'import sys, json; print(json.load(sys.stdin)["id"])'

Note that the repo url(namespace/repo name) is encoded.

The latest version of GitLab 11.4 at the time of this writing now puts the Project ID at the top of the frontpage of your repository.

Screenshot:

GitLab Project Front Page

As of Gitlab API v4, the following API returns all projects that you own:

curl --header 'PRIVATE-TOKEN: <your_token>' 'https://gitlab.com/api/v4/projects?owned=true'

The response contains project id. Gitlab access tokens can be created from this page- https://gitlab.com/profile/personal_access_tokens

If you know your project name, you can get the project id by using the following API:

curl --header "Private-Token: <your_token>" -X GET https://gitlab.com/api/v4/projects?search=<exact_project_name>

This will return a JSON that includes the id:

[
{
"id":<project id>, ...
}
]

If your project name is unique, it is handy to follow the answer by shunya, search by name, refer API doc.

If you have stronger access token and the Gitlab contains a few same name projects within different groups, then search within group is more convenient. API doc here. e.g.

curl --header "PRIVATE-TOKEN: <token>" -X GET https://gitlab.com/api/v4/groups/<group_id>/search?scope=projects&search=<project_name>

The group ID can be found from the Settings page under the group domain.

And to fetch the project id from the output, you can do:

curl --header "PRIVATE-TOKEN: <token>" -X GET https://gitlab.com/api/v4/groups/<group_id>/search?scope=projects&search=<project_name> | jq '[0].id'

To get id from all projects, use:

curl --header 'PRIVATE-TOKEN: XXXXXXXXXXXXXXXXXXXXXXX' 'https://gitlab.com/api/v4/projects?owned=true' > curloutput
grep -oPz 'name\":\".*?\"|{\"id\":[0-9]+' curloutput | sed 's/{\"/\n/g' | sed 's/name//g' |sed 's/id\"://g' |sed 's/\"//g'  | sort -u -n

No answer suits generic needs, the most similar is intended only for the gitlab site, not specific sites. This can be used to find the ID of the project streamer in the Gitlab server my-server.com, for example:

$ curl --silent --header 'Authorization: Bearer MY-TOKEN-XXXX' \
'https://my-server.com/api/v4/projects?per_page=100&simple=true'| \
jq -rc '.[]|select(.name|ascii_downcase|startswith("streamer"))'| \
jq .id


168

Remark that

  • this gives only the first 100 projects, if you have more, you should request the pages that follow (&page=2, 3, ...) or run a different API (e.g. groups/:id/projects).
  • jq is quite flexible. Here we're just filtering a project, you can do multiple things with it.

There appears to be no way to retrieve only the Project ID using the gitlab api. Instead, retrieve all the owner's projects and loop through them until you find the matching project, then return the ID. I wrote a script to get the project ID:

#!/bin/bash


projectName="$1"
namespace="$2"
default=$(sudo cat .namespace)
namespace="${namespace:-$default}"
json=$(curl --header "PRIVATE-TOKEN: $(sudo cat .token)" -X GET
'https://gitlab.com/api/v4/projects?owned=true' 2>/dev/null)
id=0
idMatch=0
pathWithNamespaceMatch=0
rowToMatch="\"$(echo "$namespace/$projectName" | tr '[:upper:]' '[:lower:]')\","
for row in $(echo "${json}" | jq -r '.'); do
[[ $idMatch -eq 1 ]] && { idMatch=0; id=${row::-1}; }
[[ $pathWithNamespaceMatch -eq 1 ]] && { pathWithNamespaceMatch=0; [[ "$row" == "$rowToMatch" ]] && { echo "$id"; return 0; } }
[[ ${row} == "\"path_with_namespace\":" ]] && pathWithNamespaceMatch=1
[[ ${row} == "\"id\":" ]] && idMatch=1
done
echo 'Error! Could not retrieve projectID.'
return 1

It expects the default namespace to be stored in a file .namespace and the private token to be stored in a file .token. For increased security, its best to run chmod 000 .token; chmod 000 .namespace; chown root .namespace; chown root .token

Not Specific to question, but somehow reached here, might help others I used chrome to get a project ID

  1. Go to the desired project example gitlab.com/username/project1
  2. Inspect network tab
  3. see the first garphql request in network tab enter image
description here

You can view it under the repository name

enter image description here

You can search for the project path

curl -s  'https://gitlab.com/api/v4/projects?search=my/path/to/my/project&search_namespaces=true' --header "PRIVATE-TOKEN: $GITLAB_TOKEN" |python -mjson.tool |grep \"id\"

https://docs.gitlab.com/ee/api/projects.html

Which will only match your project and will not find other unnecessary projects

My favorite method is to pull from the CI/CD pipeline so on build it dynamically assigns the project id.

Simply assign a variable in your code to = CI_PROJECT_ID