Extract Badge ID from JSON in .gitlab-ci.yml

2019-08-08 07:24发布

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

1条回答
贪生不怕死
2楼-- · 2019-08-08 07:53

After some hours of regex escaping I added this to my .gitlab-ci.yml:

  - json_badge_info=$(curl -H "PRIVATE-TOKEN:$API_TOKEN" -X GET https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges)
  - pylint_badge_id=$(expr match "$json_badge_info" '.*https[^"]*-blue\.svg\",\"id\":\([0-9]\+\),')

So the whole stage looks like this:

lint:
  stage: unittest-lint

  script:
  - python -m pip install setuptools 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"

  # get the json with all badge urls via API and regex the id of the badge with 'blue.svg' in it
  - json_badge_info=$(curl -H "PRIVATE-TOKEN:$API_TOKEN" -X GET https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges)
  - pylint_badge_id=$(expr match "$json_badge_info" '.*https[^"]*-blue\.svg\",\"id\":\([0-9]\+\),')
  - echo $pylint_badge_id

  - 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

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.

查看更多
登录 后发表回答