Adding version number in Node.js app using Jenkins/OpenShift deploy

In a previous post I have illustrated how to deploy a Node.js app to OpenShift from a private GitHub repository using Jenkins.

It is often the case that you want to display the revision of the current code deployed in your test environment so you can quickly see if the running version of your app uses the latest code base. In my opinion this is a task for your build tool (such as Ant, Maven, Gradle, etc) or your automation server such as Jenkins.

I want to keep the following information in a file called version.txt and serve it when a user tries to GET it. Since I am using express, all I have to do in order to serve static files is the following:

Now, all I have to do is tell Jenkins to create the file version.txt, fill it with the necessary information and save it under public in my app’s deployment directory on the OpenShift server. You can find out the OpenShift deployment directory using the predefined environment variable $OPENSHIFT_REPO_DIR:

As we saw in OpenShift’s Jenkins configuration, a shell command is executed that deploys our Node.js application. So go ahead and navigate to YOUR_PROJECT_NAME -> Configuration. Scroll down where it says Execute Shell. This field already contains a bunch of shell commands. Append the following:

And thats it! The next time Jenkins builds your project, it will execute the above shell command which will in turn create a version.txt file and place it under public in you Node.js app. You can then access it via https://testnode-lukesnode.rhcloud.com/version.txt:

 

lucas