Simple SpringBoot to DigitalOcean droplet github action (Chatgpt assited)

Simple Spring Boot Deployment to DigitalOcean Droplet with GitHub Actions

In this article, we'll outline a process for automating the deployment of a Spring Boot application to a DigitalOcean droplet using GitHub Actions. This is a simple approach to continuous integration/continuous deployment (CI/CD) that makes your development process more efficient and error-proof.

Pre-requisites

  • A Spring Boot application
  • A DigitalOcean droplet
  • SSH access to the droplet

Steps

1. Generate an SSH key pair

If you haven't done so already, generate an SSH key pair on your local machine:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Follow the prompts to save the key pair and enter a passphrase.

2. Add the public key to the DigitalOcean droplet

Add the contents of the public key (typically saved as ~/.ssh/id_rsa.pub) to the ~/.ssh/authorized_keys file on the droplet.

3. Save the private key to GitHub Secrets

In the GitHub repository, go to Settings > Secrets > New repository secret and add the private key (~/.ssh/id_rsa) as a new secret, e.g., DEPLOY_KEY.

4. Set up the GitHub Action

In your GitHub repository, create a new file at .github/workflows/deploy.yml and add the following content:

name: Deploy to DigitalOcean

on:
  push:
    branches:
      - main  # Trigger the workflow on push to the main branch

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up JDK 17
      uses: actions/setup-java@v2
      with:
        java-version: '17'
        distribution: 'adopt'

    - name: Cache Maven packages
      uses: actions/cache@v2
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-m2

    - name: Build with Maven
      run: mvn clean package

    - name: Install SSH key
      uses: webfactory/ssh-agent@v0.5.3
      with:
        ssh-private-key: ${{ secrets.DEPLOY_KEY }}

    - name: Deploy
      run: |
        scp target/*.jar user@server:/path/to/app
        ssh -T user@server <<'EOF'
          set -x
          PID=$(jps -l | grep 'your-app-name' | awk '{print $1}')
          echo "PID found: $PID"
          if [ -n "$PID" ]; then
            kill -9 $PID
            echo "Killed process $PID"
          fi
          nohup java -jar /path/to/app/your-app.jar > /dev/null 2>&1 &
          echo "Started new process"
        EOF

Replace user@server with the appropriate user and IP address of your DigitalOcean droplet, your-app-name with the name of your application (or more precisely, the name of your jar file without the .jar extension), and /path/to/app/your-app.jar with the full path to your jar file on the droplet.

This script does the following:

  • Checks out your code
  • Sets up Java 17
  • Caches your Maven packages to speed up subsequent builds
  • Builds your application using Maven
  • Installs the SSH key that we saved in the GitHub Secrets
  • Copies the built jar file to the droplet using scp
  • Logs into the droplet via SSH, kills any running instance of your application, and starts a new instance

By utilizing GitHub Actions, this process is automated, so that any push to the main branch of your repository will trigger a new build and deployment of your application.

Conclusion

With this simple setup, you can automate the deployment of your Spring Boot application to a DigitalOcean droplet. It eliminates the need for manual deployments and ensures that your application is always running the latest code. Remember, while this approach works well for small projects, larger applications may require a more sophisticated deployment process with separate stages for testing and deployment.