Automating Java Deployments Using Git Hooks on a Panelless Linux VPS Print

  • java, VPS, Server
  • 213

Automating Java Deployments Using Git Hooks on a Panelless Linux VPS

Manually copying compiled .war or .jar files to your server using FTP can be slow and error-prone. This guide explains how to configure a local production machine to automatically deploy and restart a Java backend application using standard Git hooks directly on a panelless Linux VPS.

Prerequisites

  • An unmanaged Linux VPS with SSH access.
  • Git installed on both your local development environment and your VPS.
  • A persistent system service configuration managing your application runner thread (e.g., systemd setup).

Step 1: Set Up the Target Directories on the VPS

Log in to your VPS terminal via SSH and create two separate folders: one to hold the bare Git tracking cache, and one to act as the live installation working root directory.

  • Create the repository paths:
    mkdir -p /var/git/myapp.git
    mkdir -p /var/www/myapp
  • Initialize a blank tracking repository:
    cd /var/git/myapp.git
    git init --bare

Step 2: Create the Post-Receive Automation Script

Git hooks run background automation processes based on repository actions. The post-receive hook script executes tasks right after new code is successfully pushed up to the VPS server repository.

  • Create the script document using a terminal editor text manager:
    nano /var/git/myapp.git/hooks/post-receive
  • Paste the execution automation code blocks template below:
    #!/bin/bash
    TARGET="/var/www/myapp"
    GIT_DIR="/var/git/myapp.git"
    
    # Unpack the absolute latest code into the live project directory
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f production
    
    # Navigate into the deployment working path
    cd $TARGET
    
    # Trigger your compilation manager build tool (Maven example)
    mvn clean package -DskipTests
    
    # Copy or overwrite the new execution archive file into place if required
    # Then restart the underlying service script to apply the live code
    sudo systemctl restart myapp.service
    
  • Save the file and grant the server user the proper file permission privileges to execute the tracking script:
    chmod +x /var/git/myapp.git/hooks/post-receive

Step 3: Link Your Local Development Workspace

Open your terminal on your local production development desk machine inside your project workspace root and link the remote tracking pipeline target directly:

  • Add the VPS target remote tracking shortcut address:
    git remote add production user@your_vps_ip:/var/git/myapp.git
  • Whenever your code updates are finalized and ready to go live, push the active deployment branch to trigger the remote build:
    git push production main:production

Your VPS will catch the incoming commit stream, deploy the files fresh into the live directory path, compile the framework bundle binaries, and safely cycle the background running service automatically.


Was this answer helpful?

« Back