From cc92d2938d9e46f0a151dae14c8acc050f5da5d1 Mon Sep 17 00:00:00 2001 From: denniskp <122602355+denniskp@users.noreply.github.com> Date: Sun, 9 Nov 2025 22:42:27 +0100 Subject: [PATCH] feat: Docker setup --- .github/workflows/docker.yml | 65 ++++++++++++++++++++++++++++++++++++ .github/workflows/maven.yml | 12 +++---- Dockerfile | 22 +++++------- compose.yaml | 8 +++++ 4 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/docker.yml create mode 100644 compose.yaml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..874c928 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,65 @@ +name: Docker CI + +on: + release: + types: [ 'published' ] + +env: + TARGET_PLATFORM: linux/amd64,linux/arm64 + REGISTRY: ghcr.io + +jobs: + maven-build: + uses: ./.github/workflows/maven.yml + + docker-build-publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + needs: + - maven-build + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set lowercase repository name + run: echo "REPO_LC=${GITHUB_REPOSITORY@L}" >> $GITHUB_ENV + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.REPO_LC }} + tags: | + type=semver,pattern={{raw}} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Download Artifact + uses: actions/download-artifact@v4 + with: + name: build-artifacts + path: staging/ + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + platforms: ${{ env.TARGET_PLATFORM }} diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 8e1690d..3e5a2da 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -17,25 +17,25 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v4 - + - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: 21 distribution: temurin cache: maven - + - name: Build with Maven run: mvn -B package --file pom.xml - + - name: Copy Artifact - run: mkdir staging && cp -p target/*.jar staging - + run: mkdir staging && cp -p target/*.jar staging/codespark-backend.jar + - name: Upload Artifact uses: actions/upload-artifact@v4 with: name: codespark-backend - path: staging + path: staging/ retention-days: 1 - name: Update dependency graph diff --git a/Dockerfile b/Dockerfile index 47e5b1f..78f0ffa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,14 @@ -# Use an official Maven image to build the project -FROM maven:3.9.6-eclipse-temurin-21 AS build +# Use Java slim image +FROM openjdk:21-slim -# Copy all the code into the image -COPY . . +# Set workdir +WORKDIR /app -# Build the Spring Boot application -RUN mvn clean package -DskipTests - -# Use JRE image to run the application -FROM eclipse-temurin:21-jre - -# Copy the built JAR from the build stage -COPY --from=build /target/*.jar app.jar +# Copy the java file +COPY staging/codespark-backend.jar . # Expose port 8080 for the application EXPOSE 8080 -# Run the application -ENTRYPOINT ["java", "-jar", "app.jar"] +# Set default command to run the bot +CMD ["java", "-jar", "codespark-backend.jar"] diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..e9d8dc3 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,8 @@ +services: + codespark-backend: + container_name: codespark-backend + image: ghcr.io/codesparkapp/api:latest + ports: + - "8080:8080" + restart: always + env_file: stack.env