Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Docker CI/CD for Coderacer Backend

on:
push:
branches:
- main
- cicd

env:
IMAGE_NAME: coderacer-postgres # Name of your Docker image
DOCKER_REGISTRY: ghcr.io # Using GitHub Container Registry (ghcr.io)
APP_PORT: 8000

jobs:
build-and-push:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Step to define a lowercase repository name
- name: Set lowercase repository name
id: set_repo_name
run: echo "LOWER_REPO_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'maven'

- name: Build Spring Boot application with Maven
run: mvn clean install -DskipTests
working-directory: ./

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to Docker Registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ env.DOCKER_REGISTRY }}/${{ env.LOWER_REPO_NAME }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
${{ env.DOCKER_REGISTRY }}/${{ env.LOWER_REPO_NAME }}/${{ env.IMAGE_NAME }}:latest
build-args: |
APP_PORT=${{ env.APP_PORT }}
SQL_DATABASE=${{ secrets.SQL_DATABASE }}
SQL_USERNAME=${{ secrets.SQL_USERNAME }}
SQL_PASSWORD=${{ secrets.SQL_PASSWORD }}
EMAIL_ADDRESS=${{ secrets.EMAIL_ADDRESS }}
EMAIL_PASSWORD=${{ secrets.EMAIL_PASSWORD }}
cache-from: type=gha
cache-to: type=gha,mode=max
env:
SQL_DATABASE: ${{ secrets.SQL_DATABASE }}
SQL_USERNAME: ${{ secrets.SQL_USERNAME }}
SQL_PASSWORD: ${{ secrets.SQL_PASSWORD }}
EMAIL_ADDRESS: ${{ secrets.EMAIL_ADDRESS }}
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
19 changes: 19 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
55 changes: 55 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Stage 1: Build the React frontend
FROM node:20 AS frontend-build

WORKDIR /frontend

# Install dependencies
COPY frontend/package.json frontend/package-lock.json ./
RUN npm install --legacy-peer-deps

# Copy the rest of the frontend source code
COPY frontend .

# Build the React frontend
RUN npm run build

# Stage 2: Build the Spring Boot application
FROM eclipse-temurin:21-jdk-jammy AS build

WORKDIR /app


# Copy the Maven wrapper and pom.xml to leverage Docker caching
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .

# Grant execute permissions to mvnw
RUN chmod +x mvnw

# Download dependencies (only if pom.xml changes)
RUN ./mvnw dependency:go-offline -B

# Copy the rest of the source code
COPY src ./src

# Copy React build output into Spring Boot static resources
COPY --from=frontend-build /frontend/build ./src/main/resources/static

# Build the Spring Boot application
RUN ./mvnw clean install -DskipTests

# Stage 3: Create the final Docker image
FROM eclipse-temurin:21-jre-jammy

# Set the working directory
WORKDIR /app

# Copy the built JAR from the build stage
COPY --from=build /app/target/*.jar app.jar

# Expose the port your Spring Boot app runs on (default is 8080, but yours is 8000)
EXPOSE 8000

# Run the Spring Boot application
ENTRYPOINT ["java", "-jar", "app.jar"]
6 changes: 3 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ services:
image: postgres:16-alpine
container_name: coderacer-postgres
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${SQL_DATABASE}
POSTGRES_USER: ${SQL_USERNAME}
POSTGRES_PASSWORD: ${SQL_PASSWORD}
ports:
- "5332:5432"
volumes:
Expand Down
Loading