Gitlab CI对每个作业都使用徽章 [英] Gitlab CI using Badges for each job
问题描述
假设我为一个项目配置了多个作业,如下所示:
Let's say I have configured multiple jobs for a project like following:
build_win32:
script: ...
build_ios:
script: ...
unit_tests:
script: ...
server_tests:
script: ...
client_tests:
script: ...
我想要实现的是在README.md下为每个作业配置徽章,以便我可以立即得到关于具体哪部分出错的反馈.
What I want to achieve is to configure badges per each job under README.md so that I can have immediate feedback of specifically which part went wrong.
关于设置徽章,有 Gitlab文档但这有一个限制,那就是它显示了如何仅为build
和coverage status
配置徽章.
There is a Gitlab Documentation on setting badges but this has a limitation that it shows how to configure badges for build
and coverage status
only.
我想知道Gitlab CI中是否有这样的内置功能.我也可以使用3rd party插件.任何帮助将不胜感激.
I'm wondering if there is such a build-in feature in Gitlab CI. I could use 3rd party plugins also. Any helps will be appreciated.
推荐答案
您可以通过在管道步骤中创建徽章,将徽章文件注册为管道工件并将它们发布到GitLab页面上来实现所需的功能.在这里,您可以引用README.md
You can achieve what you need by creating badges in your pipeline steps, registering the badge files as pipeline artifacts, and publishing them to GitLab Pages. From there you can reference the badges in your README.md
在每个CI步骤中,您都需要生成徽章文件,并将其存储在public/<badge-file>.svg
In each of your CI steps you would need to generate badge files, and store them under public/<badge-file>.svg
您可以使用 http://shields.io 生成徽章文件.我已经编写了自己的Python徽章生成器,可以在这里找到: https://github.com/jongracecox/anybadge
You can generate badge files using http://shields.io. I have written my own Python badge generator, which can be found here: https://github.com/jongracecox/anybadge
通过将每个生成的徽章文件包含在.gitlab-ci.yml
中的每个作业中,将其注册为CI作业中的工件:
Register each of the generated badge files as artifacts in the CI job by including this in each job in the .gitlab-ci.yml
:
build_win32:
script: ...
- <generate public/build_win32.svg>
artifacts:
paths:
- public/build_win32.svg
build_ios:
script: ...
- <generate public/build_ios.svg>
artifacts:
paths:
- public/build_ios.svg
unit_tests:
script: ...
- <generate public/unit_tests.svg>
artifacts:
paths:
- public/unit_tests.svg
server_tests:
script: ...
- <generate public/server_tests.svg>
artifacts:
paths:
- public/server_tests.svg
client_tests:
script: ...
- <generate public/client_tests.svg>
artifacts:
paths:
- public/client_tests.svg
3.将徽章发布到GitLab页面上
包括一个新的pages
发布步骤,该步骤部署GitLab页面的公共目录:
3. Publish badge to GitLab Pages
Include a new pages
publish step, which deploys everything in the public directory to GitLab pages:
pages:
stage: deploy
artifacts:
paths:
- public
only:
- master
您不需要修改以上内容.运行此作业后,public
中的所有文件都将通过GitLab页面Web服务器可用.这通常是http://NAMESPACE.GITLABPAGESSERVER/project
You shouldn't need to modify the above. Once this job runs, all the files in public
will be available via the GitLab pages web server. This is often http://NAMESPACE.GITLABPAGESSERVER/project
在此处阅读有关GitLab页面的更多信息: https://docs.gitlab .com/ce/user/project/pages/index.html
Read more about GitLab pages here: https://docs.gitlab.com/ce/user/project/pages/index.html
为项目运行主管道时,徽章文件将发布到GitLab页面,然后可以从项目README.md
中引用.
When the master pipeline runs for the project, the badge files are published to GitLab Pages, and can then be referenced from the project README.md
.
我理解您为什么会问这个问题,但我想解释一下为什么您可能不想这样做.
I understand why you would ask this question, but I would like to explain why you might not want to do that.
GitLab管道在每次推送项目时(在每个分支上)运行.通常,您只想从master
分支(或发行版)分支为您的README文件生成徽章,因此您将
GitLab pipelines run on every push to the project - on every branch. Typically you would only want to generate badges for your README file from the master
branch (or a release) branch, so you would restrict the pages step to only run on master@group/project
.
还应考虑通常将管道配置为在作业错误时停止.这意味着,如果主管道作业失败,则不会生成该管道的徽章,因此对于确定哪个作业失败没有帮助.
Also consider that a pipeline will usually be configured to stop when a job errors. This would mean that if a master pipeline job failed then the badges for that pipeline would not get generated, and would therefore not be helpful in determining which job failed.
获得即时反馈的最佳位置是在管道视图中,您可以通过指向管道的链接将管道成功标志添加到自述文件中,通常类似于https://gitlabserver/namespace/project/badges/branch/build.svg
The best place to get your immediate feedback is in the pipeline view, and you could add the pipeline success badge to your readme with a link to the pipeline, usually something like https://gitlabserver/namespace/project/badges/branch/build.svg
如果您仍然想使用您所描述的方法,那么我的建议是将每个管道阶段设置为
If you still want to use the approach you have described then my suggestion would be to set each pipeline stage to allow failure. This would allow the full pipeline to run, despite failures in any job, and the badges should still get generated, and published to Pages.
这篇关于Gitlab CI对每个作业都使用徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!