Gitlab - 如何根据作业管道添加徽章

Gitlab - How to add badge based on jobs pipeline

我的目标是根据管道结果显示徽章(例如:)。

我有一个带有以下 .gitlab-ci.yml 的私有 gitlab ce omnibus 实例:

image: python:3.6

stages:
  - lint
  - test

before_script:
  - python -V
  - pip install pipenv
  - pipenv install --dev

lint:
  stage: lint
  script:
  - pipenv run pylint --output-format=text --load-plugins pylint_django project/ | tee pylint.txt
  - score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*//p' pylint.txt)
  - echo "Pylint score was $score"
  - ls
  - pwd
  - pipenv run anybadge --value=$score --file=pylint.svg pylint
  artifacts:
    paths:
      - pylint.svg

test:
  stage: test
  script:
  - pipenv run python manage.py test

所以我想我会将图像存储在 lint 作业的工件中并通过徽章功能显示它。

但我遇到以下问题:当我浏览 https://example.com/[group]/[project]/-/jobs/[ID]/artifacts/file/pylint.svg 时,我没有看到徽章,而是看到以下消息:

The image could not be displayed because it is stored as a job artifact. You can download it instead.

无论如何,我觉得这是错误的方式,因为即使我能得到图像,似乎也没有办法从 gitlab 之后的最后一份工作中得到图像 URL徽章图像仅支持 %{project_path}, %{project_id}, %{default_branch}, %{commit_sha}

那么如何根据从 gitlab 管道中的结果生成的 svg 向 gitlab 项目添加徽章?

我的猜测是我可以推送到 .badge 文件夹,但这听起来不像是一个干净的解决方案。

你确实可以获得最新工作的工件(见文档here),但诀窍是你需要使用稍微不同的URL:

https://example.com/[group]/[project]/-/jobs/artifacts/[ref]/raw/pylint.svg?job=lint

其中 [ref] 是对您的 branch/commit/tag 的引用。

说到 Gitlab 中可用的徽章占位符,您可以将 %{default_branch}%{commit_sha} 放入 [ref]。这不会让您获得每个分支的正确徽章,但至少您的默认分支会得到一个。

另请注意,?job=lint 查询参数是必需的,没有它,URL 将无法工作。