Commit c4821344 authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Added dockerized database and deploy stage

parent 86bde927
Pipeline #27190 failed with stage
in 34 seconds
image: python:3-buster image: debian:buster-slim
stages:
- test
- deploy
test: test:
before_script: before_script:
...@@ -14,9 +18,28 @@ test: ...@@ -14,9 +18,28 @@ test:
interruptible: true interruptible: true
script: script:
- | - run-migrations.sh -u postgres -h postgres
for file in `ls migrations/*` # - |
do # for file in `ls migrations/*`
echo "importing $file" # do
psql -v ON_ERROR_STOP=1 -h postgres -U postgres -f $file # echo "importing $file"
done # psql -v ON_ERROR_STOP=1 -h postgres -U postgres -f $file
# done
deploy:
image: docker:19.03.12
services:
- docker:19.03.12-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
tags:
- dind
script:
- docker build --pull -t $IMAGE_TAG .
- docker push $IMAGE_TAG
# only:
# refs:
# - master
FROM $CI_REGISTRY/dynamicexposure/server-components/containers/docker-obm-database:master
COPY . .
RUN run-migrations.sh -u postgres -h postgres
SOURCES=./run-migrations.sh
check: $(SOURCES)
shellcheck --external-sources $^
shfmt -i 2 -d $^
format: $(SOURCES)
shfmt -i 2 -w $^
#!/bin/bash
#
# Deletes current `obm_tiles` database and sets it up anew by running
# all SQL scripts in the migrations folder in ascending order.
# Run: script_name [db_user]
# May provide the database user as an argument (default: postgres).
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
sql_scripts_dir='migrations'
db_name='obm_tiles'
db_user='postgres'
db_hostname='localhost'
# Test for argument from the command line (now only one argument: database user)
while getopts d:u:h: flag
do
case "${flag}" in
d) db_name=${OPTARG};;
u) db_user=${OPTARG};;
h) db_hostname=${OPTARG};;
esac
done
printf 'Running %s with database user %s on %s table %s \n' "$(basename "$0")" "${db_user}" "${db_hostname}" "${db_name}"
# Delete current `obm_tiles` database if exists and create anew
if psql -lqt | cut -d \| -f 1 | grep -qw ${db_name}; then
# Database exists
printf 'Database %s exists, deleting.\n' ${db_name}
dropdb "${db_name}"
printf 'Creating %s\n' ${db_name}
createdb "${db_name}"
else
createdb "${db_name}"
fi
# Run the SQL scripts in the migrations folder in ascending order
for file in "${sql_scripts_dir}"/*.sql; do
f_base=$(basename "${file}" .sql)
printf 'Running script %s\n' "${f_base}"
psql -v ON_ERROR_STOP=1 \
--username "${db_user}" \
--dbname "${db_name}" \
--hostname "${db_hostname}" \
--echo-all --file "${file}"
done
printf 'Done.\n'
exit 0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment