I've got an exampleproject at gitlab where I would like to get the ID of the last badge in the .gitlab-ci.yml
via script. I get the overview of all badges as a json. Is there a way to get the "id" of the last element?
At the moment I'm setting a custom CI variable PYLINT_BADGE_ID
by hand from the json for each project. In this case it is 37777. How to automate this by commandline?
Details:
I'm trying to solve this question: Pylint badge in gitlab. But they use gitlab pages, anybadge, artifacts and the readme to display badges (which is not in the standard badge area). The following way feels more slim:
This is the .gitlab-ci.yml I'm using
lint:
script:
- python -m pip install setuptools
- python -m pip install pylint pylint-exit
- pylint src/*.py | tee pylint.txt || pylint-exit $?
- score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
- echo "Pylint score was $score"
# To check your badge ID go to https://gitlab.com/api/v4/projects/43126475/badges
# and insert your $CI_PROJECT_ID. Must be a quite high number!
# Would be great to automate this!
- badge_url=https://img.shields.io/badge/lint%20score-$score-blue.svg
- >-
curl https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges/$PYLINT_BADGE_ID
-X PUT
-H "PRIVATE-TOKEN: $API_TOKEN"
-H "Content-Type: application/json"
-d '{"image_url": "'"$badge_url"'"}'
artifacts:
paths:
- pylint.txt
After some hours of regex escaping I added this to my
.gitlab-ci.yml
:So the whole stage looks like this:
This solution depends on the order of the elements in the regex looks for
-blue.svg
in the json, which needs to be before the badge id.