Skip to Main Content

Using Gradle for Single Click Deployment

using gradle for single click deployment

As a software engineer, nothing demotivates me more than having to test a web application remotely. Just imagining having to stop the application, upload the new code, and rerun the app tires me out. Fortunately, this process can be easily automated with build tools. Here I will share how I’ve overcome this issue by using one of my favorite tools: Gradle.

We will be deploying a simple “Hello World” web app. The entire source used here can be found on GitHub.

 

Requirements:

  • Gradle
  • A remote Linux machine (we will be using a Docker container)
  • Basic command line knowledge

 

Project folder structure:

- webapp/
  - src/main/java/server/Main.java
  - deploy/
    - Dockerfile
    - setup_webserver.sh
  - build.gradle
  - gradle.properties

 

 

Step 1: Setting up our remote machine

Note: If you already have a Linux server that you can SSH into, then skip to Step 2.

Let’s begin by creating a Docker image using a Dockerfile. Following the folder structure shown previously, save this to webapp/deploy/Dockerfile:

Dockerfile:

FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y openssh-server
RUN apt-get install -y unzip
RUN apt-get install -y default-jre
RUN mkdir /var/run/sshd
RUN echo 'root:password' | chpasswd
RUN useradd webadmin
RUN echo "webadmin:password" | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@sessions*requireds*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
RUN mkdir /home/webadmin/
RUN chown -R webadmin:webadmin /home/webadmin/
EXPOSE 22 80
CMD ["/usr/sbin/sshd", "-D"]

To execute:

  • Open command line in webapp/deploy/
  • Execute:
    docker build -t ssh_ubuntu ./
  • Then execute:
    docker run -t -d -p 8887:22 -p 8888:8888 --name webserver ssh_ubuntu

The first command will create a Linux Docker image with all the requirements and a user called webadmin with password “password”.

The second command will create a Docker container with the Linux image. Note: It will map the SSH port to 8887. To continue with the other steps, download the rest of the article here.

Isaac Yousuf, Software Engineer III

Isaac Yousuf joined Accusoft as a software engineer in 2016. He currently contributes to the Accusoft SDK products. Although he is early in his career, he has presented at a conference on Deep Learning, and he plans on doing more. A graduate of the University of South Florida, Isaac’s degree is in Information Technology. His background includes enterprise web applications, hardware accelerated graphics, physics engines, and more. In his spare time, Isaac enjoys online games, running his guild, rugby, and coding.