A Spring War Deployment Bash Script

I blogged on how to deploy your Executable Spring War file on a public-facing server. Today we're going to make that process even easier by automating server deployment in bash.

We packaged up our War executable in IntelliJ and Gradle (see the earlier post) and will now perform the following steps in bash.

  1. Mount our remote server as a local drive. We'll be using my Linode Cloud Server in the example
  2. Copy our War file to the remote server
  3. Kill the existing War process on the remote server
  4. Launch our new Spring War

We'll need two bash scripts, one to mount our Remote Server and copy the War, and a second script to kill the existing War process on the remote server and launch the new site. We need two scripts because the second script obviously has to be executed on the remote server to which we've connected with an SSH Terminal Session.

Here's the first script we'll run. We're going to check if our /linode remote has been mounted and if not mount it. Then we'll copy the War from where it was created in IntelliJ and place it on the remote server.

#!/bin/bash

FILE=/nixmash-spring/mvc/build/libs/nixmashSpring.war
LINODEDIRECTORY=/linode/wars

if mount | grep /linode > /dev/null; then
    echo "/linode is already mounted. Copying war to host..."
else
    sshfs daveburke@XX.XX.XX.XX:/ /linode
    echo "/linode has been mounted. Copying war to host..."
fi

cp $FILE $LINODEDIRECTORY

echo "nixmashSpring.war copied to host. Execute startNixmashSpring.war on host"

The second script is executed on the remote server, as I said, where it will kill the java -jar nixmashSpring.war process and launch the War file we just copied in a background process.

#!/bin/bash

echo "killing existing nixmashSpring.war instance..."
kill $(ps aux | grep '[n]ixmashSpring.war' | awk '{print $2}')
echo "starting new nixmashSpring.war instance..."
java -jar /wars/nixmashSpring.war &

Here's what the two scripts look like when run in the console.

The second script executed in a remote terminal session.

One less deployment process we have to think about, which is always a good thing.

Posted September 09, 2015 03:52 PM EDT

More Like This Post