#!/bin/bash # This hook builds pixelplanet after a push, and deploys it # If it is the production branch, it will deploy it on the life system, and other branch will get deployed to the dev-canvas (a second canvas that is running on the server) # Update messages will get sent via the Webhooks to Discord # # To set up a server to use this, you have to go through the building steps manually first. # This hook just builds the canvas, it does not install new yarn/npm packages if needed. So this has to be done manually first # Also keep in mind that running a dev-canvas and a life canvas independently together on one server needs two redis installations. # tl;dr: Don't just copy that script, try to know how that setup works first # #discord webhook for dev canvas WEBHOOK='https://discordapp.com/api/webhooks/' #discord webhook for production canvas PWEBHOOK='https://discordapp.com/api/webhooks/' #folder for building the canvas (the git repository will get checkout there and the canvas will get buil thtere) BUILDDIR="pixelplanet-build" #folder for dev canvas DEVFOLDER="pixelplanet-dev" #folder for production canvas PFOLDER="pixelplanet" #proxies.json path PROXYFILE="/proxies.json" while read oldrev newrev refname do branch=$(git rev-parse --symbolic --abbrev-ref $refname) if [ "production" == "$branch" ]; then echo "---UPDATING REPO ON PRODUCTION SERVER---" GIT_WORK_TREE="$BUILDDIR" GIT_DIR="${BUILDDIR}/.git" git fetch --all GIT_WORK_TREE="$BUILDDIR" GIT_DIR="${BUILDDIR}/.git" git reset --hard origin/production curl -H "Content-Type: application/json" --data-binary '{ "username": "PixelPlanet Server", "avatar_url": "https://pixelplanet.fun/favicon.ico", "content": "Restarting canvas for Updates..." }' "$PWEBHOOK" COMMITS=`git log --pretty=format:'- %s%b' $newrev ^$oldrev` COMMITS=`echo "$COMMITS" | sed ':a;N;$!ba;s/\n/\\\n/g'` echo "---BUILDING pixelplanet---" cd "$BUILDDIR" cp "$PROXYFILE" ./ yarn run build --release echo "---RESTARTING CANVAS---" cp -r build/* "${PFOLDER}/" #cp ecosystem-production.yml "${PFOLDER}/ecosystem.yml" cd "$PFOLDER" pm2 stop web pm2 start ecosystem.yml #make backup tar -cvJf /backup/pixelplanet-src/pixelplanet-src-`date +%Y%m%d`.tar.xz --exclude=node_modules --exclude=.git -C "${BUILDDIR}/.." "pixelplanet-build" #send update message to discord curl -H "Content-Type: application/json" --data-binary '{ "username": "PixelPlanet Server", "avatar_url": "https://pixelplanet.fun/favicon.ico", "content": "...Done", "embeds": [{"title": "New Commits", "url": "https://pixelplanet.fun", "description": "'"$COMMITS"'", "color": 15258703}] }' "$PWEBHOOK" else echo "---UPDATING REPO ON DEV SERVER---" pm2 stop web-dev GIT_WORK_TREE="$BUILDDIR" GIT_DIR="${BUILDDIR}/.git" git fetch --all GIT_WORK_TREE="$BUILDDIR" GIT_DIR="${BUILDDIR}/.git" git reset --hard "origin/$branch" curl -H "Content-Type: application/json" --data-binary '{ "username": "PixelPlanet Server", "avatar_url": "https://pixelplanet.fun/favicon.ico", "content": "Restarting pixelplanet development canvas for update..." }' "$WEBHOOK" COMMITS=`git log --pretty=format:'- %s%b' $newrev ^$oldrev` COMMITS=`echo "$COMMITS" | sed ':a;N;$!ba;s/\n/\\\n/g'` echo "---BUILDING pixelplanet---" cd "$BUILDDIR" cp "$PROXYFILE" ./ nice -n 19 yarn run build --release echo "---RESTARTING CANVAS---" cp -r build/* "${DEVFOLDER}/" #cp ecosystem-dev.yml "${DEVFOLDER}/ecosystem.yml" cd "$DEVFOLDER" pm2 start ecosystem.yml curl -H "Content-Type: application/json" --data-binary '{ "username": "PixelPlanet Server", "avatar_url": "https://pixelplanet.fun/favicon.ico", "content": "...Done\nhttp://dev.pixelplanet.fun is now on branch '"$branch"'", "embeds": [{"title": "New Commits", "url": "https://pixelplanet.fun", "description": "'"$COMMITS"'", "color": 15258703}] }' "$WEBHOOK" fi done