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:
app.use(express.static(__dirname + '/public'));
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
$ ssh firstname.lastname@example.org [testnode-lukesnode.rhcloud.com 586f71b70c1e6660d200000f]\> echo $OPENSHIFT_REPO_DIR /var/lib/openshift/586f71b70c1e6660d200000f/app-root/runtime/repo/
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:
# Version number $GIT_SSH $upstream_ssh "echo "JOB: $JOB_NAME" > /var/lib/openshift/586f71b70c1e6660d200000f/app-root/runtime/repo/public/version.txt" $GIT_SSH $upstream_ssh "echo 'BUILD NUMBER: $BUILD_NUMBER' >> /var/lib/openshift/586f71b70c1e6660d200000f/app-root/runtime/repo/public/version.txt" app_version=`git log -n 1 HEAD` $GIT_SSH $upstream_ssh "echo 'GIT REVISION: $app_version' >> /var/lib/openshift/586f71b70c1e6660d200000f/app-root/runtime/repo/public/version.txt"
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:
JOB: testnode-build BUILD NUMBER: 33 GIT REVISION: commit ce0dbb07199aa24f8c84f8d0260d153297751114 Author: Lucas Louca <email@example.com> Date: Wed Feb 22 18:58:38 2017 +0100 Testing version.txt