Skip to content

Commit

Permalink
Adding setup-postgres action
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami committed Nov 20, 2024
1 parent 7cf11d0 commit 78e20c4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/run-e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ jobs:
- name: Checkout code
uses: actions/[email protected]
- name: Setup postgres
uses: actions/setup-postgres
uses: ../../actions/setup-postgres
- name: Setup Go
uses: actions/[email protected]
with:
go-version: "1.22.6"
check-latest: true
- name: Setup DB
run: |
# Build binary
Expand Down
18 changes: 18 additions & 0 deletions actions/setup-postgres/action.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions actions/setup-postgres/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions actions/setup-postgres/wait-for-healthy-postgres.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 78e20c4

Please sign in to comment.