Last active
January 19, 2021 10:33
-
-
Save sagarchauhan005/1ddf634a6e1870fcf697b79bbbcc4b12 to your computer and use it in GitHub Desktop.
deployer.sh
This file contains 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
#!/usr/bin/env bash | |
RED="\033[1;31m\n" | |
NOCOLOR="\033[0m\n" | |
YELLOW='\033[0;33m' | |
GREEN='\033[0;32m' | |
if [[ $EUID -ne 0 ]]; then | |
printf "${RED}This script must be run as root${NOCOLOR}" | |
exit 1 | |
fi | |
printf "${GREEN}Enter the name of the app you wish to deploy (avoid spaces and special character)${NOCOLOR}" | |
read -r app_name | |
printf "${YELLOW}Configuring deployment for $app_name.${NOCOLOR}" | |
sleep 1 | |
command -v git >/dev/null 2>&1 || | |
{ echo >&2 "Git is not installed. Installing.."; | |
yum install git | |
} | |
command -v go >/dev/null 2>&1 || | |
{ echo >&2 "Go is not installed. Installing.."; | |
wget https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz | |
sudo tar -C /usr/local -xzf go1.11.4.linux-amd64.tar.gz | |
export PATH=$PATH:/usr/local/go/bin | |
} | |
cd "$HOME" || exit | |
# Installing webhook | |
go get github.com/adnanh/webhook | |
app_hook="$HOME/$app_name/webhooks" | |
cwd="$HOME/$app_name/webhooks/deployer/" | |
hjson="$HOME/$app_name/webhooks/hooks.json" | |
deploy_script="$HOME/$app_name/webhooks/deployer/deploy.sh" | |
mkdir "$app_name" || exit | |
mkdir "$app_hook" | |
mkdir "$cwd" | |
touch "$hjson" | |
touch "$deploy_script" | |
chmod +x "$deploy_script" | |
#write to deployer script | |
{ | |
echo "#\!/bin/bash" | |
echo "git fetch --all" | |
echo "git checkout --force origin/master" | |
} >> "$deploy_script" | |
printf "${GREEN}Enter your secret key${NOCOLOR}" | |
read -r secret | |
jq -n --arg id "$app_name" \ | |
--arg cwd "$cwd" \ | |
--arg deployer "$deploy_script" \ | |
--arg secret "$secret" \ | |
'[{"id": "$id","execute-command": "$deploy_script","command-working-directory": "$cwd","response-message": "Executing deploy script...","trigger-rule": {"match": {"type": "HMACSha256","secret": "$secret","parameter": {"source": "header","name": "X-Hub-Signature"}}}}]' > "$hjson" | |
printf "${YELLOW}Please enter the git repository link (without clone command).${NOCOLOR}" | |
read -r git_repo | |
cd "$cwd" || exit | |
git init | |
git remote add origin "$git_repo" | |
cd "$HOME" || exit | |
printf "${GREEN}Enter your server IP${NOCOLOR}" | |
read -r server_ip | |
"$HOME/go/bin/webhook" -hooks "$hjson" -ip "$server_ip" -verbos | |
ssh-keygen -t rsa -N "" -f "$app_name.key" | |
printf "${GREEN} Please copy paste the below mentioned ssk keys and paste into your repo settings" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment