From 93a1e193df2c18b2b8f6c1673dd08ecd136a2725 Mon Sep 17 00:00:00 2001 From: Dan Sirbu Date: Mon, 10 Mar 2025 22:18:27 +0200 Subject: [PATCH] Add dockerfile and publish functionality --- .github/workflows/gradle-ghcr-cicd.yml | 90 ++++++++++++++++++++++++++ .github/workflows/gradle.yml | 43 ------------ Dockerfile | 25 +++++++ README.md | 13 ++++ 4 files changed, 128 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/gradle-ghcr-cicd.yml delete mode 100644 .github/workflows/gradle.yml create mode 100644 Dockerfile diff --git a/.github/workflows/gradle-ghcr-cicd.yml b/.github/workflows/gradle-ghcr-cicd.yml new file mode 100644 index 0000000..cbc7660 --- /dev/null +++ b/.github/workflows/gradle-ghcr-cicd.yml @@ -0,0 +1,90 @@ +name: Java CI CD + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + + - name: Set up Gradle + uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 + + - name: Build with Gradle + run: ./gradlew build + + dependency-submission: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + + - name: Generate and submit dependency graph + uses: gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 + + push-to-ghcr: + name: Push image to ghcr + runs-on: ubuntu-latest + needs: build + if: github.event_name == 'push' || github.event_name == 'release' + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: + - uses: actions/checkout@v4 + + - name: Log in to container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=raw,value=latest + type=semver,pattern={{version}} + + - name: Build and push image + id: push + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Generate attestation + uses: actions/attest-build-provenance@v2 + with: + subject-name: ghcr.io/${{ github.repository }} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true \ No newline at end of file diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml deleted file mode 100644 index 5cee62c..0000000 --- a/.github/workflows/gradle.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Java CI - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read - steps: - - uses: actions/checkout@v4 - - - name: Set up JDK 21 - uses: actions/setup-java@v4 - with: - java-version: '21' - distribution: 'temurin' - - - name: Set up Gradle - uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 - - - name: Build with Gradle - run: ./gradlew build - - dependency-submission: - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - uses: actions/checkout@v4 - - - name: Set up JDK 21 - uses: actions/setup-java@v4 - with: - java-version: '21' - distribution: 'temurin' - - - name: Generate and submit dependency graph - uses: gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..89e0992 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +# Copyright (c) 2025 Dan Sirbu +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT +FROM gradle:jdk21 AS build +WORKDIR /app +COPY build.gradle ./build.gradle +COPY gradle ./gradle +COPY gradlew ./gradlew +COPY settings.gradle ./settings.gradle +RUN chmod +x ./gradlew + +# Download dependencies +RUN ./gradlew dependencies + +# Copy source code and build jar +COPY src ./src +RUN ./gradlew bootJar + +FROM eclipse-temurin:21-jdk +WORKDIR /app +COPY --from=build /app/build/libs/*.jar ./app.jar +EXPOSE 8080 + +ENTRYPOINT [ "java", "-jar", "./app.jar" ] \ No newline at end of file diff --git a/README.md b/README.md index da8e1f0..df7576f 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,19 @@ Here is a short guide on how to run this project on your pc: Inside the root directory of the project run ``.\gradlew bootRun --args='--spring.profiles.active=dev'``. This command starts the backend with the dev profile active +## Using the docker container +In order to use the docker container, you only need docker installed. +First you have to build image with ``docker build -t simple-store:latest .`` + +And then run the image, while defining the needed environment variables (see [.env.template](.env.template)). +Any arguments added to the end will be appended to the program (useful for setting spring boot profiles) +### Running using .env file +If you have an .env file, then in order to run the container just do +``docker run --name simple-store-backend --env-file LOCATION_OF_ENV_FILE -p 8080:8080 simple-store:latest`` +## Running without .env file +If you do not have an .env file, then you will have to define all the needed environment variables yourself. +Here is how to run the container without an .env file: +``docker run --name simple-store-backend -e ENV_VAR1=VAL1 -e ENV_VAR2=VAL2 ... -p 8080:8080 simple-store:latest`` # 📡 API Documentation - The API is documented using **Swagger**