Skip to content

Project Setup

#!/bin/bash
echo -e "\n"
echo ">> Welcom to the SKNKWOXS project set up spcript."
echo -e ">> This script will set up a basic project structure, staging git repository, and password protection.\n"
echo ">> This script assumes you are cloning from Sknkwoxs Github Account."
echo ">> Please enter the repository name."
echo -e ">> It will be used for the project and htaccess username.\n"
echo ">> Repository Name:"
read name
echo -e "\n"
echo -e ">> Begin setup...\n"
# Create the repo directory
cd ~/repos
git clone --bare --shared git@github.com:sknkwoxs/$name.git $name.git
if [ $? -eq 0 ]; then
cd $name.git
else
exit;
fi
while :
do
echo ">> Git branch:"
read git_branch
# Create Web directory
echo -e "\n"
mkdir -p /var/www/$name
echo -e ">> Git post receive hook is set up at ~/repos/$name.git \n"
echo -e "\n"
echo -e ">> Cloning repository...\n"
git --work-tree="/var/www/$name" --git-dir="/home/sknk/repos/$name.git" checkout -f $git_branch
if [ $? -eq 0 ]; then
break
else
rm -rf /var/www/$name
fi
done
# Create hook post-receive
cat > hooks/post-receive << EOF
#!/bin/bash
#for debugging
#set -ex
# the work tree, where the checkout/deploy should happen
TARGET="/var/www/$name"
# the location of the .git directory
GIT_DIR="/home/sknk/repos/$name.git"
BRANCH="$git_branch"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "\${ref}" = "refs/heads/\${BRANCH}" ];
then
echo "Ref \${ref} received. Deploying \${BRANCH} branch on server..."
git --work-tree="\${TARGET}" --git-dir="\${GIT_DIR}" checkout -f \${BRANCH}
cd \${TARGET} && ddev composer install -o --no-dev
else
echo "Ref \${ref} received. Doing nothing: only the \${BRANCH} branch may be deployed on this server."
fi
done
EOF
chmod +x hooks/post-receive
# Create DDEV
cd /var/www/$name
if [ -d "/var/www/$name/.ddev" ]; then
echo -e "\n"
echo ">> Exist ddev dir"
echo -e ">> DDEV start ..."
ddev start
echo -e "\n"
echo -e ">> If snapshot exiest, please select db file that you want to restore..."
ddev snapshot restore
else
echo -e "\n"
echo -e ">> Create ddev config"
ddev config --auto
#echo -e ">> DDEV start ..."
#ddev start
fi
# Execute composer install
if [ -f "/var/www/$name/composer.json" ]; then
echo -e "\n"
echo -e ">> composer.json exists."
ddev composer install -o --no-dev
fi