Skip to content

Default location for CI-created custom-badges

Problem to solve

Using badges in a GitLab project is a good way to have the most important pipeline results always visible on the project page.
But currently GitLab is missing a default location to store custom-badges (for example auto-generated by CI-jobs [AnyBadge]).

One possible (but limited) location are currently the GitLab Pages [StackOverflow], but ATTENTION. SVG files of badges MUST BE part of the artifact 'pages', but ...

Hence, all badges need to be accumulated somewhere else.

  • A possible solution is to use a separate branch and push the badge files from the CI jobs there. A separate job can then clone that branch and copy the files to its artifacts 'pages'. This is a complicated and error-some solution, which requires to use PRIVATE_TOKENS for enabling the runner to push to an extra branch.
  • Alternative one can accumulate the badges at Dropbox (https://gitlab.com/kubouch/ci-test/tree/badges).

But that way the dependency to GitLab pages is not solved, which can be a limit.
With the future access support for GitLab Pages (https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18589) they will not be a proper place for badges anymore.

Simple workarounds using single artifacts 'pages'

  • Work-around No.1 using GitLab Pages
    • Generate the badge
      Use a badge generator like this one in each CI-job: https://github.com/jongracecox/anybadge or https://shields.io

    • Register badge as pipeline artifact
      Register each of the generated badge files as artifacts in the CI job by including this in each job in the .gitlab-ci.yml:

      tests:
        script: ...
          - <generate badge-tests.svg>
          - mv badge-tests.svg public/badge-tests_$CI_COMMIT_REF_NAME.svg
        artifacts:
          name: pages
          paths:
            - public/badge-tests_$CI_COMMIT_REF_NAME.svg
    • Publish badge to GitLab Pages
      Add 'pages' job and enable GitLab Pages via Settings->Pages

      pages:
        stage: deploy
        artifacts:
          name: pages
          paths:
          - public
        only:
        - master
    Once this job runs, all the files in public will be available via the GitLab pages web server.

Why is this "just" a work-around

What does success look like, and how can we measure that?

  1. Have a special storage place to copy files for world-wide access (e.g. always_public/)
  2. Have a keyword for a script, which runs independent of a job failed or not (even if the precious job failed)
tests:
  always_script:
    - <generate badge-tests_$CI_COMMIT_REF_NAME.svg>
    - cp badge-tests_$CI_COMMIT_REF_NAME.svg always_public/

Links / references

AnyBadge - https://pypi.org/project/anybadge/
StackOverflow - https://stackoverflow.com/questions/45756371/gitlab-ci-using-badges-for-each-job

Badge generation with GitLab API (JSON) and Shields.io

https://img.shields.io/badge/dynamic/json.svg?label=forks&url=https://gitlab.com/api/v4/projects/13083&query=$.forks_count&colorB=brightgreen
Edited by Jens Henrik Göbbert