diff --git a/.github/scripts/check-container-health.sh b/.github/scripts/check-container-health.sh new file mode 100755 index 0000000..2ea9e68 --- /dev/null +++ b/.github/scripts/check-container-health.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -e + +echo "Checking the health of the containers..." + +SERVICES="$1" + +for service in $SERVICES; do + running=$(docker inspect -f '{{.State.Running}}' "$service" 2>/dev/null || echo "false") + + if [ "$running" = "true" ]; then + echo "$service is running ✔️" + else + exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$service" 2>/dev/null || echo "1") + if [ "$exit_code" = "0" ]; then + echo "$service is stopped but exited with code 0 ✔️" + else + echo "::error ::$service is stopped and exited with code $exit_code ❌" + exit 1 + fi + fi +done diff --git a/.github/workflows/spring-kafka-example.yml b/.github/workflows/spring-kafka-example.yml index fd6fed4..80469a1 100644 --- a/.github/workflows/spring-kafka-example.yml +++ b/.github/workflows/spring-kafka-example.yml @@ -65,22 +65,4 @@ jobs: - name: Check container health run: | - echo "Checking the health of the containers..." - - SERVICES="${{ steps.services.outputs.services }}" - - for service in $SERVICES; do - running=$(docker inspect -f '{{.State.Running}}' "$service" 2>/dev/null || echo "false") - - if [ "$running" = "true" ]; then - echo "$service is running ✔️" - else - exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$service" 2>/dev/null || echo "1") - if [ "$exit_code" = "0" ]; then - echo "$service is stopped but exited with code 0 ✔️" - else - echo "::error ::$service is stopped and exited with code $exit_code ❌" - exit 1 - fi - fi - done + ./.github/scripts/check-container-health.sh "${{ steps.services.outputs.services }}" diff --git a/.github/workflows/spring-prometheus-grafana-example.yml b/.github/workflows/spring-prometheus-grafana-example.yml index 43228ce..45ddbaa 100644 --- a/.github/workflows/spring-prometheus-grafana-example.yml +++ b/.github/workflows/spring-prometheus-grafana-example.yml @@ -65,14 +65,4 @@ jobs: - name: Check container health run: | - echo "Checking the health of the containers..." - - SERVICES="${{ steps.services.outputs.services }}" - for service in $SERVICES; do - status=$(docker inspect -f '{{.State.Running}}' "$service" || echo "false") - echo "$service status: $status" - if [ "$status" != "true" ]; then - echo "::error ::Service $service is not running." - exit 1 - fi - done \ No newline at end of file + ./.github/scripts/check-container-health.sh "${{ steps.services.outputs.services }}" \ No newline at end of file