Skip to content

Instantly share code, notes, and snippets.

@saqibarfeen
Created December 27, 2018 08:27
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/31be844f7345c73b2c3a9d940b1bba83 to your computer and use it in GitHub Desktop.
Save saqibarfeen/31be844f7345c73b2c3a9d940b1bba83 to your computer and use it in GitHub Desktop.
#!/bin/bash
#client react
DIR_deploy="/srv"
DIR_client="$DIR_deploy/client"
DIR_client_WWW="$DIR_client/www"
DIR_client_tmp="$DIR_client/tmp"
DIR_client_GIT="$DIR_client/git"
#DIR_ENV="/saqib-deploy/client/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_client_WWW"
echo "created- www:" "$DIR_client_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_client
export DIR_client_WWW
export DIR_client_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_client_GIT/$1.git
DIR_client_GIT=$DIR_client_GIT/$1.git
cd "$DIR_client_GIT" || exit
echo "~~~~~~~~~~~~~~~~ changed to $DIR_client_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_client_GIT}"
TMP="${DIR_client_tmp}/${PROJECT_NAME}"
echo "~~~~~~~~~~~~TMP !!!!!!!!!!! = \$TMP"
BASE="${DIR_deploy}"
WWW="${DIR_client_WWW}"
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/dev ]];
then
echo "Saqib dev ref :\$ref received. Deploying sandbox branch to beta..."
git --work-tree=\$TMP --git-dir=\$GIT checkout -f dev
# install_node
echo "~~~~changing dir to \$TMP"
cd \$TMP
npm install
rm -rf \$WWW
mkdir -p \$WWW
echo "~~~~changing dir to \$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
#!/bin/bash
/usr/bin/node ${DIR_client_WWW}/${PROJECT_NAME}/bin/www.js > ${DIR_deploy}/cstdout.txt 2> ${DIR_deploy}/cstderr.txt &
# make it executable
EOF
sudo chmod +x setup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment