From 78e20c497bd4a244b3235578b8ab99599be8f96f Mon Sep 17 00:00:00 2001 From: Balamurali Gopalswami Date: Wed, 20 Nov 2024 16:24:33 -0500 Subject: [PATCH] Adding setup-postgres action --- .github/workflows/run-e2e-tests.yml | 7 +++++- actions/setup-postgres/action.yml | 18 +++++++++++++ actions/setup-postgres/docker-compose.yml | 15 +++++++++++ .../wait-for-healthy-postgres.sh | 25 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 actions/setup-postgres/action.yml create mode 100644 actions/setup-postgres/docker-compose.yml create mode 100755 actions/setup-postgres/wait-for-healthy-postgres.sh diff --git a/.github/workflows/run-e2e-tests.yml b/.github/workflows/run-e2e-tests.yml index 4444e4d0..300f9a06 100644 --- a/.github/workflows/run-e2e-tests.yml +++ b/.github/workflows/run-e2e-tests.yml @@ -337,7 +337,12 @@ jobs: - name: Checkout code uses: actions/checkout@v4.2.1 - name: Setup postgres - uses: actions/setup-postgres + uses: ../../actions/setup-postgres + - name: Setup Go + uses: actions/setup-go@v5.0.2 + with: + go-version: "1.22.6" + check-latest: true - name: Setup DB run: | # Build binary diff --git a/actions/setup-postgres/action.yml b/actions/setup-postgres/action.yml new file mode 100644 index 00000000..f1dc5ddd --- /dev/null +++ b/actions/setup-postgres/action.yml @@ -0,0 +1,18 @@ +name: Setup Postgresql +description: Setup postgres docker container via docker-compose, allowing usage of a custom command, see https://github.com/orgs/community/discussions/26688 +inputs: + base-path: + description: Path to the base of the repo + required: false + default: . +runs: + using: composite + steps: + - name: Start postgres service + run: docker compose up -d + shell: bash + working-directory: ${{ inputs.base-path }}/actions/setup-postgres + - name: Wait for postgres service to be healthy + run: ./wait-for-healthy-postgres.sh + shell: bash + working-directory: ${{ inputs.base-path }}/actions/setup-postgres diff --git a/actions/setup-postgres/docker-compose.yml b/actions/setup-postgres/docker-compose.yml new file mode 100644 index 00000000..23f8d82b --- /dev/null +++ b/actions/setup-postgres/docker-compose.yml @@ -0,0 +1,15 @@ +name: gha_postgres +services: + postgres: + ports: + - "5432:5432" + container_name: cl_pg + image: postgres:14-alpine + command: postgres ${POSTGRES_OPTIONS} + env_file: + - .env + healthcheck: + test: "pg_isready -d ${POSTGRES_DB} -U ${POSTGRES_USER}" + interval: 2s + timeout: 5s + retries: 5 diff --git a/actions/setup-postgres/wait-for-healthy-postgres.sh b/actions/setup-postgres/wait-for-healthy-postgres.sh new file mode 100755 index 00000000..438cfbaf --- /dev/null +++ b/actions/setup-postgres/wait-for-healthy-postgres.sh @@ -0,0 +1,25 @@ +#!/bin/bash +RETRIES=10 + +until [ $RETRIES -eq 0 ]; do + DOCKER_OUTPUT=$(docker compose ps postgres --status running --format json) + JSON_TYPE=$(echo "$DOCKER_OUTPUT" | jq -r 'type') + + if [ "$JSON_TYPE" == "array" ]; then + HEALTH_STATUS=$(echo "$DOCKER_OUTPUT" | jq -r '.[0].Health') + elif [ "$JSON_TYPE" == "object" ]; then + HEALTH_STATUS=$(echo "$DOCKER_OUTPUT" | jq -r '.Health') + else + HEALTH_STATUS="Unknown JSON type: $JSON_TYPE" + fi + + echo "postgres health status: $HEALTH_STATUS" + if [ "$HEALTH_STATUS" == "healthy" ]; then + exit 0 + fi + + echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..." + sleep 2 +done + +exit 1