Last active
January 2, 2016 01:46
-
-
Save TylerJPresley/0bbe7e7ebb543d66a002 to your computer and use it in GitHub Desktop.
Bash script to deploy static/js website
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
#!/bin/bash | |
YELLOW='\033[0;33m' | |
YELLOW_BOLD='\033[1;33m' | |
BLUE='\033[0;34m' | |
BLUE_BOLD='\033[1;34m' | |
NC='\033[0m' # No Color | |
USER="thatuser" | |
IP="999.999.999.999" | |
PORT="22" | |
TIMESTAMP=$(date +"%Y%m%d%H%M%S") | |
DEPLOYMENT_PKG="deploy-$TIMESTAMP.tar.gz" | |
REMOTE_DIR="/var/websites/mywebsite123.com" | |
DIST_DIR="dist" | |
KEEP=5 | |
echo "$YELLOW_BOLD - Running local scripts $NC" | |
echo "$YELLOW ├─ Cleaning dist directory $NC" | |
gulp clean > /dev/null | |
echo "$YELLOW ├─ Generating the deployment files $NC" | |
gulp --production > /dev/null | |
echo "$YELLOW ├─ Packaging files: $DEPLOYMENT_PKG $NC" | |
tar -zcf $DEPLOYMENT_PKG -C $DIST_DIR . > /dev/null | |
echo "$YELLOW ├─ Uploading package to: /tmp/$DEPLOYMENT_PKG $NC" | |
scp -P $PORT $DEPLOYMENT_PKG $USER@$IP:/tmp/$DEPLOYMENT_PKG > /dev/null | |
echo "$YELLOW └- Removing local $DEPLOYMENT_PKG $NC" | |
rm $DEPLOYMENT_PKG > /dev/null | |
echo "" | |
ssh -T -p $PORT $USER@$IP <<EOF | |
echo -e "$BLUE_BOLD - Running script on server $NC" | |
echo -e "$BLUE ├─ Creating release directory: $REMOTE_DIR/releases/$TIMESTAMP $NC" | |
mkdir -p $REMOTE_DIR/releases/$TIMESTAMP | |
echo -e "$BLUE ├─ Extracting: /tmp/$DEPLOYMENT_PKG to $REMOTE_DIR/releases/$TIMESTAMP $NC" | |
tar -zxf /tmp/$DEPLOYMENT_PKG -C $REMOTE_DIR/releases/$TIMESTAMP | |
echo -e "$BLUE ├─ Updating symlink to new release $NC" | |
ln -sfn $REMOTE_DIR/releases/$TIMESTAMP $REMOTE_DIR/current | |
echo -e "$BLUE ├─ Removing /tmp/$DEPLOYMENT_PKG $NC" | |
rm /tmp/$DEPLOYMENT_PKG | |
echo -e "$BLUE ├─ Removing old directories $NC" | |
CNTR=0; | |
for i in \$(find $REMOTE_DIR/releases -maxdepth 1 -type d | sort -Vr); do | |
((CNTR+=1)); | |
if [[ "\$i" != "$REMOTE_DIR/releases" ]] && [ "\$CNTR" -gt "$KEEP" ]; then | |
rm -rf \$i; | |
fi | |
done | |
echo -e "$BLUE └- Setting file permissions $NC" | |
find $REMOTE_DIR/releases/$TIMESTAMP -type d -exec chmod 755 {} + | |
find $REMOTE_DIR/releases/$TIMESTAMP -type f -exec chmod 644 {} + | |
EOF | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment