Files
pedini.dev/deploy.sh
Bryan Joshua Pedini 8a94fe0add
All checks were successful
Deploy website on production server when committing on main / test (push) Successful in 11s
THE PRIVATE KEEEEEEEYYY
2026-02-01 19:25:29 +01:00

43 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
###
# FLOW
###
#
# if the username variable is set, append the at sign to it
# if either the deployment host or deployment path variables are not set, return an error
# tarball the built website and scp it to the deployment host
# ssh to the remote host, cd to the deployment path, and get the data path from .env file
# then remove everything in the data path, untar the tarball and reload the server
# finally remove the tarball, both from the remote host and locally (cleanup)
if [ -t 0 ]; then # Interactive: prompt user
source .vars
fi
# Check if the username variable is set
if [ ! -z "${SSH_USERNAME}" ]; then
SSH_USERNAME="${SSH_USERNAME}@"
fi
# Check if either the deployment host or deployment path variables are not set
if [ -z "${DEPLOYMENT_HOST}" ] || [ -z "${DEPLOYMENT_PATH}" ]; then
echo "required variable DEPLOYMENT_HOST is not set"
exit 1
fi
# Compress the built website and scp it to the remote host
tar -czf httpdocs.tgz -C public .
scp httpdocs.tgz ${SSH_USERNAME}${DEPLOYMENT_HOST}:/tmp/httpdocs.tgz
# SSH to the remote host, cd to the deployment path, and deploy the website (delete and overwrite everything)
ssh ${SSH_USERNAME}${DEPLOYMENT_HOST} "DEPLOYMENT_PATH=$DEPLOYMENT_PATH bash" << 'EOF'
cd ${DEPLOYMENT_PATH}
DATAPATH=$(cat .env | grep "NGINX_DATA" | sed "s/NGINX_DATA=//g")
rm -rf ${DATAPATH}/{*,.*}
tar xf /tmp/httpdocs.tgz -C ${DATAPATH}
rm -f /tmp/httpdocs.tgz
EOF
rm -f httpdocs.tgz