The Easiest Way To Deploy With Gradle

Written by steklopod | Published 2022/05/12
Tech Story Tags: javascript | gradle | cicd | java | kotlin | ssh | deployment | coding

TLDRWhen you run `git push, you will deploy the latest `master` branch distribution to the server without preparations on the server-side. After running a specific task your code distribution will be delivered to the remote server. There will be as many tasks as Gradle subprojects that will become available for your project. The plugin author promises that you will not find an easier way to deploy than this. If you have a remote Linux server for several gigabytes, you’ll need to decide upon a method for securely connecting to it.via the TL;DR App

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

Goal

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

Introduction

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?

gradle-ssh-plugin

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

Prerequisites

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/id_rsa.pub root@host.domain

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 ~/${project.name}/... folder

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

tasks {
    scp { 
        host = "colaba.online"
    }
}

Run deploy task:

gradle scp

🔮 Customization:

  1. Register new task in your build.gradle.kts:
register("customSshTask", Ssh::class) {
   host = "my-domain.com"
   user = "root"
   gradle = true
   frontend = false
   docker = true
   nginx = true
   directory = "distribution"
   run = "cd ${project.name} && 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.name}


📋 Project's structure example

  • There could be as many backends as you need.
 project
   |-[backend]
              | - [src/main/java/build/libs]/*.jar
              | - Dockerfile
              | - Dockerfile.dev
              | - docker-compose.yml
              | - docker-compose.dev.yml
              | - ...
   |-[backend-2]
              | - [src/main/koltin/build/libs]/*.jar
              | - ...
   |-[backend-3]
              | - [src/main/scala/build/libs]/*.jar
              | - ...
   |-[frontend]
              | - docker-compose.yml
              | - ...
   |-[nginx]
              | - ...
   |-[postgres]
              | - [backups]
              | - ...
   |-[elastic]
              | - ...
   |-[static]
              | - ...
   |-[gradle]
              | - [wrapper]
   |- gradlew
   |- gradlew.bat
   |- docker-compose.yml
   |- ...

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


Written by steklopod | Kotlin developer
Published by HackerNoon on 2022/05/12