From 4dc21cb1ff8b57b666b1b8315c8bdc4279eb059f Mon Sep 17 00:00:00 2001 From: Pedro Aguiar Date: Mon, 30 Sep 2024 21:39:02 -0400 Subject: [PATCH] build: refine workflow configuration - Set workflow timeout for better job control - Implement Maven dependency caching for faster builds - Use Maven Wrapper (mvnw) to prevent development environment discrepancies - Enable batch mode in Maven to reduce log clutter from download progress - Trigger workflow on push and pull request events for the master branch - Replace fixed 10-second sleep with a dynamic readiness check for the app Signed-off-by: Pedro Aguiar --- .github/workflows/maven.yml | 109 ++++++++++++++++++++++++++++++------ README.md | 18 ++++++ pom.xml | 4 ++ 3 files changed, 114 insertions(+), 17 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 9d3eede..905dd7d 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -7,30 +7,105 @@ on: jobs: container-job: runs-on: ubuntu-latest - services: - postgres: - image: postgres:17.0-alpine - ports: - - 5432:5432 - env: - POSTGRES_PASSWORD: 123456 + timeout-minutes: 8 + steps: - - uses: actions/checkout@v4 - - name: Set up JDK 21 + - name: Checkout code + uses: actions/checkout@v4 + + - name: Make Maven Wrapper executable + run: chmod +x ./mvnw + + - name: Create Maven config + run: | + mkdir -p .mvn + echo "--batch-mode" > .mvn/maven.config + + - name: Set up JDK 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: Cache Maven dependencies + uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-maven- + + - name: Cache build artifacts + id: cache-build + uses: actions/cache@v4 + with: + path: target + key: ${{ runner.os }}-build-${{ hashFiles('src/**/*.java', '**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-build- + + - name: Compile project + if: steps.cache-build.outputs.cache-hit != 'true' + run: ./mvnw compile + + - name: Run integration tests + if: steps.cache-build.outputs.cache-hit != 'true' + run: ./mvnw verify + + - name: Package project + if: steps.cache-build.outputs.cache-hit != 'true' + run: ./mvnw package -DskipTests + + - name: Start and wait for project readiness + if: steps.cache-build.outputs.cache-hit != 'true' + run: | + nohup java -jar target/quarkus-app/quarkus-run.jar & + + count_requests=0 + MAX_REQUEST_ATTEMPTS=15 + HEALTH_CHECK_ENDPOINT="http://localhost:8080/q/health/ready" + + while [ "$count_requests" -lt "$MAX_REQUEST_ATTEMPTS" ]; do + count_requests=$((count_requests+1)) + + # Allows curl failures without stopping the script + set +e + http_code=$(curl "$HEALTH_CHECK_ENDPOINT" \ + --silent \ + --output /dev/null \ + --write-out "%{http_code}") + set -e + + curl_exit_code=$? + + if [ $curl_exit_code -ne 0 ]; then + echo "Attempt ($count_requests/$MAX_REQUEST_ATTEMPTS): curl failed with exit code $curl_exit_code (could not connect)" + fi + + if [ $curl_exit_code -eq 0 ]; then + echo "Attempt ($count_requests/$MAX_REQUEST_ATTEMPTS): HTTP $http_code" + fi + + if [ "$http_code" -eq 200 ]; then + echo "Project is ready!" + exit 0 + fi + + sleep 1 + done + + echo "Max attempts ($count_requests/$MAX_REQUEST_ATTEMPTS) reached." + exit 1 + - name: Setup K6 + if: steps.cache-build.outputs.cache-hit != 'true' uses: grafana/setup-k6-action@v1 - - name: Start app - shell: bash - run: | - nohup java -jar target/quarkus-app/quarkus-run.jar & sleep 10s + - name: Run local k6 test + if: steps.cache-build.outputs.cache-hit != 'true' uses: grafana/run-k6-action@v1 with: - path: e2e/api-test.js + path: ./e2e/api-test.js + # Dummy token for nektos/act. Can't run this step locally without it + github-token: ${{ github.token || 'dummy-token' }} + cloud-run-locally: ${{ github.token == 'dummy-token' && 'false' || 'true' }} + cloud-comment-on-pr: ${{ github.token == 'dummy-token' && 'true' || 'false' }} diff --git a/README.md b/README.md index 0260cfe..91474a8 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,13 @@ infrastructure/ -> technical details layer # Getting started + +### Grant execute permissions to the Maven wrapper + +```shell +chmod +x ./mvnw +``` + ### Start local server ```shell @@ -57,6 +64,17 @@ The server should be running at http://localhost:8080 ### Running postman collection tests +1. (Recommended) Using [nektos/act](https://github.com/nektos/act): + +> [!IMPORTANT] +> After cloning the repository, make sure to run `dos2unix ./mvnw` if you're using Git Bash. Otherwise, Act might throw an `Error 126`. + +```shell +act --cache-server-addr host.docker.internal +``` + +2. Using the local script: + ```shell ./collections/run-api-tests.sh ``` diff --git a/pom.xml b/pom.xml index 4cdea8e..80bc27a 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,10 @@ quarkus-junit5 test + + io.quarkus + quarkus-smallrye-health + io.quarkus quarkus-resteasy-jackson