diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 5096758ea5..def29cb1ae 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -39,6 +39,12 @@ jobs: run: | chmod +x ./.github/workflows/countline.py ./.github/workflows/countline.py --lines 600 --exclude_files src/screens/LoginPage/LoginPage.tsx src/GraphQl/Queries/Queries.ts src/screens/OrgList/OrgList.tsx src/GraphQl/Mutations/mutations.ts src/components/EventListCard/EventListCardModals.tsx src/components/TagActions/TagActionsMocks.ts src/utils/interfaces.ts src/screens/MemberDetail/MemberDetail.tsx + + + - name: Make healthcheck.sh executable + run: | + git update-index --chmod=+x .github/workflows/scripts/healthcheck.sh + - name: Get changed TypeScript files id: changed-files @@ -304,22 +310,12 @@ jobs: run: | npm run preview & echo $! > .pidfile_prod + + - name: Check if Production App is running run: | - timeout=120 - echo "Starting production health check with ${timeout}s timeout" - while ! nc -z localhost 4173 && [ $timeout -gt 0 ]; do - sleep 1 - timeout=$((timeout-1)) - if [ $((timeout % 10)) -eq 0 ]; then - echo "Still waiting for production app to start... ${timeout}s remaining" - fi - done - if [ $timeout -eq 0 ]; then - echo "Timeout waiting for production application to start" - exit 1 - fi - echo "Production app started successfully" + .github/workflows/scripts/healthcheck.sh 4173 120 + echo "Production app started successfully" - name: Stop Production App run: | if [ -f .pidfile_prod ]; then @@ -329,22 +325,14 @@ jobs: run: | npm run serve & echo $! > .pidfile_dev + + + - name: Check if Development App is running run: | - timeout=120 - echo "Starting development health check with ${timeout}s timeout" - while ! nc -z localhost 4321 && [ $timeout -gt 0 ]; do - sleep 1 - timeout=$((timeout-1)) - if [ $((timeout % 10)) -eq 0 ]; then - echo "Still waiting for development app to start... ${timeout}s remaining" - fi - done - if [ $timeout -eq 0 ]; then - echo "Timeout waiting for development application to start" - exit 1 - fi + .github/workflows/scripts/healthcheck.sh 4321 120 echo "Development app started successfully" + - name: Stop Development App if: always() run: | @@ -377,30 +365,20 @@ jobs: echo "Started Docker container..." docker run -d --name talawa-admin-app-container -p 4321:4321 talawa-admin-app echo "Docker container started successfully" + + - name: Check if Talawa Admin App is running run: | - timeout="${HEALTH_CHECK_TIMEOUT:-120}" - echo "Starting health check with ${timeout}s timeout" - while ! nc -z localhost 4321 && [ $timeout -gt 0 ]; do - sleep 1 - timeout=$((timeout-1)) - if [ $((timeout % 10)) -eq 0 ]; then - echo "Still waiting for app to start... ${timeout}s remaining" - fi - done - if [ $timeout -eq 0 ]; then - echo "Timeout waiting for application to start" - echo "Container logs:" - docker logs talawa-admin-app-container - exit 1 - fi + .github/workflows/scripts/healthcheck.sh 4321 120 echo "Port check passed, verifying health endpoint..." + - name: Stop Docker Container if: always() run: | docker stop talawa-admin-app-container docker rm talawa-admin-app-container + Check-Target-Branch: if: ${{ github.actor != 'dependabot[bot]' }} name: Check Target Branch diff --git a/.github/workflows/scripts/healthcheck.sh b/.github/workflows/scripts/healthcheck.sh new file mode 100644 index 0000000000..1b4f1e78de --- /dev/null +++ b/.github/workflows/scripts/healthcheck.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +# Validate input parameters +if [ -z "$1" ]; then + echo "Error: Port number is required" + echo "Usage: $0 [timeout]" + exit 1 +fi + +if ! [[ "$1" =~ ^[0-9]+$ ]] || [ "$1" -lt 1 ] || [ "$1" -gt 65535 ]; then + echo "Error: Invalid port number. Must be between 1 and 65535" + exit 1 +fi + +if [ ! -z "$2" ] && ! [[ "$2" =~ ^[0-9]+$ ]]; then + echo "Error: Timeout must be a positive number" + exit 1 +fi + +port="$1" +timeout="${2:-120}" +declare -a TEMP_FILES=() + +# Function to check if the app is running +check_health() { + local port=$1 + local timeout=$2 + if ! command -v nc >/dev/null 2>&1; then + echo "Error: netcat (nc) is not installed" + return 2 + fi + + local status=3 + while ! nc -z localhost "${port}" && [ "${timeout}" -gt 0 ]; do + sleep 1 + timeout=$((timeout - 1)) + if [ $((timeout % 10)) -eq 0 ]; then + echo "Still waiting for app to start on port ${port}... ${timeout}s remaining" + fi + done + + if nc -z localhost "${port}" >/dev/null 2>&1; then + status=0 + fi + return $status +} + +# Default container name, can be overridden by environment variable +CONTAINER_NAME="${CONTAINER_NAME:-talawa-admin-app-container}" + +# Cleanup function +cleanup() { + echo "Cleaning up..." + local exit_code="${1:-1}" + local cleanup_failed=0 + # Kill any background processes started by this script + if ! jobs -p | xargs -r kill 2>/dev/null; then + echo "Warning: Failed to kill some background processes" + cleanup_failed=1 + fi + # Remove any temporary files if created + if [ ${#TEMP_FILES[@]} -gt 0 ]; then + if ! rm -f "${TEMP_FILES[@]}" 2>/dev/null; then + echo "Warning: Failed to remove some temporary files" + cleanup_failed=1 + fi + fi + + [ $cleanup_failed -eq 1 ] && exit_code=1 + exit $exit_code +} + +# Set up signal handling +trap 'cleanup' SIGINT SIGTERM SIGHUP EXIT + +# Function to handle timeout +handle_timeout() { + echo "Timeout waiting for application to start" + if [ -f /.dockerenv ]; then + echo "Container logs:" + docker logs "${CONTAINER_NAME}" 2>/dev/null || echo "Failed to retrieve container logs" + fi + cleanup 1 +} + +# Check if the script is running inside Docker container +if [ -f /.dockerenv ]; then + echo "Running inside Docker container" + check_health "${port}" "${timeout}" || handle_timeout +else + echo "Running outside Docker container" + check_health || handle_timeout +fi