Skip to content

Instantly share code, notes, and snippets.

@cyco130
Created April 8, 2024 09:26
Show Gist options
  • Save cyco130/a619d914de120cf05e0127f6901164b3 to your computer and use it in GitHub Desktop.
Save cyco130/a619d914de120cf05e0127f6901164b3 to your computer and use it in GitHub Desktop.
Simple deployment manager
# This script also runs on the VPS.
# It handles the bits that require
# root access.
set -e
if [ $(id -u) != 0 ]; then
echo "You're not root"
exit 1
fi
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: dploy-nginx <config-filename> <site-name>"
exit 1
fi
install $1 /etc/nginx/sites-available/$2
ln -sfn /etc/nginx/sites-available/$2 /etc/nginx/sites-enabled/
echo "Reloading nginx configuration"
service nginx reload
# This script runs on the VPS
set -e
PATH=/home/cyco/.npm-global/bin:$PATH
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: dploy <package-file> <website>"
exit 1
fi
PACKAGES_DIR=packages
DEPLOY_DIR=dploy
cd ~
package_file_name=$1
website=$2
package_name=$(echo $(basename $package_file_name | sed 's/-[0-9]\+\.[0-9]\+\.[0-9]\+.*//'))
time_stamp=$(date +"%F-%H%M%S")
mkdir -p $DEPLOY_DIR/$website
echo Extracting $package_file_name to $DEPLOY_DIR/$website/$time_stamp
tar -xf $package_file_name -C $DEPLOY_DIR/$website
mv $DEPLOY_DIR/$website/package $DEPLOY_DIR/$website/$time_stamp
cd $DEPLOY_DIR/$website/$time_stamp
if [ -e ../.env ]; then
echo Copying previous .env file
cp ../.env .
else
echo This is a first-time deployment, create an .env file first.
exit 1
fi
echo Installing production dependencies
NODE_ENV=production pnpm install --prod
clean_website=`echo "$website" | tr '*' '_'`
# Parse JSON array
previous_ids=`pm2 id $clean_website | tr -d '[] ' | tr ',' ' '`
echo Starting the application
port_number=`free-port -p 3000`
PORT=$port_number dotenv -- pm2 start --name $clean_website 'pnpm start'
echo "Waiting for the application to become online on port $port_number"
timeout 10 sh -c 'until nc -z $0 $1; do sleep 1; done' 0.0.0.0 $port_number
# Create/update nginx config
nginx_config="server {
server_name $website;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/cyco130.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cyco130.com/privkey.pem;
location / {
try_files /nonexistent @$http_upgrade;
}
location @websocket {
proxy_pass http://127.0.0.1:$port_number;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \"Upgrade\";
proxy_set_header Host \$host;
}
location @ {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host \$host:\$server_port;
proxy_set_header X-Forwarded-Server \$host;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:$port_number;
proxy_buffering off;
}
}
"
echo Installing nginx configuration to /etc/nginx/sites-available/$clean_website
echo "$nginx_config" > /tmp/$clean_website
sudo dploy-nginx /tmp/$clean_website $clean_website
if [ "$previous_ids" ]; then
echo "Killing previous application $previous_ids"
pm2 delete $previous_ids
fi
echo "Deleting old deployments"
cd ..
rm -rf `ls | grep -v $time_stamp`
echo "$package_name successfully deployed, it's now running on https://$website"
# This script runs on my local computer. I build locally and
# upload the .tgz file via scp.
set -e
if [ -z "$1" ]; then
echo "Usage: uploy <website>"
exit 1
fi
SERVER=linode
PACKAGES_DIR=packages
echo "Packaging the application"
pnpm pack
package_file_name=$(ls -t *.tgz | head -1)
echo "Uploading package file $package_file_name"
scp $package_file_name $SERVER:$PACKAGES_DIR
echo "Calling remote deploy script"
ssh -t $SERVER dploy $PACKAGES_DIR/$package_file_name $1
echo "Removing package file"
rm $package_file_name
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment