如何让我的 rake 任务让 Jenkins 知道构建失败了?

发布于 12-26 11:25 字数 1344 浏览 1 评论 0原文

我们刚刚为我们的应用程序设置了一个 Jenkins CI 服务器,HiringThing

一切正常,当代码签入我们的 Github 存储库时,构建会自动启动。

问题是检测构建失败。我有以下 rake 任务由 Jenkins 从命令行运行。

rake test:browser

运行以下内容

desc "Run browser tests."
task :browser => :environment do
    start = Time.now()
    puts "Stopping apache if running"
    system 'sudo apache2ctl stop'
    puts "Running selenium tests"
    Dir.glob('./test/browser/*.rb').each { |r|
        puts r
        system 'rvmsudo ruby ' +  r
    }
    system 'echo -e "\a"'
    puts "All browser tests, elapsed: " + (Time.now() - start).to_s + " seconds"
end

我已经确认测试运行正常(使用带有 Xvfb 的无头 Firefox) 问题是 Jenkins 在浏览器测试中没有检测到任何构建失败。它似乎适用于通常的 Rails 单元、功能和集成测试(我在执行此任务之前在 Jenkins 中运行“rake test”。)

据我所知,上面的脚本不会通过 ruby​​ 任务的失败退出代码对詹金斯来说,不知道为什么。现在我能想到的唯一解决方法是将所有测试输出重定向到缓冲区并使用 grep 查找失败关键字并传递失败的退出代码(如果找到),但这对我来说感觉很糟糕。

我需要更改什么才能让 Jenkins 检测到“system 'rvmsudo ruby​​ ' + r”命令何时返回失败的退出代码?

UPDATE

robertodecurnex 是正确的,系统始终返回成功。然而,rake 命令“sh”允许您捕获准确的代码。所以我将这个: 更改

system 'rvmsudo ruby ' +  r

为:

 sh 'rvmsudo ruby ' +  r do |ok, res|
     raise "Failed test." if !ok
 end

这似乎通过退出 Rake 并显示失败代码并警告 Jenkins 构建未成功来实现这一点。

We've just set up a Jenkins CI server for our app, HiringThing

Everything works well, with builds starting automatically when code is checked into our Github repo.

The problem is detecting build failures. I have the following rake task being run from the command line by Jenkins.

rake test:browser

runs the following

desc "Run browser tests."
task :browser => :environment do
    start = Time.now()
    puts "Stopping apache if running"
    system 'sudo apache2ctl stop'
    puts "Running selenium tests"
    Dir.glob('./test/browser/*.rb').each { |r|
        puts r
        system 'rvmsudo ruby ' +  r
    }
    system 'echo -e "\a"'
    puts "All browser tests, elapsed: " + (Time.now() - start).to_s + " seconds"
end

I've confirmed that the tests are running properly (using headless Firefox with Xvfb) The problem is that Jenkins doesn't detect any build failures in the browser tests. It seems to work for the usual Rails unit, functional and integration tests (I run "rake test" in Jenkins before this task.)

As far as I can tell, this script above doesn't pass the failed exit code of the ruby tasks to Jenkins, no idea why. Right now the only workaround I can think of is to redirect all the test output to a buffer and use grep to look for failure keywords and pass the failed exit code if found, but that feels hacky to me.

What do I need to change to get Jenkins to detect when the "system 'rvmsudo ruby ' + r" commands return a failed exit code?

UPDATE

robertodecurnex is correct, system is always returning success. The rake command 'sh', however, allows you to capture accurate codes. So I changed this:

system 'rvmsudo ruby ' +  r

to this:

 sh 'rvmsudo ruby ' +  r do |ok, res|
     raise "Failed test." if !ok
 end

Which seems to be doing the trick by exiting Rake with a fail code and alerting Jenkins that the build did not succeed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一直在等你来2025-01-02 11:25:03

即使系统调用失败,您的任务也会返回正常的执行状态。

为了让 CI 服务器通知您出现问题,该进程应该返回错误执行代码。

请注意,举个例子,如果在任何情况下出现错误,cucumber 和 rspec 都会失败。

您应该检查system 调用返回值并使您的任务返回相同的值,或者在同一 ruby​​ 进程中运行测试,而不是作为对另一个 ruby​​ 脚本的system 调用。

Even if the system call fails your task is returning a normal execution status.

In order to get you CI server notified that there's something wrong the process should be returning an error execution code.

Note that, as an example, cucumber and rspec will blow if there's an error in any of the cases.

You should either check the system call return value and make your task return the same or run the tests inside the same ruby process and not as a system call to another ruby script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文