Continuous Integration -- Starting and Stopping Windows services in your build

Introduction

For CFMongoDB , I wanted to run all tests on a schedule, either when a source code change was detected on github, or every day. I didn't want to have Mongo running all the time -- just when the tests needed to run. Ideally, my CI process would start Mongo, run the tests, and stop Mongo when finished. 

Here's how I accomplished that.

Installing MongoDB as a service

It was easiest for me to achieve this workflow by starting MongoDB as  a Windows service because then I could simply use NET START and NET STOP commands rather than spinning up Mongod.exe and killing it.

Installing mongod as a service is as simple as:

How you will be able to run this service in Jenkins, but can't quite yet

Long heading, I know. In Jenkins, configuring the "Build Steps" should look like this:

  1. Invoke Windows Batch Command --  "NET START 'Mongo DB'"
  2. ANT -- runtests
  3. Invoke Windows Batch Command -- "NET STOP 'Mongo DB'"

However, due to this bug , stopping the mongo service throws an error which will cause the Jenkins build to fail. And that won't do.

How to use ANT to start and stop the MongoDB Windows Service

Fortunately, you can add ANT targets to start and stop these services which won't throw errors when start/stop fails, which is the behavior I need.

build.xml

 Then, in the Jenkins config, I simply specify "startMongoService runtests stopMongoService" as the ANT targets to run.

Navigate space
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.