This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/scripts/ci
2019-03-11 14:42:27 -04:00

184 lines
3.3 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
IMAGE="quay-ci"
CACHE_DIR="${HOME}/docker"
SHORT_SHA="${TRAVIS_COMMIT:0:7}"
IMAGE_TAG="${SHORT_SHA}-${TRAVIS_BUILD_NUMBER}"
IMAGE_TAR="${CACHE_DIR}/${IMAGE}-${IMAGE_TAG}.tar.gz"
MYSQL_IMAGE="mysql:5.7"
POSTGRES_IMAGE="postgres:9.6"
POSTGRES_CONTAINER="test_postgres"
export MYSQL_ROOT_PASSWORD="quay"
export MYSQL_USER="quay"
export MYSQL_PASSWORD="quay"
export MYSQL_DATABASE="quay_ci"
export POSTGRES_USER="quay"
export POSTGRES_PASSWORD="quay"
export POSTGRES_DB="quay_ci"
build_image() {
# Build the image and save it to the shared cache.
docker build -t "${IMAGE}:${IMAGE_TAG}" .
echo "Exporting Docker image to cache..."
time (docker save "${IMAGE}:${IMAGE_TAG}" | gzip -2 > "${IMAGE_TAR}")
}
load_image() {
# Load our cached Docker image.
echo "Loading Docker image from cache..."
time (zcat "${IMAGE_TAR}" | docker load)
}
clean_cache() {
rm "${IMAGE_TAR}"
}
fail_clean() {
if [[ $TRAVIS_TEST_RESULT -ne 0 ]]; then
echo "Job failed. Cleaning cache..."
clean_cache
fi
}
quay_run() {
docker run --net=host -e TEST_DATABASE_URI -ti "${IMAGE}:${IMAGE_TAG}" "$@"
}
unit() {
load_image && quay_run make unit-test
}
registry() {
load_image && quay_run make registry-test
}
registry_old() {
load_image && quay_run make registry-test-old
}
certs_test() {
load_image && quay_run make certs-test
}
mysql_ping() {
mysqladmin --connect-timeout=2 --wait=60 --host=127.0.0.1 \
--user=root --password="${MYSQL_ROOT_PASSWORD}" ping
}
mysql_start() {
docker run --net=host -d -e MYSQL_ROOT_PASSWORD -e MYSQL_USER \
-e MYSQL_PASSWORD -e MYSQL_DATABASE "${MYSQL_IMAGE}"
if ! (sleep 20 && mysql_ping); then
echo "MySQL failed to respond in time."
exit 1
fi
}
mysql() {
TEST_DATABASE_URI="mysql+pymysql://"
TEST_DATABASE_URI+="${MYSQL_USER}:${MYSQL_PASSWORD}"
TEST_DATABASE_URI+="@127.0.0.1/${MYSQL_DATABASE}"
export TEST_DATABASE_URI
load_image
mysql_start
quay_run make full-db-test
}
postgres_ping() {
pg_isready --timeout=30 --dbname="${TEST_DATABASE_URI}"
}
postgres_start() {
docker run --name="${POSTGRES_CONTAINER}" --net=host -d -e POSTGRES_USER -e POSTGRES_PASSWORD \
-e POSTGRES_DB "${POSTGRES_IMAGE}"
if ! (sleep 10 && postgres_ping); then
echo "PostgreSQL failed to respond in time."
exit 1
fi
}
postgres_init() {
docker exec "${POSTGRES_CONTAINER}" psql -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" -c 'CREATE EXTENSION IF NOT EXISTS pg_trgm;'
}
postgres() {
TEST_DATABASE_URI="postgresql://"
TEST_DATABASE_URI+="${POSTGRES_USER}:${POSTGRES_PASSWORD}"
TEST_DATABASE_URI+="@127.0.0.1/${POSTGRES_DB}"
export TEST_DATABASE_URI
load_image
postgres_start
postgres_init
quay_run make full-db-test
}
case "$1" in
build)
build_image
;;
unit)
unit
;;
registry)
registry
;;
registry_old)
registry_old
;;
certs_test)
certs_test
;;
mysql)
mysql
;;
postgres)
postgres
;;
fail-clean)
fail_clean
;;
clean)
clean_cache
;;
*)
echo "Usage: $0 {build|unit|registry|registry_old|mysql|postgres|clean}"
exit 1
;;
esac