diff --git a/.github/workflows/docker-ci.yml b/.github/workflows/docker-ci.yml index d6ee24a..5bdd105 100644 --- a/.github/workflows/docker-ci.yml +++ b/.github/workflows/docker-ci.yml @@ -11,10 +11,11 @@ jobs: runs-on: ubuntu-latest steps: - + # 1. Get the source code - name: Checkout Code uses: actions/checkout@v4 + # 2. Setup Java 21 with Maven Caching - name: Set up JDK 21 uses: actions/setup-java@v4 with: @@ -22,27 +23,39 @@ jobs: distribution: 'temurin' cache: maven + # 3. Run Tests & Coverage Gate - name: Run Unit Tests - run: mvn test - + run: mvn clean test + + # 4. Generate Coverage Summary for GitHub UI + - name: Add Coverage Summary to Action + uses: irongut/CodeCoverageSummary@v1.3.0 + with: + filename: target/site/jacoco/jacoco.xml + badge: true + format: markdown + output: both + + - name: Write Summary to GitHub Step + run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY + + # 5. Prepare Docker - name: Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - + # 6. Build and Push (Only if tests passed) - name: Build and Push Image uses: docker/build-push-action@v5 with: - context: . - file: ./Dockerfile - push: true - + context: . + file: ./Dockerfile + push: true tags: | ${{ secrets.DOCKERHUB_USERNAME }}/docker-test:latest ${{ secrets.DOCKERHUB_USERNAME }}/docker-test:${{ github.sha }} \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 60f7cae..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Java CI with Docker - -on: - push: - branches: [ "main" ] - -jobs: - build-and-push: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Build and Push Image - uses: docker/build-push-action@v5 - with: - context: . - file: ./Dockerfile - push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/docker-test:latest \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 33c9b15..3e02e8e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,6 @@ services: volumes: - /var/run/docker.sock:/var/run/docker.sock environment: - # If your Docker Hub repo is private, you need these: - REPO_USER=${DOCKERHUB_USERNAME} - REPO_PASS=${DOCKERHUB_TOKEN} command: --interval 30 --cleanup # Check for updates every 30 seconds \ No newline at end of file diff --git a/dockerfile b/dockerfile index 6160cc8..e028eb7 100644 --- a/dockerfile +++ b/dockerfile @@ -29,5 +29,4 @@ USER appuser COPY --from=build /app/ci-cd-docker-github/target/*.jar app.jar # Standard Java 25 execution -# Optional: Added flags for better container memory management ENTRYPOINT ["java", "-XX:+UseContainerSupport", "-Xmx512m", "-jar", "app.jar"] \ No newline at end of file diff --git a/pom.xml b/pom.xml index b68e328..9537832 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,46 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + prepare-agent + + prepare-agent + + + + report + test + + report + + + + check-coverage + + check + + + + + BUNDLE + + + LINE + COVEREDRATIO + 0.80 + + + + + + + + org.springframework.boot spring-boot-maven-plugin diff --git a/src/main/java/com/dhurba/docker/Application.java b/src/main/java/com/dhurba/docker/Application.java index 6331ab4..71a70e6 100644 --- a/src/main/java/com/dhurba/docker/Application.java +++ b/src/main/java/com/dhurba/docker/Application.java @@ -5,7 +5,7 @@ @SpringBootApplication public class Application { - static void main(String[] args) { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } } diff --git a/src/test/java/com/dhurba/docker/service/DummyServiceTest.java b/src/test/java/com/dhurba/docker/service/DummyServiceTest.java index 1648ce0..2e995f5 100644 --- a/src/test/java/com/dhurba/docker/service/DummyServiceTest.java +++ b/src/test/java/com/dhurba/docker/service/DummyServiceTest.java @@ -28,7 +28,7 @@ void fetchAll() { when(modelRepo.findAll()).thenReturn(Collections.singletonList(createModel())); List modelDtos = dummyService.fetchAll(); verify(modelRepo).findAll(); - assert modelDtos.size() == 9; + assert modelDtos.size() == 1; } private Model createModel() {