From 518f5788f70d0c72fd98d2844d5d35ce2979216e Mon Sep 17 00:00:00 2001 From: AleixMT Date: Tue, 16 Jan 2024 22:28:03 +0100 Subject: [PATCH] ADDED: docker support and ci cd --- .github/workflows/ci.yml | 38 +++++++++++++++ Dockerfile | 48 +++++++++++++++++++ docker-compose.yml | 27 +++++++++++ pom.xml | 3 ++ src/main/resources/application-dev.properties | 21 ++++++++ .../resources/application-prod.properties | 21 ++++++++ 6 files changed, 158 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3469272 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +name: ci + +# Trigger event on every push / delete event, no matter the branch +# on: [push, delete] + +# Trigger event on every push / delete event, only in master branch +on: + push: + branches: + - "master" + delete: + branches: + - "master" + +jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - + name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/book-a-book-operador:latest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bd80e68 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +# Use the UBI minimal base image for the compilation stage +FROM openjdk:17-oracle as build + +# Set the working directory +WORKDIR /app + +# Install necessary dependencies for compilation +RUN microdnf install --nodocs -y java-17-openjdk-headless maven && \ + microdnf clean all + +# Copy the source code to the container +COPY . /app + +# Apply permissions +RUN chown -R 1001:1001 /app + +# Set the user to run the application +USER 1001 + +# Compile project skipping testing goals (compilation, resources and run of tests) +RUN JAVA_HOME= ./mvnw clean package spring-boot:repackage -Dmaven.test.skip=true + + +# Use the UBI minimal base image for the run stage +FROM openjdk:17-oracle + +# Set the working directory +WORKDIR /app + +# Install necessary dependencies for running +RUN microdnf install --nodocs -y java-17-openjdk-headless && \ + microdnf clean all + +# Copy the directory created in the first stage into the run container +RUN mkdir -p /app/target +COPY --from=build /app/target/book-a-book-operador.jar /app + +# Cambia la propiedad del directorio '/app/target' al usuario con id 1001 +RUN chown 1001:1001 /app/book-a-book-operador.jar + +# Cambia el usuario que va a ejecutar los siguientes comandos al usuario con id 1001 +USER 1001 + +# Set the application profile in order to change the config of DB location +ENV spring_profiles_active=prod + +ENTRYPOINT ["java", \ + "-jar", "book-a-book-operador.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e2dbe33 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +--- +version: "2.1" +services: + book-a-book-buscador: + image: aleixmt/book-a-book-buscador:latest + ports: + - "0.0.0.0:8082:8081" + #restart: unless-stopped + + book-a-book-operador: + image: aleixmt/book-a-book-operador:latest + ports: + - "0.0.0.0:8084:8083" + #restart: unless-stopped + + postgres_db: + image: postgres + environment: + - PUID=1001 + - PGID=1001 + - TZ=Etc/UTC + - POSTGRES_PASSWORD=chemistry + - POSTGRES_USER=aleixmt + - POSTGRES_DB=book-a-book-operador + ports: + - "0.0.0.0:5433:5432" + restart: unless-stopped diff --git a/pom.xml b/pom.xml index 6e7e927..23bdebc 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ net.unir.missi.desarrollowebfullstack.bookabook operador 0.0.1-SNAPSHOT + jar operador Operador microservice for the Book a book web application @@ -42,6 +43,8 @@ + book-a-book-operador + org.springframework.boot diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index e69de29..c82eb7d 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -0,0 +1,21 @@ +#################################### +### SETTING UP CONNECTION TO DB #### +#################################### + +# Set connection to the desired table of DB using URL +spring.datasource.url=jdbc:postgresql://localhost:5432/bookabook + +# Database credentials +spring.datasource.username=aleixmt +spring.datasource.password=chemistry + +# Default policy with database contents when booting: "validate" checks consistency, "create" wipes colliding tables and creates +# them from scratch, "create-drop" creates DB schema and then drops the information. "Update" updates the information. +# "None" does nothing +spring.jpa.hibernate.ddl-auto=update + +# Output the SQL queries +spring.jpa.show-sql=true +# Format of the SQL queries written in the output +spring.jpa.properties.hibernate.format_sql=true + diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index e69de29..e6774cc 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -0,0 +1,21 @@ +#################################### +### SETTING UP CONNECTION TO DB #### +#################################### + +# Set connection to the desired table of DB using URL +spring.datasource.url=jdbc:postgresql://postgres_db:5432/book-a-book-operador + +# Database credentials +spring.datasource.username=aleixmt +spring.datasource.password=chemistry + +# Default policy with database contents when booting: "validate" checks consistency, "create" wipes colliding tables and creates +# them from scratch, "create-drop" creates DB schema and then drops the information. "Update" updates the information. +# "None" does nothing +spring.jpa.hibernate.ddl-auto=update + +# Output the SQL queries +spring.jpa.show-sql=true +# Format of the SQL queries written in the output +spring.jpa.properties.hibernate.format_sql=true +