diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 3f91c66..3611949 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -1,35 +1,38 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: Java CI with Maven +# Définition du nom du workflow, qui sera affiché sur GitHub +name: Java CI/CD with Maven +# Définition des événements qui déclencheront l'exécution du workflow on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] + push: # Lors d'un push + branches: [ "main" ] # Sur la branche main + pull_request: # Lors d'une pull request + branches: [ "main" ] # Ciblant la branche main +# Définition des jobs à exécuter jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 21 - uses: actions/setup-java@v3 - with: - java-version: '21' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml - - # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - # - name: Update dependency graph - # uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 + build-and-test: # Nom du job + runs-on: ubuntu-latest # Exécute le job sur la dernière version d'Ubuntu disponible + + steps: # Étapes du job + - uses: actions/checkout@v4 # Étape pour cloner le code source du dépôt + + - name: Set up JDK 21 # Étape pour configurer JDK 21 + uses: actions/setup-java@v3 # Utilise l'action setup-java pour configurer Java + with: # Options pour l'action setup-java + java-version: '21' # Spécifie la version de Java à utiliser + distribution: 'temurin' # Spécifie la distribution de Java à utiliser + cache: maven # Active le cache pour Maven + + - name: Build with Maven # Étape pour construire le projet avec Maven + run: mvn -B package --file pom.xml # Exécute Maven pour compiler le projet et empaqueter + + - name: Run Checkstyle # Étape pour exécuter Checkstyle + run: mvn checkstyle:checkstyle # Exécute Checkstyle pour vérifier la conformité du style de code + + - name: Run Tests # Étape pour exécuter les tests + run: mvn test # Exécute les tests unitaires + + - name: Build Docker Image # Étape pour construire l'image Docker + run: docker build -t spring-boot-student-management . # Construit l'image Docker à partir du Dockerfile + + # Ajoutez ici les étapes supplémentaires pour l'analyse de code et le déploiement \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6a4349d..f3fe98c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,33 @@ -FROM openjdk:21 +# Utilisation d'une image de base plus légère +FROM maven:eclipse-temurin + LABEL authors="nassim" -ARG JAR_FILE=target/*.jar -COPY ${JAR_FILE} app.jar +# Création d'un répertoire pour l'application +WORKDIR /app + +# Installation de dockerize +ENV DOCKERIZE_VERSION v0.6.1 +RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ + && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ + && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz + +# Copie du fichier pom.xml et téléchargement des dépendances pour améliorer la mise en cache +COPY pom.xml . +RUN mvn dependency:go-offline + +# Copie du reste de l'application +COPY src ./src + +# Construction de l'application +RUN mvn clean install -DskipTests + +# Copie du jar dans le conteneur +RUN mv target/*.jar app.jar + +# Spécification d'un utilisateur non root +RUN useradd -m myuser +USER myuser -ENTRYPOINT ["java","-jar","/app.jar"] +ENTRYPOINT ["dockerize", "-wait", "tcp://db:3306", "-timeout", "30s", "java","-jar","app.jar"] +EXPOSE 8080 \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..9fbeee1 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,31 @@ +services: + db: + image: mysql:8.0 + command: --default-authentication-plugin=caching_sha2_password + environment: + MYSQL_ROOT_PASSWORD: password + MYSQL_DATABASE: springbootdb + volumes: + - db_data:/var/lib/mysql + networks: + - app-network + + app: + build: . + ports: + - "8080:8080" + environment: + SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/springbootdb + SPRING_DATASOURCE_USERNAME: root + SPRING_DATASOURCE_PASSWORD: password + depends_on: + - db + networks: + - app-network + +volumes: + db_data: + +networks: + app-network: + driver: bridge \ No newline at end of file diff --git a/mvnw b/mvnw old mode 100644 new mode 100755 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5b812ce..0bbe21b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb +spring.datasource.url=jdbc:mysql://db:3306/springbootdb spring.datasource.username=root spring.datasource.password=password spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect