Files
dexorder/bin/build.sh
2024-03-21 19:39:21 -04:00

87 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
if [ "$1" != "backend" ] && [ "$1" != "server" ] && [ "$1" != "web" ]; then
echo
echo usage: "$0 "'{backend|server|web} [''dev''] [config] [deployment] [kubernetes]'
echo
echo ' [''dev''] if the literal string ''dev'' is not the second argument, then the build refuses to run if source code is not checked in. Otherwise, the git revision numbers are used in the image tag.'
echo
echo ' [config] is used to find configuration files e.g. web/.env.{config} or backend/dexorder-{config}.toml. Defaults to project name.'
echo
echo ' [deployment] refers to the set of contracts bundled in the image, from contract/deployment/{deployment}. Defaults to config name.'
echo
echo ' [kubernetes] is used for the base image name and also to find the yaml file for deployment: deploy/k8s/{kubernetes}.yaml. Defaults to project name.'
echo
exit 1
else
PROJECT=$1
shift
fi
if [ "$1" == "dev" ]; then
echo A
shift
TAG="dev`date +%Y%m%d%H%M%S`"
if [ "$1" != "" ]; then
CONFIG=$1
shift
else
CONFIG=dev
fi
else
CONFIG=$1
shift
DIRTY="$( cd $PROJECT && git status | grep "Changes " )"
if [ "$DIRTY" != "" ]; then
echo $PROJECT has uncommited changes.
echo
echo Use \`$0 $PROJECT dev\` to deploy a development-tagged version instead.
exit 1
fi
DIRTY="$( cd contract && git status | grep "Changes " )"
if [ "$DIRTY" != "" ]; then
echo contract has uncommited changes.
echo
echo Use \`$0 $PROJECT dev\` to deploy a development-tagged version instead.
exit 1
fi
TAG="$( cd $PROJECT && git log --oneline | head -1 | cut -d ' ' -f 1 )-$( cd contract && git log --oneline | head -1 | cut -d ' ' -f 1 )"
fi
if [ "$1" != "" ]; then
DEPLOYMENT=$1
shift
else
DEPLOYMENT=$CONFIG
fi
if [ "$1" != "" ]; then
KUBERNETES=$1
shift
else
KUBERNETES=$PROJECT
fi
if [ $(basename "$0") == 'deploy.sh' ]; then
DEPLOY=1
else
DEPLOY=0
fi
REMOTE=gcr.io/cointrader-211623/dexorder
# todo nocache as a script option
#docker build --no-cache -f deploy/Dockerfile-$PROJECT -t dexorder/$PROJECT:latest . || exit 1
echo Building $PROJECT config=$CONFIG deployment=$DEPLOYMENT '=>' $TAG
docker build -f deploy/docker/Dockerfile-$PROJECT --build-arg="CONFIG=$CONFIG" --build-arg="DEPLOYMENT=$DEPLOYMENT" -t dexorder/$KUBERNETES:latest . || exit 1
docker tag dexorder/$KUBERNETES:latest dexorder/$KUBERNETES:$TAG
docker tag dexorder/$KUBERNETES:$TAG $REMOTE/$KUBERNETES:$TAG
docker tag $REMOTE/$KUBERNETES:latest $REMOTE/$KUBERNETES:$TAG
echo "$(date)" built $REMOTE/$KUBERNETES:$TAG
if [ "$DEPLOY" != "0" ]; then
docker push $REMOTE/$KUBERNETES:$TAG $REMOTE/$KUBERNETES:latest
sed "s#dexorder/$KUBERNETES*#$REMOTE#" deploy/k8s/$KUBERNETES.yaml | kubectl apply -f - || (echo kubectl apply failed && exit 1)
echo deployed $KUBERNETES.yaml $REMOTE/$KUBERNETES:$TAG
fi