Files
pedini.dev/deploy.sh
Bryan Joshua Pedini 2f2c60126c
Some checks failed
Deploy website on production server when committing on main / test (push) Failing after 12s
feat(deploy): improve environment variable handling and deployment workflow
- Update .vars to properly export deployment configuration variables
- Modify deploy.sh to conditionally source .vars only in interactive mode
- Remove include directive from makefile to prevent automatic variable loading
- Enhance deployment script reliability by ensuring proper environment setup
2026-02-01 19:24:37 +01:00

43 lines
1.5 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_PRIVATE_KEY} ${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