Skip to content

Instantly share code, notes, and snippets.

@Jinksi
Last active August 27, 2019 14:09
Show Gist options
  • Save Jinksi/d688a5125aacb9334b22411417ba59ee to your computer and use it in GitHub Desktop.
Save Jinksi/d688a5125aacb9334b22411417ba59ee to your computer and use it in GitHub Desktop.
Bash script for Gitlab Webhook setup & Wordpress
#!/bin/bash
# v0.9.0
# Run from Wordpress installation directory
# DEFINE COLORS
RED='\033[0;31m' # error
GRN='\033[0;32m' # success
BLU='\033[0;34m' # task
BRN='\033[0;33m' # headline
NC='\033[0m' # no color
# GENERATE SAT
sat=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12)
dir=$(pwd)
dirname=$(pwd | sed -e 's/[\/&]/\\&/g')
printf "\n"
printf "${BRN}========== WEBHOOK SETUP ==========${NC}\n\n"
# Detect WP install
if [ -f $dir/wp-config.php ]
then
printf "${GRN}»»» found wordpress installation in $dir${NC}\n\n"
else
printf "${RED}»»» Cannot find wordpress installation!${NC}\n"
exit
fi
# Repo
while [ -z "$repo" ]; do
read -p "What is the git repository address: " repo
if [ -z "$repo" ]; then
printf "${RED}-> need a repo...${NC}\n";
elif [[ $repo != git* ]]; then
# address doesn't start with git
printf "${RED}-> not a valid SSH address${NC}\n";
unset repo
fi
done
repo=$(echo $repo | sed -e 's/[\/&]/\\&/g')
# Website Url
while [ -z "$url" ]; do
read -p "What is the website's url: " url
if [ -z "$url" ]; then
printf "${RED}-> need a url...${NC}\n";
elif [[ $url != http://* && $url != https://* ]]; then
# address doesn't start with http
url=$(echo "http://$url")
fi
done
# ThemeDir
while [ -z "$themedir" ]; do
read -p "Theme directory name: " themedir
if [ -z "$themedir" ]; then printf "${RED}-> need a themedir...${NC}\n"; fi
done
# Email
while [ -z "$email" ]; do
read -p "Email address (for errors): " email
if [ -z "$email" ]; then printf "${RED}-> need an email...${NC}\n"; fi
done
# Clone
printf "${BLU}»»» cloning deploy script${NC}\n"
git clone https://github.com/Jinksi/simple-php-git-deploy.git $dir/deploy || printf 'The deploy directory exists\n'
printf "${GRN}done${NC}\n"
# Write Config
printf "${BLU}»»» writing config${NC}\n"
cp ./deploy/deploy-config.example.php ./deploy/deploy-config.php
# Search replace
sed -i "s/BetterChangeMeNowOrSufferTheConsequences/$sat/g" $dir/deploy/deploy-config.php
sed -i "s/https:\/\/github\.com\/markomarkovic\/simple-php-git-deploy.git/$repo/g" $dir/deploy/deploy-config.php
sed -i "s/\/tmp\/simple-php-git-deploy\//$dirname\/wp-content\/themes\/$themedir\//g" $dir/deploy/deploy-config.php
sed -i "s/'DELETE_FILES', false/'DELETE_FILES', true/g" $dir/deploy/deploy-config.php
sed -i "s/'CLEAN_UP', true/'CLEAN_UP', false/g" $dir/deploy/deploy-config.php
sed -i "s/'EMAIL_ON_ERROR', false/'EMAIL_ON_ERROR', '$email'/g" $dir/deploy/deploy-config.php
printf "${GRN}done${NC}\n"
# Permissions
printf "${BLU}»»» setting permissions${NC}\n"
chmod 755 $dir/deploy/
chmod 755 $dir/deploy/deploy.php
printf "${GRN}done${NC}\n"
# Deploy Key
printf "${BLU}»»» creating deploy key${NC}\n"
if [ -f ~/.ssh/id_rsa.pub ]
then
sshkey=$(cat ~/.ssh/id_rsa.pub)
else
ssh-keygen -q -N '' -f ~/.ssh/id_rsa
sshkey=$(cat ~/.ssh/id_rsa.pub)
fi
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
printf "${GRN}done${NC}\n\n"
printf "${BRN}========== COMPLETE ==========${NC}\n\n"
printf "${GRN}Your Webhook is: ${NC}\n"
printf "${BRN}$url/deploy/deploy.php?sat=$sat${NC}\n\n"
printf "${GRN}Your Deploy Key is: ${NC}\n"
printf "${BRN}$sshkey${NC}\n\n"
printf "${GRN}Directory to be synced: ${NC}\n"
printf "${BRN}$dir/wp-content/themes/$themedir${NC}\n\n"
printf "${GRN}done${NC}\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment