Skip to content

Instantly share code, notes, and snippets.

@cassaram09
Last active September 13, 2022 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cassaram09/66ab288e37d2b4143c61e8c040d49fea to your computer and use it in GitHub Desktop.
Save cassaram09/66ab288e37d2b4143c61e8c040d49fea to your computer and use it in GitHub Desktop.
Bash script to setup a bare git repository on a server utilizing ssh for deployment and password protection using htaccess
# This script will help configure automated deployment of projects to a server using Git.
# Created by Matt Cassara
# More info at http://mattcassara.com/automated-git-project-deployment/
# This is based on a script by Richard Westenra
# See http://richardwestenra.com/automating-git-deployment/
echo -e "\n"
echo ">> Welcome to the DSB project set up script."
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 your Github Account. "
echo ">> Please enter the repository name."
echo -e ">> It will be used for the project and htaccess username.\n"
# Get the project name - this will be used in the folder path and in the htacess username
echo ">> Repository Name:"
read name
echo -e "\n"
echo -e ">> Begin setup...\n "
# Echo verbose commands
#set -x
# Create the repo directory
mkdir -p ~/Development/temp/$name/website/$name.git
# Run git init and set up git hooks in the directory
cd ~/Development/temp/$name/website/$name.git
git init --bare
cat > hooks/post-receive << EOF
#!/bin/sh
GIT_WORK_TREE=~/public_html/client/$name/website git checkout -f
EOF
chmod +x hooks/post-receive
#set +x
echo -e ">> Git post receive hook is set up at $name/$name.git \n"
# Start htaccess setup
cd ~/Development/temp/$name
echo -e ">> Create htaccess file \n"
# Create htpasswd file
htpasswd -c .htpasswd $name
echo -e "\n"
# Create htaccess file
echo -e ">> Setting up htaccess file... \n"
{
echo 'AuthUserFile /home/darksquarebishop/public_html/client/'$name'/.htpasswd'
echo 'AuthName "Please Log In"'
echo 'AuthType Basic'
echo 'require user' $name
} > .htaccess
# Create website index file
echo "This is the project index page for:" $name " - <a href='website'>Visit Website</a>"> index.html
# Clone new repo down to your computer
echo -e ">> Cloning repository... \n"
cd ~/Development
if git clone git@github.com:<YOUR ACCOUNT NAME HERE>/$name.git
then
echo -e ">> Copying files to server... \n"
# Copy folder and files to server
if scp -r ~/Development/temp/$name darksquarebishop@darksquarebishop.com:~/public_html/client/$name
then
# Delete local files
echo -e "\n"
echo -e ">> Cleaning up temp files... \n"
rm -r ~/Development/temp/$name
fi
# Create a basic index page and commit it to the repository
cd ~/Development/$name
echo "This is the website index page for: " $name > demo.html
git remote add staging darksquarebishop@darksquarebishop.com:~/public_html/client/$name/website/$name.git
git add .
git commit -m "First commit"
# Push the new repository up to the server
echo -e "\n"
echo -e ">> Pushing to origin (Github)...\n"
git push origin master
echo -e "\n"
echo -e ">> Pushing to staging (DSB)...\n"
git push staging master
echo -e "\n"
echo -e ">> Set up successful."
else
echo -e "\n"
echo -e ">> Failed to clone repository.\n"
echo "Delete remote files? y/n"
read answer
until [ $answer = "y" ] || [ $answer = "n" ]; do
echo ">> Please select a valid command."
echo "Delete files? y/n:"
read answer
done
if [ $answer = "y" ]; then
rm -R ~/Development/$name
ssh darksquarebishop@darksquarebishop.com 'rm -R ~/public_html/client/'$name
echo -e ">> Deleted. \n"
echo -e ">> Setup failed. \n"
else
echo "not deleting files"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment