The Easiest Way To Deploy With Gradle by@steklopod

The Easiest Way To Deploy With Gradle

Dmitry Kaltovich HackerNoon profile picture

Dmitry Kaltovich

Kotlin developer

Here are the simplest steps needed to create a deployment from your local GIT repository to a remote server.


When you run git push, deploy the latest master branch distribution to the server without preparations on the server-side.



In addition to writing code, indie developers need to think about the continuous delivery of their services. And often setting up a deployment pipeline is a non-trivial task: you need to set up Jenkins, Kubernetes, Git Hooks, and many other technologies. In addition to the fact that they require such a valuable programmer resource as the time needed for learning them, they also consume hardware resources. But what if I have a Linux server for several gigabytes where I want to place, for example, my small blog with zero configs?


As the plugin author, I promise that you will not find an easier way to deploy than this.


If you have a remote Linux server you must have the id_rsa file.

When setting up a remote Linux server, you’ll need to decide upon a method for securely connecting to it. While passwords are one way of verifying a user’s identity, passwords have multiple vulnerabilities and can be cracked by a brute force attack. Secure Shell keys — better known as SSH keys — are often used instead of passwords, as they offer a more secure method of connecting to remote Linux servers. As part of the Secure Shell cryptographic network protocol, SSH keys also enable users to securely perform network services over an unsecured network, such as delivering text-based commands to a remote server or configuring its services.

If you don’t have this key let’s create it - run this commands on your local machine:

  1. ssh-keygen -m PEM -t rsa -b 2048
  2. chmod 400 ~/.ssh/id_rsa
  3. ssh-copy-id -i .ssh/ [email protected]

Now you need to copy the file ~/.ssh/id_rsa from the local machine into the root of your project which you need to deploy.

🎯 Quick start

In root project build.gradle.kts file:

plugins {
    id("online.colaba.ssh") version "1.8.17"
group = "online.colaba"

That's all! After running a specific grade task your code distribution will be delivered to the remote server.

This task will copy folders & files from local machine to remote host ~/${}/... folder

You can set host, or it will computed from (example above)

tasks {
    scp { 
        host = ""

Run deploy task:

gradle scp

🔮 Customization:

  1. Register new task in your build.gradle.kts:
register("customSshTask", Ssh::class) {
   host = ""
   user = "root"
   gradle = true
   frontend = false
   docker = true
   nginx = true
   directory = "distribution"
   run = "cd ${} && echo \$PWD"
  1. Run this task:
gradle customSshTask

🌀 Available Gradle tasks from ssh plugin:

By default you have preconfigured tasks:

  • ssh - all options are disabled by default (false)
  • scp - all options are enabled by default (true)
  • ssh-gradle - copy gradle needed files to remote server in every subproject
  • ssh-docker - copy docker files to remote server
  • ssh-jars - copy ${subproject}/build/libs/___.jar file to remote server in every subproject

Example of tasks that will become available for your project:

  • There will be as many tasks as Gradle subprojects.
  1. ssh-backend - copy backend distribution *.jar-file to remote server
  2. ssh-frontend - copy frontend folder to remote server
  3. ssh-nginx - copy nginx folder to remote server
  4. ...

Name of service for all tasks equals to ${}

📋 Project's structure example

  • There could be as many backends as you need.
              | - [src/main/java/build/libs]/*.jar
              | - Dockerfile
              | -
              | - docker-compose.yml
              | -
              | - ...
              | - [src/main/koltin/build/libs]/*.jar
              | - ...
              | - [src/main/scala/build/libs]/*.jar
              | - ...
              | - docker-compose.yml
              | - ...
              | - ...
              | - [backups]
              | - ...
              | - ...
              | - ...
              | - [wrapper]
   |- gradlew
   |- gradlew.bat
   |- docker-compose.yml
   |- ...

A more detailed description of this plugin and examples will be in my next articles soon.


Signup or Login to Join the Discussion


Related Stories