Setup GitLab CLI and how to use that for vast amount of data

Posted By :Harsh Kumar |23rd May 2021

 

 

Building software is a hefty task, but managing software is the real key. 

 

Git is an open-source project, which aims to manage software development projects and their files as they change over time. Git stores this information in a set of a data structure called a repository. 

A git repository is a primary place where developers share, store, test, and collaborates on web projects.

 

GitLab joins the need for better and deeper integrations with development and DevOps toolchains. GitLab provides a scope of tooling for both developers and operation teams.

 

Let’s Suppose a GitLab has more than a thousand projects, more than 500 Users, and more than 200 Groups, and you want to retrieve some details from GitLab. Details such as a list of the project with Name, commit, storage, and a list of Groups and Users. 

 

Getting all this done from GitLab GUI is a nightmare. 

 

You can use GitLab API, and here we will learn how to get these data from GitLab in seconds or minutes depending upon the size of data, using a Python Module of GitLab. This module uses the GitLab API.

 

In this blog will learn how to install and set up this GitLab Command Line (CLI) and see some examples:-

 

 

Step 1. Create an Access Token

We will need to create an access token to access the GitLab using API.

  1. Go to your Avatar at the top-right and click on Edit Profile.
  2. Next, select Access Token.
  3. Create a Token by checking the API option and add an expiry date.
  4. Copy the token somewhere for future use.

 

 

Step 2. Install GitLab CLI

You will need Python 3.6+ version for this.

 

         1.  Install using pip

$ sudo pip install --upgrade python-gitlab

 

         2. Clone the gitlab CLI code and install it.

$ git clone https://github.com/python-gitlab/python-gitlab
$ cd python-gitlab
$ sudo python setup.py install

 

         3. Check the installation by running the following command:

$ gitlab --help

 

 

Step 3. Configuring the GitLab CLI

We will need to configure the GitLab CLI by using an Access Token created in Step 1.

  • Create a configuration by file at /etc/ path.

    $ sudo nano /etc/python-gitlab.cfg

  • Paste the following code in the python-gitlab.cfg file

 

[global]

default = somewhere

ssl_verify = true

timeout = 60

 

[somewhere]

url = https://www.gitlab.com

private_token = BQdERfKksnf4xViZjNyz

api_version = 4

per_page = 10

 

  • Replace the GitLab URL if you are using GitLab self-managed instance.

  • Replace the private_token with your Access Token.

 

 

Step 4. Test the Configuration

  • Check the Gitlab CLI configuration by running the following command:

$ gitlab project list

 

This command will give you the data such as the id and path of some projects. (10 projects as mentioned in the config file)

 

Output:

 

id: 26857216

path: road_damaged_detection_model

 

id: 26857212

path: test

 

id: 26857192

path: robber-run

 

id: 26857171

path: codes.assign

 

id: 26857170

path: fad6-role

 

id: 26857150

path: TestAssessment

 

id: 26857131

path: exam_17t1021123

 

id: 26857127

path: hakaton

 

id: 26857126

path: hello

 

id: 26857099

path: dump_device_realme_rmx2076

 

 

Some useful commands:

 

$ gitlab project list

 

If you are using GitLab.com then the below command will show you any project which is publicly accessible.

 

$ gitlab project list --owned true

 

This command will show your projects present in your account.

 

$ gitlab project list --owned true --all

 

This will provide you with a list of all your projects.

 

Similarly, there are lots of commands to get the names of users, groups, etc. You can create and update the data of projects as well. You can check other options by running --help commands.

 

$ gitlab --help

 

Conclusion:

 

We have learned how to install and configure a Python Module of Gitlab CLI. This CLI can create, update and access the data from GitLab.

 

 


About Author

Harsh Kumar

He is an enthusiastic employee who has worked on cloud platforms like Azure and AWS and some DevOps tools.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Chat With Us