2020-01-02 16:58:06 +00:00
#!/bin/bash
2022-06-11 21:44:25 +00:00
# This hook builds pixelplanet after a push, and deploys it, it should be ron post-receive
2020-01-02 16:58:06 +00:00
# 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)
#
# To set up a server to use this, you have to go through the building steps manually first.
#
#folder for building the canvas (the git repository will get checkout there and the canvas will get buil thtere)
2020-05-10 08:43:43 +00:00
BUILDDIR = "/home/pixelpla/pixelplanet-build"
2020-01-02 16:58:06 +00:00
#folder for dev canvas
2020-05-10 08:43:43 +00:00
DEVFOLDER = "/home/pixelpla/pixelplanet-dev"
2020-01-02 16:58:06 +00:00
#folder for production canvas
2020-05-10 08:43:43 +00:00
PFOLDER = "/home/pixelpla/pixelplanet"
2020-01-02 16:58:06 +00:00
2022-06-11 21:35:37 +00:00
should_reinstall ( ) {
local TMPFILE = " ${ BUILDDIR } /package.json. ${ branch } .tmp "
2022-06-17 10:33:09 +00:00
local NODEDIR = " ${ BUILDDIR } /node_modules "
2022-06-11 21:35:37 +00:00
local ORFILE = " ${ BUILDDIR } /package.json "
2022-06-17 10:33:09 +00:00
[ -f " ${ TMPFILE } " ] && [ -d " ${ NODEDIR } " ] && diff -q " ${ TMPFILE } " " ${ ORFILE } " && {
2022-06-11 21:35:37 +00:00
echo "package.json stil the same, no need to rerun npm install."
return 1
}
cp " ${ ORFILE } " " ${ TMPFILE } "
echo "package.json changed, need to run npm install."
return 0
}
npm_reinstall ( ) {
rm -rf node_modules
rm package-lock.json
npm install
}
2020-01-02 16:58:06 +00:00
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
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 "
2022-06-11 21:35:37 +00:00
should_reinstall
DO_REINSTALL = $?
[ $DO_REINSTALL -eq 0 ] && npm_reinstall
2020-05-10 08:43:43 +00:00
npm run build
2020-01-02 16:58:06 +00:00
echo "---RESTARTING CANVAS---"
2022-01-03 12:59:40 +00:00
cp -r dist/*.js " ${ PFOLDER } / "
2022-06-11 21:35:37 +00:00
cp -r dist/workers " ${ PFOLDER } / "
rm -rf " ${ PFOLDER } /public/assets "
2022-01-03 12:59:40 +00:00
cp -r dist/public " ${ PFOLDER } / "
cp -r dist/captchaFonts " ${ PFOLDER } / "
cp -r dist/package.json " ${ PFOLDER } / "
cp -r dist/assets.json " ${ PFOLDER } / "
cp -r dist/styleassets.json " ${ PFOLDER } / "
2020-05-10 08:43:43 +00:00
mkdir -p " ${ PFOLDER } /log "
2020-01-02 16:58:06 +00:00
cd " $PFOLDER "
2022-01-03 12:59:40 +00:00
pm2 stop ppfun-server
pm2 stop ppfun-backups
2022-06-11 21:35:37 +00:00
pm2 stop ppfun-captchs
[ $DO_REINSTALL -eq 0 ] && npm_reinstall
2020-01-02 16:58:06 +00:00
pm2 start ecosystem.yml
2020-06-13 09:17:16 +00:00
pm2 start ecosystem-backup.yml
2022-06-11 21:35:37 +00:00
pm2 start ecosystem-captchas.yml
2020-01-02 16:58:06 +00:00
else
2022-06-11 21:44:25 +00:00
branch = "dev"
2020-01-02 16:58:06 +00:00
echo "---UPDATING REPO ON DEV SERVER---"
2022-01-03 12:59:40 +00:00
pm2 stop ppfun-server-dev
2022-06-11 21:35:37 +00:00
pm2 stop ppfun-captchas-dev
2020-01-02 16:58:06 +00:00
GIT_WORK_TREE = " $BUILDDIR " GIT_DIR = " ${ BUILDDIR } /.git " git fetch --all
GIT_WORK_TREE = " $BUILDDIR " GIT_DIR = " ${ BUILDDIR } /.git " git reset --hard " origin/ $branch "
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 "
2022-06-11 21:35:37 +00:00
should_reinstall
DO_REINSTALL = $?
[ $DO_REINSTALL -eq 0 ] && npm_reinstall
2022-01-04 19:38:27 +00:00
nice -n 19 npm run build:dev
2020-01-02 16:58:06 +00:00
echo "---RESTARTING CANVAS---"
2022-01-03 12:59:40 +00:00
cp -r dist/*.js " ${ DEVFOLDER } / "
2022-06-11 21:35:37 +00:00
cp -r dist/workers " ${ DEVFOLDER } / "
rm -rf " ${ DEVFOLDER } /public/assets "
2022-01-03 12:59:40 +00:00
cp -r dist/public " ${ DEVFOLDER } / "
cp -r dist/captchaFonts " ${ DEVFOLDER } / "
cp -r dist/package.json " ${ DEVFOLDER } / "
cp -r dist/assets.json " ${ DEVFOLDER } / "
cp -r dist/styleassets.json " ${ DEVFOLDER } / "
2020-05-10 08:43:43 +00:00
mkdir -p " ${ PFOLDER } /log "
2020-01-02 16:58:06 +00:00
cd " $DEVFOLDER "
2022-06-11 21:35:37 +00:00
[ $DO_REINSTALL -eq 0 ] && npm_reinstall
2020-01-02 16:58:06 +00:00
pm2 start ecosystem.yml
2022-06-11 21:35:37 +00:00
pm2 start ecosystem-captchas.yml
2020-01-02 16:58:06 +00:00
fi
2022-06-11 21:35:37 +00:00
done