如何在GitLab CI中显示来自另一个存储库的测试 [英] How to show tests from another repository in GitLab CI

查看:127
本文介绍了如何在GitLab CI中显示来自另一个存储库的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的项目带有单元测试,在GitLab中配置CI系统后,当我发出合并请求时,它们很好地显示为绿色或红色圆圈。

So I have my project with it's unit tests and they show nicely as green or red circles when I do a merge request after I've configured the CI system in GitLab.

但是现在我还有一些集成测试,这些测试位于单独的存储库中(为什么要问?因为我有多个需要一起测试的微服务,并且每个微服务都有自己的存储库)。

But now I also have some integration tests, which reside in a separate repository (why you ask? because I have multiple micro-services that need to be tested together, and each has it's own repository).

当我在此集成测试的存储库上执行合并请求时,它们会很好地显示,但是我需要那些测试才能显示在其他存储库的合并请求中。

When I do a merge request on this integration tests' repository, they show nicely, but what I need it for those tests to show on the merge requests of the other repositories.

我确实设法通过GitLab CI给我的URL /命令从微服务的存储库中触发了它们,如下所示: curl -X POST- F token = ... -F ref = master https://gitlab.com/api/v4/projects/.../trigger/pipeline

I did manage to trigger them from the micro-services' repositories with a URL/command that GitLab CI gives me, something like this: curl -X POST -F token=... -F ref=master https://gitlab.com/api/v4/projects/.../trigger/pipeline

但是在微服务的存储库中,它始终显示为绿色圆圈,表示它已成功启动了集成测试,但我不知道ow如何显示测试结果(或至少显示它们是否破裂)。

But in the micro-services' repositories, it always shows as a green circle, meaning it successfully started the integration tests, but I don't know how to display the tests results (or at least if they broke or not).

如果有的话,谁能指出我正确的文档,或者只是解释一下

Could anyone point me to the right documentation, if there is one, or just explain to me how to do it and if it's even possible?

我能想到的最佳解决方案是将集成测试创建为库,然后导入并在所有其他项目上都使用该库,但是我绝对希望避免这种情况,因为这将迫使我用与项目相同的编程语言编写集成测试(假设它们相同),或者进行一些破解来运行它

The best solution I could think of was to create my integration tests as a library, then I'd import and use that library on all the other projects, but I'd definitely rather avoid this, since it would force me to write the integration tests in the same programming language as the projects (assuming they are the same) or make some hack to run it on the other languages.

谢谢。

推荐答案

可以做的就是使用Python / Bash脚本扩展当前的操作;

What you could do is expand on what you're currently doing using a Python/Bash script;

在主项目中,使用上述脚本:

From the main project, using said script:


  1. 触发微服务管道(并捕获管道ID)

  2. 轮询状态,使用捕获的ID(可以正在运行待处理失败取消跳过

  3. 如果失败,则引发异常/错误...

  1. Trigger the micro-service pipeline (and capture the pipeline ID)
  2. Poll the status of the pipeline, using the captured ID (which can be running, pending, failed, canceled or skipped)
  3. Raise an exception / error if it has failed...

这应该可以完成您的工作要求,但是这意味着您将使用跑步者只是向GitLab实例不断发送curl请求(并且该跑步者无法接任其他工作,具体取决于您如何设置跑步者的限制和并发设置) 。

This should do what you require, but would mean that you would be using a runner just to be constantly sending a curl request to the GitLab instance (and that this runner can't pick up another job, depending on how you have setup the runner's limit and concurrent settings).

示例 run_pipeline.py

import gitlab
import time, timeit
import sys

from datetime import timedelta

gl = gitlab.Gitlab("https://your_gitlab_instance.com/",
                    private_token="you_private_token")

project = gl.projects.get('your_project')
create_pipeline = project.pipelines.create({'ref': 'master'})

# Set default
status = "pending"
start_time = timeit.default_timer()

while (status == "running" or status == "pending"):
    pipeline = project.pipelines.get(create_pipeline.id)

    status = pipeline.status

    elapsed_time = timeit.default_timer() - start_time
    formated_time = str(timedelta(seconds=elapsed_time))
    sys.stderr.write("Still running pipeline... ({})\n".format(formated_time))

    if status == "success":
        sys.stderr.write("\nPipeline success\n")
        break
    elif status == "failed":
        raise Exception
    elif status == "canceled":
        raise Exception

    time.sleep(10)

然后在 gitlab-ci.yml中将此Python脚本作为一个阶段调用

这篇关于如何在GitLab CI中显示来自另一个存储库的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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