How to create repository badges in gitlab?
Asked Answered
I

4

55

How do I create badges for my project in gitlab?

I know there is a webpage for it here https://docs.gitlab.com/ee/user/project/badges.html

but I have no idea what they are saying.

suppose my project is here https://gitlab.com/username/userproject

what should be the link and the badge image link?

Infuse answered 30/5, 2018 at 13:1 Comment(1)
Please, check the comments on docs.gitlab link . There're people also asking for examples, here some links: - gitlab.com/gitlab-org/gitlab-ce/issues/46188 - gitlab.com/gitlab-org/gitlab-ce/issues/41174Goraud
E
46

There are two default badges: Pipeline and Coverage. The example URLs would be:

https://gitlab.com/username/userproject/badges/master/pipeline.svg
https://gitlab.com/username/userproject/badges/master/coverage.svg
  • For pipeline you have to set up a buildserver pipeline (.gitlab-ci.yml).
  • For coverage you have to enter a regex parsing expression in the settings for CI.

But you can also set up custom badges. But you have to create them on your own. For public repos you can use an service like Shields.io or private repos you can use AnyBadge (needs an python interpreter in build chain. Here is an example how to set up a custom badge: Adding Custom badges to Gitlab

When you look at bigger repositories (for example this), you can have a look at the path of the images and see where they are created.

Ermine answered 20/8, 2019 at 11:59 Comment(0)
H
31

Usually you can get the url for the badges from the respective services.

For example the url of the pipeline batch for your repo would be:

https://gitlab.com/username/userproject/badges/master/pipeline.svg)


For adding it to the README you would use following markdown code:

[![pipeline status](https://gitlab.com/user/userproject/badges/master/pipeline.svg)](https://gitlab.com/user/userproject/commits/master)

Note: This badge is the pipeline badge for the master branch. For a pipeline badge of a different branch you'd simply replace master with the respective branch name.

File from the screenshot: https://gitlab.com/gitlab-org/gitlab-ee/blob/master/README.md

Henna answered 6/6, 2018 at 8:38 Comment(2)
Do you know where to find a list of available badges?Kylander
@PeterDavis a good page with a list of badges is shields.ioRelax
G
2

If you don't want to hard code the path, you can construct it using gitlab variables. e.g.

For pipeline - https://gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg

For Code Coverage - https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg

For Release information - https://gitlab.com/%{project_path}/-/badges/release.svg

Galatea answered 10/2, 2023 at 10:13 Comment(0)
B
0

Note: GitLab 17.1 (June 2024) comes with:

New placeholders for group and project badges

You can now create badge links and image URLs using four new placeholders:

  • %{project_namespace} - referencing the full path of a project namespace
  • %{group_name} - referencing the group name
  • %{gitlab_server} - referencing the group’s or project’s server name
  • %{gitlab_pages_domain} - referencing the group’s or project’s domain name

Thank you @TamsilAmani for this community contribution!

See Documentation and Issue.

For instance, in your README.md, you could embed the badge with dynamic linking:

[![coverage report](https://%{gitlab_server}/%{project_namespace}/%{project_path}/badges/%{default_branch}/coverage.svg)](http://%{project_namespace}.pages.%{gitlab_pages_domain}/%{project_path}/coverage/)

But, warning, the documentation does mention:

Placeholders allow badges to expose otherwise-private information, such as the default branch or commit SHA when the project is configured to have a private repository.

This behavior is intentional, as badges are intended to be used publicly.

Avoid using these placeholders if the information is sensitive.

Bimanous answered 25/6 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.