Continuous Integration Starting and Stopping Windows services in your build - mxunit/mxunit GitHub Wiki

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:

mongod --install --logpath c:\path\to\some\logfile.log

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.

<target name="startMongoService" depends="init">
	<echo message="starting ${MongoDBService}"/>
	<exec executable="cmd.exe">
		<arg line="/c"/>
		<arg line="net"/>
		<arg line="start"/>
		<arg line="${MongoDBService}"/>
	</exec>
</target>

<target name="stopMongoService" depends="init">
	<echo message="stopping ${MongoDBService}"/>
	<exec executable="cmd.exe">
		<arg line="/c"/>
		<arg line="net"/>
		<arg line="stop"/>
		<arg line="${MongoDBService}"/>
	</exec>
</target>

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

⚠️ **GitHub.com Fallback** ⚠️