Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab - How to add badge based on jobs pipeline

Tags:

gitlab

badge

My goal is to show badges (ex : enter image description here) based on pipeline results.

I have a private gitlab ce omnibus instance with the following .gitlab-ci.yml :

image: python:3.6

stages:
  - lint
  - test

before_script:
  - python -V
  - pip install pipenv
  - pipenv install --dev

lint:
  stage: lint
  script:
  - pipenv run pylint --output-format=text --load-plugins pylint_django project/ | tee pylint.txt
  - score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
  - echo "Pylint score was $score"
  - ls
  - pwd
  - pipenv run anybadge --value=$score --file=pylint.svg pylint
  artifacts:
    paths:
      - pylint.svg

test:
  stage: test
  script:
  - pipenv run python manage.py test

So I thought that I would store the image in the artifacts of the lint job and display it via the badge feature.

But I encounter the following issue : when I browse https://example.com/[group]/[project]/-/jobs/[ID]/artifacts/file/pylint.svg, instead of seeing the badge I have the following message :

The image could not be displayed because it is stored as a job artifact. You can download it instead.

And anyways, I feel like this is the wrong way, because even if I could get the image, there don't seems to be a way to get the image from the last job since gitlab URL for badges images only supports %{project_path}, %{project_id}, %{default_branch}, %{commit_sha}

So how one would add badge to a gitlab project based on a svg generated from results in a gitlab pipeline ?

My guess is that I could push to a .badge folder but that doesn't sound like a clean solution.

like image 726
L. Faros Avatar asked Sep 07 '18 18:09

L. Faros


People also ask

How do I create a custom badge in Gitlab?

Go to project Settings > General> Badges and add URL link and image URL click on Add Badge and it will be available on your project page.

What badges are available in Gitlab?

What are badges? Badges are those small images that shows information about your git project. By default, Gitlab supports only two types of badges: Code Coverage and Pipeline Status.

How do I add a badge to a GitLab CI pipeline?

A common project badge presents the GitLab CI pipeline status. To add this badge to a project: On the top bar, select Menu > Projects and find your project. On the left sidebar, select Settings > General .

How do I configure a pipeline in GitLab?

You can also configure specific aspects of your pipelines through the GitLab UI. For example: Pipeline settings for each project. Pipeline schedules . Custom CI/CD variables . When a runner picks a pipeline job, GitLab provides that job’s metadata.

What is special refs/pipelines/<ID> in GitLab?

GitLab generates the special ref refs/pipelines/<id> during a running pipeline job. This ref can be created even after the associated branch or tag has been deleted. It’s therefore useful in some features such as automatically stopping an environment, and merge trains that might run pipelines after branch deletion.

Where can I find additional pipeline analytics and report badges?

Pipeline analytics are available on the CI/CD Analytics page . Pipeline status and test coverage report badges are available and configurable for each project. For information on adding pipeline badges to projects, see Pipeline badges . Perform basic functions. For more information, see Pipelines API . Maintain pipeline schedules.


1 Answers

You can indeed get the artifact(s) for the latest job (see documentation here), but the trick is that you need to use a slightly different URL:

https://example.com/[group]/[project]/-/jobs/artifacts/[ref]/raw/pylint.svg?job=lint

where [ref] is the reference to your branch/commit/tag.

Speaking of badge placeholders available in Gitlab, you can potentially put %{default_branch} or %{commit_sha} into [ref]. This won't allow you to get the correct badge for every branch, but at least your default branch will get one.

Please also note that ?job=lint query parameter is required, without it the URL won't work.

like image 186
Dmitry Deryabin Avatar answered Oct 13 '22 19:10

Dmitry Deryabin