zenwalker
3/22/2018 - 11:04 AM

bitbucket-deploy.sh

#!/bin/bash

# Environment variables:
# REMOTE_USER, REMOTE_HOST — obviously

# Read more about deployment over SSH:
# https://confluence.atlassian.com/bitbucket/access-remote-hosts-via-ssh-847452940.html

set -e  # automatic exit on error

REMOTE_DIR="~/www"
DIST_FILE="/tmp/dist.tar.gz"
SSH_USER="$REMOTE_USER@$REMOTE_HOST"
SSH_OPTIONS=""

echo "creating an archive of project files"
tar -czf $DIST_FILE --exclude .git --exclude dist.tar.gz ./

echo "uploading the archive on the remote host"
scp $SSH_OPTIONS $DIST_FILE $SSH_USER:~/dist.tar.gz

echo "unpacking the archive and restart the servcies"
ssh $SSH_USER $SSH_OPTIONS << EOF
    mkdir -p ~/dist
    mkdir -p $REMOTE_DIR
    tar xf ~/dist.tar.gz -C ~/dist

    if [[ -d $REMOTE_DIR ]]; then
        rm -rf $REMOTE_DIR
    fi

    mv ~/dist $REMOTE_DIR
EOF

rm $DIST_FILE
echo "Done"
exit 0