How To Use Custom GitLab Badges?

In this article, I want to tell you about the GitLab badges feature.

Gitlab badges are a visual way of presenting summary information about your projects. They consist of a small image and a URL that the image points to as a link. We can use badges for example to show information about pipeline status, test coverage, code smells, etc.

The badge can be added to a project by authorized users and can be viewed on the project’s overview page.

Gitlab badges sample

How To Add Badges To a Project?

While in the Gitlab repository, we reach the form below by following the path is General > Settings > Badges

CodeSmells custom badge sample

%{project_path} => xeon-supplier-return-api

%{project_id} => 1192

%{default_branch} =>release

%{commit_sha} => 234234e455d54d5ed45ededd80aaeec85e5

Gitlab’s group badge feature is also available but unfortunately, we cannot use any other value than the 4 parameters it supports. Therefore, we can go to add badges to projects via GitLab Badge API by browsing all the Repos under the group.

Gitlab Badge API

You can use Get/Edit/Add/Remove/List to your project by using the Gitlab token

Types of Shells Supported By GitLab Runner

GitLab Runner implements shell script generators that allow executing builds on different systems. In this article, We preferred to use bash script to call GitLab API to create badges.

Overview

The shell scripts contain commands to execute all steps of the build:

  1. git clone
  2. Restore the build cache
  3. Build commands
  4. Update the build cache
  5. Generate and upload the build artifacts
There is a support list table for GitLab runners

Sh/Bash shells

This is the default shell used on all Unix-based systems. The bash script used in .gitlab-ci.yml is executed by piping the shell script to one of the following commands:

Create Custom Badges Into a Project via Gitlab API

We used a bash script to generate sonar badges for all projects under the group. In doing so, we added our standard sonar badges for all projects using the Gitlab group-id.

One job to run

We started our start() sh script and performed a loop operation within the statically defined GroupIds variable.

Then we create my custom badge URLs for each project with the generate_custom_badge() method. While doing this, we add lines with line() to make it readable on the output screen.

Thanks for reading.

References and Sources:

GitLab Docs :https://docs.gitlab.com/ee/README.html

About GitLab: https://about.gitlab.com/company/

Gitlab Badges API: https://docs.gitlab.com/ee/api/project_badges.html

Types of shells supported by GitLab Runner: https://docs.gitlab.com/runner/shells/

--

--

--

Software Development Engineer @Trendyol

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Implementing a Repository, Part I: Contract considerations

Improved Vim setup in Visual Studio Code

My First Ruby Gem Part 2: A Look Under the Hood

CS50-第三課tutorial notes

Personal Knowledge Management How to Choose the Right System for You.

Routing in Neo4j With Spring Boot

Build a web-scraped time-series application with AWS CDK in TypeScript — Part 2

What is CDN ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mennan Sevim

Mennan Sevim

Software Development Engineer @Trendyol

More from Medium

Working with different Github account on the same PC

How to upgrade Zabbix Network Monitoring tools 5.4 from version 5.2/5.0

How to build a simple Xbox Game Bar Widget

Laravel’s Eloquent ORM (Object Relational Mapping)