Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SS-1820: fixed pgsql client #2

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build image

on:
pull_request:
branches:
- master
push:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
MAIN_GIT_REF: refs/heads/master

jobs:
image:
name: Build docker image
runs-on: [ self-hosted, bear-ephemeral ]
steps:
- name: Set Up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Checkout
uses: actions/checkout@v2

- name: Build And Push Image (Pull Request)
if: ${{ github.ref != env.MAIN_GIT_REF }}
uses: docker/build-push-action@v3
with:
push: true
tags: |
${{ secrets.ECR_REGISTRY }}/pgsql-client:${{ github.run_id }}

- name: Build And Push Image (Main Branch)
if: ${{ github.ref == env.MAIN_GIT_REF }}
uses: docker/build-push-action@v3
with:
context: migrations
push: true
tags: |
${{ secrets.ECR_REGISTRY }}/pgsql-client:${{ github.run_id }}
${{ secrets.ECR_REGISTRY }}/pgsql-client:latest
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.7
FROM 567716553783.dkr.ecr.us-east-1.amazonaws.com/alpine:3.8
RUN apk --update add postgresql-client && rm -rf /var/cache/apk/*
COPY . /
ENTRYPOINT ["./entrypoint.sh"]
27 changes: 27 additions & 0 deletions createdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env sh

set -e

DB_HOST=${DB_HOST:?}
DB_PORT=${DB_PORT:?}
DB_NAME=${DB_NAME:?}
DB_USER=${DB_USER:?}
DB_PASS=${DB_PASS:?}
DB_TIMEOUT=${DB_TIMEOUT:-10}

# wait for db to be ready
for i in $(seq $DB_TIMEOUT) ; do
if ! nc -z $DB_HOST $DB_PORT > /dev/null 2>&1; then
sleep 1
else
break
fi
done

# create auth file without db as it does not exist
echo "${DB_HOST}":"${DB_PORT}":*:"${DB_USER}":"${DB_PASS}" > ~/.pgpass
chmod 0600 ~/.pgpass

# run database query using shell workaround for \gexec
# see https://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql
echo "SELECT 'CREATE DATABASE ${DB_NAME}' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec" | psql -h ${DB_HOST} -U ${DB_USER} -w
29 changes: 25 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
#!/bin/sh
echo "${DB_HOST}":*:*:"${DB_USER}":"${DB_PASS}" > ~/.pgpass
#!/usr/bin/env sh

set -e

DB_HOST=${DB_HOST:?}
DB_PORT=${DB_PORT:?}
DB_NAME=${DB_NAME:?}
DB_USER=${DB_USER:?}
DB_PASS=${DB_PASS:?}
DB_TIMEOUT=${DB_TIMEOUT:-10}

# wait for db to be ready
for i in $(seq $DB_TIMEOUT) ; do
if ! nc -z $DB_HOST $DB_PORT > /dev/null 2>&1; then
sleep 1
else
break
fi
done

# create auth file
echo "${DB_HOST}":"${DB_PORT}":"${DB_NAME}":"${DB_USER}":"${DB_PASS}" > ~/.pgpass
chmod 0600 ~/.pgpass
psql -h ${DB_HOST} -U ${DB_USER} -w postgres -c "$@"
exit 0

# run database query
psql -h ${DB_HOST} -U ${DB_USER} -w ${DB_NAME} -c "$@"