Generate badges for Gitlab Projects in Public and Private Repositories
Project description
Badges Gitlab
This project was created to generate badges for Gitlab in CI jobs, mainly for private repositories where other common methods are not available (direct API Calls, shields.io, etc...).
By default, Gitlab supports only two types of badges: pipeline and test coverage.
These badges are better detailed at: https://docs.gitlab.com/ee/user/project/badges.html.
Requirements
For using this package, it will also install these packages and their dependencies:
- Python Gitlab API
- Anybadge
- Iso8601
Usage
Common Usage
Install this package from pip.
$ pip install badges-gitlab
$ badges-gitlab -h
usage: badges-gitlab [-h] [-p TEXT] [-t TEXT] [-V]
Generate Gitlab Badges using JSON files and API requests. Program version v0.0.0.
optional arguments:
-h, --help show this help message and exit
-p TEXT, --path TEXT path where json and badges files will be generated/located (default: ./public/badges/)
-t TEXT, --token TEXT specify the private-token in command line (default: ${PRIVATE_TOKEN})
-V, --version returns the package version
This package was intended to be used in CI jobs, but if you to test locally, you must point a folder with the json files in the format used by shields.io endpoint.
{
"schemaVersion": 1,
"label": "hello",
"message": "sweet world",
"color": "orange"
}
Gitlab CI YAML Pipeline Job Example
Below it is an example of a job running at the end of the pipeline in the default branch (main) and using cache for job optimization.
To ensure all possible badges are generated, include the personal access token as an environment variable direct into the .gitlab-ci.yml or in the CI/CD Variables configuration.
badges:
image: python:3.9
stage: badges
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
PRIVATE_TOKEN: $ACESS_TOKEN
cache:
key: badges
paths:
- .cache/pip
- venv/
before_script:
- python -V
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
script:
- pip install badges-gitlab
- badges-gitlab -V
- badges-gitlab
artifacts:
when: always
paths:
- public/badges/*.svg
expire_in: 3 months
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: always
allow_failure: true
As the badges are generated only during this job, and if you want to make sure there are available for some time. So, adjust the expiration of the artifacts accordingly.
Schedule Pipelines
Some badges have dynamic data and are generated only during this job, and the data can be outdated. If you don't want to use third party APIs all the time to generate the badges (sometimes these APIs fail), you could use the pipeline schedule function in conjunction with the rules option to run once a day as example.
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
Dockerfile Example
Alternatively, you can optimize even further the job building a docker image for this job and uploading to Gitlab Container Registry.
FROM python:3.9-alpine
MAINTAINER foo@bar.com
RUN pip install badges-gitlab
Using the Badges
This package seeks to use the post jobs availability of the artifacts through links, which are described in Gitlab Documentation.
Gitlab Project Badges
You can insert using the project badges https://docs.gitlab.com/ee/user/project/badges.html#badges.
Examples of a link for the project license in project badges section:
https://gitlab.com/%{project_path}/-/jobs/artifacts/%{default_branch}/raw/public/badges/license_name.svg?job=badges
Readme
Other option is to use in the Readme file, through links. In Gitlab you can leverage the relative links feature.
Example of a link in a markdown Readme.
![License](../-/jobs/artifacts/main/raw/public/badges/license_name.svg?job=badges)
Author
Felipe P. Silva
Motivation
Although it is possible to generate badges with other API's such as shields.io, usually this process is not available in private repositories.
So if you are hosting a public project, this package is not specifically meant for you as you can workaround with other easier implementations.
But, if you are hosting a private project and don't want to expose your project (Gitlab pages) or don't want to risk exposing your credentials (API Requests), maybe this project is for you.
Another reason would be to avoid overloading servers (e.g. shields.io) with unnecessary requests for (re)creating badges.
Design Choices
Some design choices were made to create this package.
- The badges' generation were converted into two stages:
- The first stage uses the Gitlab API (if the private-token turns out to be valid) to generate the json for some badges.
- The second stage gets all the JSON files from the target folder and creates badges using anybadge.
- These two stages have a purpose, if any other CI Pipeline job generates json files with their own data, you can also use these files to create badges.
- The default directory is /public/badges:
- This folder may be used later for Gitlab pages, although this can be modified through parameters
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for badges_gitlab-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecd75bab24a651be06da34f34f2e28f9ccced11367f73d35f88ac336ade46cbe |
|
MD5 | 6ce46162cab5481dab09d50f8987de2a |
|
BLAKE2b-256 | 3bb85de9e5d183e699ec33767c2465bd86eac30a73acf830b70f14ecd2192271 |