Using Gitlab API

With the given gitlab api many things are possible, for example the exploration of projects and groups as well as triggering pipelines.
Unlike other web-sources the api refers to project- and group-IDs. These IDs are shown in the gitlab web-UI.
To have project access with api a Token is necessary. To create a Token, go to project (or group) “Settings” → “Access Tokens”. For full access select “api”.
Tokens have an expiration date, but with leaving it blank, no expiration date is set.
Tokens can not be seen after creation so save it locally!

Exploring groups via API

For a better overview and prettier output Postman is used.

Exploring projects via API

To start exploring a project, create a access token and get the project ID. In this example project “webdev” of the group “tests” is used.

cURL example to access project root



Authors:
  • Martin Büchner
curl --location --request GET 'https://gitsrv.schnuerle.com:8443/api/v4/projects/82/?ref=main&access_token=YourPipelineToken'

Obviously a cURL command isn't perfect for exploration, but this URL is also accessable via browser abd e.g. Firefox can handle the json formatted response.
Nonetheless there are many possibilities with cURL when using Pipelines to process the output. Requests can also be sent and processed with other program languages like python or perl.
With the api files are also accessable:

cURL example to access project root



Authors:
  • Martin Büchner
curl --location --request GET 'https://gitsrv.schnuerle.com:8443/api/v4/projects/82/repository/files/.gitlab-ci.yml/raw?ref=main&access_token=YourPipelineToken'

With this snippet we can get the file “.gitlab-ci.yml”. With a URL like this, files from private projects can be included in e.g. the project gitlab-ci file.
The project tags can be shown as well, see example below.
highlighted
With this api call, all project tags as well as more information about the tag, like creation date and user.

Trigger a pipeline via API

First go to project settings → “CI/CD” → “Pipeline triggers” After enabling pipeline triggers for a project the given token is used as variable in the trigger request. Unlike previous used api calls, a post request is used.
As you can see after creating this token, cURL examples

With Postman we also have a better overview about the sent variables. These can also be seen in the created pipeline, as seen below.
It is also possible to overwrite Gitlab CI variables, these are only set in this running job.



Authors:
  • Martin Büchner