Last active
October 7, 2024 02:45
-
-
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 file contains hidden or 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
# 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 [email protected]:<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 [email protected]:~/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 [email protected]:~/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 [email protected] '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