git-scripts/new-deployable-site.sh

59 lines
1.6 KiB
Bash
Raw Normal View History

2019-06-01 00:17:28 +00:00
#!/usr/bin/env bash
set -u
set -e
my_user_tmp=$(grep -r "Git Site Deploys" /etc/passwd | cut -d':' -f1)
read -p "Repository Username [$my_user_tmp]: " my_user
2019-06-01 00:29:28 +00:00
: ${my_user:="$my_user_tmp"}
2019-06-01 00:17:28 +00:00
while [ -z "${my_site:-}" ]; do
read -p "Site name (ex: example.com): " my_site
2019-06-01 00:17:28 +00:00
done
my_repo_tmp=$(eval echo "~$my_user")
2019-06-01 05:14:57 +00:00
read -p "Repository [$my_repo_tmp/${my_site}.git]: " my_repo
: ${my_repo:="$my_repo_tmp/${my_site}.git"}
2019-06-01 00:17:28 +00:00
2019-06-01 05:14:57 +00:00
read -p "Deploy to [/srv/www/$my_site]: " my_deploy
: ${my_deploy:="/srv/www/$my_site"}
2019-06-01 00:17:28 +00:00
2019-06-01 00:43:21 +00:00
my_pretty="x"
while [ "y" != "$my_pretty" ] && [ "n" != "$my_pretty" ]; do
read -p "Reject ugly commits? [y/n]: " my_pretty
2019-06-01 00:43:21 +00:00
done
2019-06-01 00:17:28 +00:00
echo ""
echo ""
set -x
sudo git init --bare --shared=group "${my_repo}"
sudo bash -c "cat << EOF > ${my_repo}/hooks/post-receive
#!/usr/bin/env bash
mkdir -p '${my_deploy}'
# always deploys from the default (master) branch
2019-06-01 06:08:19 +00:00
#git remote add upstream git@example.com:${my_repo}
#git push -u upstream master
#git push --all --force
2019-06-01 00:17:28 +00:00
git --work-tree='${my_deploy}' --git-dir='${my_repo}' checkout -f
2019-06-01 02:06:31 +00:00
echo Deployed branch "'$(git rev-parse --abbrev-ref HEAD)'" to '${my_deploy}'
2019-06-01 00:17:28 +00:00
EOF
"
2019-06-01 00:43:21 +00:00
if [ "y" == "$my_pretty" ]; then
# TODO test for / install 'prettier'
if [ -z "$(command -v prettier)" ]; then
curl -fsL bit.ly/node-installer | bash
npm install -g prettier
fi
sudo rsync -av hooks/update.format.full "${my_repo}/hooks/update"
2019-06-01 00:43:21 +00:00
fi
sudo chmod -R a+x "${my_repo}/hooks/"
2019-06-01 00:17:28 +00:00
sudo chown -R ${my_user}:${my_user} "$my_repo"
set +x
echo ""
echo "Authorized users can push to this repo by adding it as a remote. Example:"
2019-06-01 00:26:18 +00:00
echo " git remote add origin ${my_user}@$(hostname):${my_repo}"
2019-06-01 00:17:28 +00:00
echo ""