Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev:git:gitlab:api:examples [2022/05/18 06:20] mbdev:git:gitlab:api:examples [2022/05/18 08:45] (current) mb
Line 1: Line 1:
 ====== Using Gitlab API ====== ====== 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. \\   +With the given gitlab api many things are possible, for example the exploration of projects and groups as well as triggering pipelines. \\  
-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!**+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 ==== ==== Exploring groups via API ====
 For a better overview and prettier output Postman is used. \\ 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. \\ 
 +<Code: shell linenums:0 | cURL example to access project root> 
 +curl --location --request GET 'https://gitsrv.schnuerle.com:8443/api/v4/projects/82/?ref=main&access_token=YourPipelineToken' 
 +</Code> 
 +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: \\ 
 +<Code: shell linenums:0 | cURL example to access project root> 
 +curl --location --request GET 'https://gitsrv.schnuerle.com:8443/api/v4/projects/82/repository/files/.gitlab-ci.yml/raw?ref=main&access_token=YourPipelineToken' 
 +</Code> 
 +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. \\ 
 +{{ :dev:git:gitlab:api:api_project1.png?direct&600 |}} 
 +<wrap hi>highlighted</wrap> \\ 
 +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 ==== ==== Trigger a pipeline via API ====
 First go to project settings -> "CI/CD" -> "Pipeline triggers" 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. \\ As you can see after creating this token, cURL examples  +After enabling pipeline triggers for a project the given token is used as variable in the trigger request. Unlike previous used api calls, a <wrap hi>**post**</wrap> request is used. \\ 
-\\ {{:dev:git:gitlab:api:api_pipetrigger1.png?direct&600|}} \\ + 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.  +\\ {{ :dev:git:gitlab:api:api_pipetrigger3.png?direct&600 |}} \\ 
-{{:dev:git:gitlab:api:api_pipetrigger2.png?direct&600|}}+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.  
 +{{ :dev:git:gitlab:api:api_pipetrigger2.png?direct&600 |}}