Skip to content

Instantly share code, notes, and snippets.

@saqibarfeen
Created January 10, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saqibarfeen/0dd36f5ff718784c194b18a231629fed to your computer and use it in GitHub Desktop.
Save saqibarfeen/0dd36f5ff718784c194b18a231629fed to your computer and use it in GitHub Desktop.
#!/bin/bash
#server react
DIR_deploy="/srv"
DIR_server="$DIR_deploy/server"
DIR_server_WWW="$DIR_server/www"
DIR_server_tmp="$DIR_server/tmp"
DIR_server_GIT="$DIR_server/git"
#DIR_ENV="/saqib-deploy/server/env/"
function dir_create() {
sudo mkdir -p "$1"
#cd "$1" || exit
cd $DIR_deploy || exit
sudo chgrp -R users .
sudo chmod -R g+rwX .
}
if [ $# -eq 0 ]; then
echo 'No project name provided (mandatory)'
exit 1
else
echo "- Project name:" "$1"
fi
dir_create "$DIR_server_WWW"
echo "created- www:" "$DIR_server_WWW"
#GIT=$DIR_GIT$1.git
#TMP=$DIR_TMP$1
#WWW=$DIR_WWW
#ENV=$DIR_ENV$1
# Create $WWW parent directory
#echo "- git:" "$GIT"
#echo "- tmp:" "$TMP"
#echo "- www:" "$WWW"
#echo "- env:" "$ENV"
export DIR_deploy
export DIR_server
export DIR_server_WWW
export DIR_server_GIT
PROJECT_NAME=$1
export PROJECT_NAME
#export GIT
#export TMP
#export WWW
#export ENV
# Create a directory for the git repository
dir_create $DIR_server_GIT/$1.git
DIR_server_GIT=$DIR_server_GIT/$1.git
cd "$DIR_server_GIT" || exit
#echo "~~~~~~~~~~~~~~~~ changed to $DIR_server_GIT" || exit
# Init the repo as an empty git repository
sudo git init --bare
# Define group recursively to "users", on the directories
sudo chgrp -R users .
# Define permissions recursively, on the sub-directories
# g = group, + add rights, r = read, w = write, X = directories only
# . = curent directory as a reference
sudo chmod -R g+rwX .
# Sets the setgid bit on all the directories
# https://www.gnu.org/software/coreutils/manual/html_node/Directory-Setuid-and-Setgid.html
sudo find . -type d -exec chmod g+s '{}' +
# Make the directory a shared repo
sudo git config core.sharedRepository group
cd hooks || exit
# create a post-receive file
sudo tee post-receive <<EOF
#!/bin/bash
# The production directory
#WWW="${WWW}"
# A temporary directory for deployment
# The Git repo
# The Env repo
#ENV="${ENV}"
GIT="${DIR_server_GIT}"
TMP="${DIR_server_tmp}/${PROJECT_NAME}"
#echo "~~~~~~~~~~~~TMP !!!!!!!!!!! = \$TMP"
BASE="${DIR_deploy}"
WWW="${DIR_server_WWW}"
BRANCH="production"
function install_node()
{
sudo mkdir -p /workspace
sudo chown mohamed:mohamed /workspace
cd /workspace
curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
sudo yum install -y nodejs
node --version
}
mkdir -p \$TMP
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
#echo "Testing~~~~~~ \$oldrev ~~~~~ \$newrev ~~~~~~~~ \$ref in while loop"
if [[ \$ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/home/webuser/public --git-dir=/home/webuser/www.git checkout -f master
else
if [[ \$ref = refs/heads/\$BRANCH ]];
then
echo "Saqib dev ref :\$ref received. Deploying sandbox branch to beta..."
git --work-tree=\$TMP --git-dir=\$GIT checkout -f \$BRANCH
# install_node
echo "changing dir to \$TMP and installing npm packages ..."
cd \$TMP
npm install
#change to client and install packages
#cd \$TMP/KPKApp
#npm install
#npm run build
#deploy
rm -rf \$WWW
mkdir -p \$WWW
cd \$DIR_deploy || exit
sudo chgrp -R users .
sudo chmod -R g+rwX .
echo "~~~~~~~~~~MOVING \$TMP to \$WWW ~~~~~~~~~"
mv \$TMP \$WWW
/bin/bash \$GIT/hooks/setup.sh
else
echo "Ref \$ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
fi
done
EOF
sudo chmod +x post-receive
#create a new script
sudo tee setup.sh <<EOF
echo "Starting the server~~~~~~~"
/usr/bin/forever start /srv/server/www/27dec/bin/www.js
EOF
# make it executable
sudo chmod +x setup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment