Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automating SpringBoot Deployment with GitHub Actions

Tech 1

Dockerfile Configuration

# Multi-stage build for efficiency
FROM maven:3.8-jdk-11 as build-stage

WORKDIR /workspace
COPY pom.xml .
COPY src ./src

# Build with caching and skip tests
RUN mvn package -DskipTests

# Runtime image
FROM openjdk:11-jre-slim

WORKDIR /app
COPY --from=build-stage /workspace/target/*.jar app.jar

# Run with production profile
ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=prod"]

Server Preparation

  1. Install Docker on the target server
  2. Note server credentials (IP, username, password) for GitHub Secrets

GitHub Actions Workflow

name: CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    env:
      SERVER_IP: ${{ secrets.SERVER_IP }}
      SERVER_USER: ${{ secrets.SERVER_USER }}
      SERVER_PWD: ${{ secrets.SERVER_PWD }}
      DEPLOY_DIR: "/deployments"
      APP_PORT: 8080
      VERSION: 1.0
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up JDK
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'

    - name: Build Docker image
      run: |
        docker build -t app:$VERSION .
        docker save -o app.tar app:$VERSION

    - name: Clean remote server
      uses: appleboy/ssh-action@master
      with:
        host: ${{ env.SERVER_IP }}
        username: ${{ env.SERVER_USER }}
        password: ${{ env.SERVER_PWD }}
        script: |
          docker stop app || true
          docker rm app || true
          docker rmi app:$VERSION || true
          rm -f $DEPLOY_DIR/app.tar

    - name: Transfer image
      run: |
        scp -o StrictHostKeyChecking=no app.tar $SERVER_USER@$SERVER_IP:$DEPLOY_DIR

    - name: Deploy application
      uses: appleboy/ssh-action@master
      with:
        host: ${{ env.SERVER_IP }}
        username: ${{ env.SERVER_USER }}
        password: ${{ env.SERVER_PWD }}
        script: |
          docker load -i $DEPLOY_DIR/app.tar
          docker run -d -p $APP_PORT:8080 --name app app:$VERSION

Verification Steps

  1. Check GitHub Actions execution logs
  2. On server, verify with:
    • docker ps for running containers
    • docker images for avialable images
  3. Access application at http://<server-ip>:8080

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.