From 45e0aa09cb3ebdbb150a6571984e624e6123b46f Mon Sep 17 00:00:00 2001 From: vysk Date: Fri, 3 Jan 2025 00:42:47 +0400 Subject: [PATCH] build: add CI for bruno-cli api tests --- cms/package.json | 3 ++- cms/run-tests.sh | 57 +++++++++++++++++++++++++++++++++++++++ dev-config/Dockerfile.cms | 2 +- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 cms/run-tests.sh diff --git a/cms/package.json b/cms/package.json index 120c93c..2b114e0 100644 --- a/cms/package.json +++ b/cms/package.json @@ -33,7 +33,8 @@ "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", - "typescript": "^5" + "typescript": "^5", + "@usebruno/cli": "^1.36.3" }, "engines": { "node": ">=18.0.0 <=22.x.x", diff --git a/cms/run-tests.sh b/cms/run-tests.sh new file mode 100644 index 0000000..268de82 --- /dev/null +++ b/cms/run-tests.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Constants +HEALTH_CHECK_URL="http://localhost:1337/_health" +API_COLLECTION_PATH="../bruno-church-api-collection" +WAIT_FOR_START=20 +MAX_RETRIES=30 +RETRY_INTERVAL=4 + +# Function to run tests based on environment +run_tests() { + echo "Running tests in $NODE_ENV mode..." + cd $API_COLLECTION_PATH + + if [ "$NODE_ENV" == "development" ]; then + bru run --env local-environment + elif [ "$NODE_ENV" == "production" ]; then + bru run --env prod-environment + else + echo "NODE_ENV is not set or has an invalid value. Please set it to 'development' or 'production'." + exit 1 + fi +} + +# Function to check health container +check_health() { + local retry_count=0 + + while [ $retry_count -lt $MAX_RETRIES ]; do + if curl -f $HEALTH_CHECK_URL &>/dev/null; then + echo "Health check passed" + return 0 + fi + + echo "Waiting for service to be healthy... (Attempt $((retry_count + 1))/$MAX_RETRIES)" + sleep $RETRY_INTERVAL + retry_count=$((retry_count + 1)) + done + + echo "Health check failed after $MAX_RETRIES attempts" + return 1 +} + +# Main execution +echo "Current NODE_ENV: $NODE_ENV" + +if [ "$1" == "docker" ]; then + echo "Docker mode detected, performing health check..." + sleep $WAIT_FOR_START + if check_health; then + run_tests + else + exit 1 + fi +else + run_tests +fi diff --git a/dev-config/Dockerfile.cms b/dev-config/Dockerfile.cms index 30a76e5..7ee3bd4 100644 --- a/dev-config/Dockerfile.cms +++ b/dev-config/Dockerfile.cms @@ -24,4 +24,4 @@ RUN echo "ls -la" RUN ls -la /usr/app RUN bash -c "chmod 777 run.sh;" -CMD ./run.sh +CMD bash -c "./run.sh & ./run-tests.sh docker"