Joshua
Joshua

Reputation: 5494

How can get my rake task to let Jenkins know that the build has failed?

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.

Upvotes: 4

Views: 4129

Answers (1)

Roberto Decurnex
Roberto Decurnex

Reputation: 2534

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.

Upvotes: 5

Related Questions