【问题标题】:Gitlab - How to add badge based on jobs pipelineGitlab - 如何根据作业管道添加徽章
【发布时间】:2019-02-13 03:12:45
【问题描述】:

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

我有一个私有 gitlab ce 综合实例,带有以下 .gitlab-ci.yml :

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.]*\)\/.*/\1/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 仅支持 @ 987654326@

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

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

【问题讨论】:

    标签: gitlab badge


    【解决方案1】:

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

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

    其中[ref] 是对您的分支/提交/标签的引用。

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

    还请注意?job=lint 查询参数是必需的,没有它,URL 将不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      相关资源
      最近更新 更多