从.gitlab-ci.yml中的JSON提取徽章ID [英] Extract Badge ID from JSON in .gitlab-ci.yml

查看:212
本文介绍了从.gitlab-ci.yml中的JSON提取徽章ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gitlab上有一个 exampleproject ,我想获取上一个的ID通过脚本在.gitlab-ci.yml中添加徽章.我作为json 获得了所有徽章的概述.有没有办法获取最后一个元素的"id"?

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?

此刻,我正在通过PYLINT_BADGE_ID. > json (针对每个项目).在这种情况下,它是37777.如何通过命令行自动执行此操作?

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?

我正在尝试解决以下问题: gitlab中的Pylint徽章.但是他们使用gitlab页面,anybadge,工件和自述文件来显示徽章(这不在标准徽章区域中).以下方式感觉更苗条:

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:

这是我正在使用的.gitlab-ci.yml使用

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

推荐答案

经过几小时的正则表达式转义后,我将其添加到了.gitlab-ci.yml中:

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

此解决方案取决于正则表达式中元素的顺序,以便在json中查找-blue.svg,该顺序必须在徽章ID之前.

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.

这篇关于从.gitlab-ci.yml中的JSON提取徽章ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆