Created
January 10, 2019 13:44
-
-
Save saqibarfeen/af46eb6ef321861e8a1d9bfa351f65b5 to your computer and use it in GitHub Desktop.
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 | |
#client react | |
DIR_deploy="/srv" | |
DIR_client="$DIR_deploy/client" | |
DIR_client_WWW="/srv/server/www/$1" | |
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}/KPKApp" | |
#echo "~~~~~~~~~~~~TMP !!!!!!!!!!! = \$TMP" | |
BASE="${DIR_deploy}" | |
WWW="${DIR_client_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 | |
npm run build | |
rm -rf \$WWW/KPKApp | |
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 | |
cd ${DIR_client_WWW}/${PROJECT_NAME} | |
/usr/bin/npm start > ${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