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

核能气质少年 提交于 2020-01-05 04:35:07

问题


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:


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.



来源:https://stackoverflow.com/questions/56212130/extract-badge-id-from-json-in-gitlab-ci-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!