-
-
Save konung/8837a40fabf27e093113f5747b84ef0f to your computer and use it in GitHub Desktop.
Our production deployment setup for deploying Lucky on to Elastic Beanstalk in production.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We develop locally on MacOS. Due to some issues with cross compilation, we build the crystal binary on docker locally, | |
then zip that up and ship that along with the docker stuff to elasticbeanstalk. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM crystallang/crystal:0.27.0 | |
ADD . /app | |
ADD ./docker/docker_run_build.sh /app/docker_run_build.sh | |
WORKDIR /app | |
RUN shards update && \ | |
rm -rf /app/build/app && \ | |
crystal build src/server.cr --release -o app | |
RUN chmod +x docker_run_build.sh | |
CMD ["./docker_run_build.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# dont execute next commands on error | |
trap 'exit' ERR | |
# make sure a local server isn't currently running | |
if pgrep -x "lucky" > /dev/null | |
then | |
echo -e "\033[31mLucky is currently running and must be stopped before deploying\033[0m" | |
exit 1 | |
fi | |
if (lsof -i:5000 | grep "crystal") > /dev/null | |
then | |
echo -e "\033[31mCrystal is running on port 5000.\033[0m" | |
echo -e "\033[31mStop the process before deploying.\033[0m" | |
exit 1 | |
fi | |
# let echo interpret escape chars (\n) | |
shopt -s xpg_echo | |
START=`date +%s` | |
DATE=`date '+%Y%m%d@%H%M%S'` | |
BUILD_DIR=build-temp-${DATE} | |
SHA1=`git rev-parse HEAD` | |
RANDOM=`awk -v min=5 -v max=1000000 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'` | |
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p') | |
if [ "$branch" == "master" ] | |
then | |
STAGE=production | |
else | |
STAGE=staging | |
fi | |
LABEL=$STAGE-$SHA1-$RANDOM-$DATE | |
# Remove the old build | |
rm -rf build/app | |
# Build assets | |
echo "Building Assets" | |
rm -rf public/assets/* public/mix-manifest.json | |
yarn prod | |
# Build the application | |
echo "Starting Docker" | |
docker build -t our-app-build -f docker/BuildDockerfile . | |
docker run -v $(pwd)/build:/app/build our-app-build | |
# Create a temporary directory to stage the files | |
mkdir ${BUILD_DIR} | |
# Stage the files | |
cp .env.${STAGE} ${BUILD_DIR}/.env | |
cp build/app ${BUILD_DIR} | |
cp -r public ${BUILD_DIR} | |
cp docker/Dockerfile ${BUILD_DIR} | |
cp docker/Dockerrun.aws.json ${BUILD_DIR} | |
cp -r .ebextensions ${BUILD_DIR} | |
# Getting them zipped up | |
cd ${BUILD_DIR}; zip -Xr app.zip * .env .ebextensions; mv app.zip ../; cd .. | |
# Deploy the application | |
eb deploy --label $LABEL | |
# Cleanup | |
rm -rf ${BUILD_DIR} app.zip | |
END=`date +%s` | |
echo Deploy ended with success! Time elapsed: $((END-START)) seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cp app /app/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM phusion/baseimage | |
ENV APP_DOMAIN=localhost | |
ENV SECRET_KEY_BASE=changeme | |
ENV PORT=8000 | |
ENV DEBIAN_FRONTEND=noninteractive | |
RUN apt-get -q update && \ | |
apt-get -qy install build-essential libgc-dev libssl-dev libxml2-dev libyaml-dev libevent-dev && \ | |
apt-get -y install tzdata && \ | |
apt-get -y autoremove && \ | |
apt-get -y clean && \ | |
rm -rf /var/lib/apt/lists/* && \ | |
rm -rf /tmp/* | |
RUN mkdir /app | |
ADD ./app /app | |
ADD ./.env /app/.env | |
ADD ./public /app/public | |
WORKDIR /app | |
EXPOSE 8000 | |
CMD trap exit TERM; ./app & wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment