How To Use Custom GitLab Badges?

Mennan Sevim
3 min readDec 10, 2021

--

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/

--

--