BASH
Deploy Static Website via SCP
Automate the secure deployment of a static website or build artifacts to a remote server using SCP, ideal for simple CI/CD pipelines.
#!/bin/bash
LOCAL_BUILD_DIR="./dist" # Local directory containing built static files
REMOTE_USER="webadmin"
REMOTE_HOST="your-server.com"
REMOTE_PATH="/var/www/my-static-site" # Destination path on remote server
if [ ! -d "${LOCAL_BUILD_DIR}" ]; then
echo "Error: Local build directory '${LOCAL_BUILD_DIR}' not found."
echo "Please run your build command first (e.g., 'npm run build')."
exit 1
fi
echo "Starting deployment to ${REMOTE_HOST}:${REMOTE_PATH}"
# Ensure the remote directory exists
ssh "${REMOTE_USER}@${REMOTE_HOST}" "mkdir -p ${REMOTE_PATH}"
# Copy files using scp (recursive and preserve times/modes)
scp -r -p "${LOCAL_BUILD_DIR}/." "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
if [ $? -eq 0 ]; then
echo "Deployment successful to ${REMOTE_HOST}:${REMOTE_PATH}"
else
echo "Deployment failed."
exit 1
fi
How it works: This script facilitates deploying a static website's build output to a remote server using Secure Copy Protocol (SCP). It first checks if the local build directory exists. Then, it uses `ssh` to ensure the target directory exists on the remote server. Finally, `scp -r -p` recursively copies all files and directories from the local build folder to the remote path, preserving their original timestamps and permissions.