Added deployment scripts

This commit is contained in:
2025-02-16 22:22:30 +11:00
parent fb1168a7cc
commit 5892d6a7e6
5 changed files with 209 additions and 2 deletions

94
deploy/deploy_staging.sh Normal file
View File

@@ -0,0 +1,94 @@
#!/bin/bash
# Exit on error
set -e
# Check if commit hash is passed as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <commit-hash>"
exit 1
fi
COMMIT_HASH=$1
RELEASES_DIR="/home/deploy/releases/staging"
DEPLOY_BIN="/home/deploy/staging/projectreshoot"
SERVICE_NAME="staging.projectreshoot"
BINARY_NAME="projectreshoot-${COMMIT_HASH}"
declare -a PORTS=("3005" "3006" "3007")
# Check if the binary exists
if [ ! -f "${RELEASES_DIR}/${BINARY_NAME}" ]; then
echo "Binary ${BINARY_NAME} not found in ${RELEASES_DIR}"
exit 1
fi
# Keep a reference to the previous binary from the symlink
if [ -L "${DEPLOY_BIN}" ]; then
PREVIOUS=$(readlink -f $DEPLOY_BIN)
echo "Current binary is ${PREVIOUS}, saved for rollback."
else
echo "No symbolic link found, no previous binary to backup."
PREVIOUS=""
fi
rollback_deployment() {
if [ -n "$PREVIOUS" ]; then
echo "Rolling back to previous binary: ${PREVIOUS}"
ln -sfn "${PREVIOUS}" "${DEPLOY_BIN}"
else
echo "No previous binary to roll back to."
fi
# wait to restart the services
sleep 10
# Restart all services with the previous binary
for port in "${PORTS[@]}"; do
SERVICE="${SERVICE_NAME}@${port}.service"
echo "Restarting $SERVICE..."
sudo systemctl restart $SERVICE
done
echo "Rollback completed."
}
# Copy the binary to the deployment directory
echo "Promoting ${BINARY_NAME} to ${DEPLOY_BIN}..."
ln -sf "${RELEASES_DIR}/${BINARY_NAME}" "${DEPLOY_BIN}"
WAIT_TIME=5
restart_service() {
local port=$1
local SERVICE="${SERVICE_NAME}@${port}.service"
echo "Restarting ${SERVICE}..."
# Restart the service
if ! sudo systemctl restart "$SERVICE"; then
echo "Error: Failed to restart ${SERVICE}. Rolling back deployment."
# Call the rollback function
rollback_deployment
exit 1
fi
# Wait a few seconds to allow the service to fully start
echo "Waiting for ${SERVICE} to fully start..."
sleep $WAIT_TIME
# Check the status of the service
if ! systemctl is-active --quiet "${SERVICE}"; then
echo "Error: ${SERVICE} failed to start correctly. Rolling back deployment."
# Call the rollback function
rollback_deployment
exit 1
fi
echo "${SERVICE}.service restarted successfully."
}
for port in "${PORTS[@]}"; do
restart_service $port
done
echo "Deployment completed successfully."

View File

@@ -0,0 +1,29 @@
[Unit]
Description=Project Reshoot %i
After=network.target
[Service]
ExecStart=/home/deploy/production/projectreshoot
WorkingDirectory=/home/deploy/production
User=deploy
Group=deploy
EnvironmentFile=/etc/env/projectreshoot.env
Environment="HOST=127.0.0.1"
Environment="PORT=%i"
Environment="TRUSTED_HOST=projectreshoot.com"
Environment="SSL=true"
Environment="GZIP=true"
Environment="LOG_LEVEL=info"
Environment="LOG_OUTPUT=file"
Environment="LOG_DIR=/home/deploy/production/logs"
LimitNOFILE=65536
Restart=on-failure
TimeoutSec=30
PrivateTmp=true
NoNewPrivilages=true
AmbientCapabilites=CAP_NET_BIND_SERVICE
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,29 @@
[Unit]
Description=Project Reshoot Staging %i
After=network.target
[Service]
ExecStart=/home/deploy/staging/projectreshoot
WorkingDirectory=/home/deploy/staging
User=deploy
Group=deploy
EnvironmentFile=/etc/env/staging.projectreshoot.env
Environment="HOST=127.0.0.1"
Environment="PORT=%i"
Environment="TRUSTED_HOST=staging.projectreshoot.com"
Environment="SSL=true"
Environment="GZIP=true"
Environment="LOG_LEVEL=debug"
Environment="LOG_OUTPUT=both"
Environment="LOG_DIR=/home/deploy/staging/logs"
LimitNOFILE=65536
Restart=on-failure
TimeoutSec=30
PrivateTmp=true
ProtectSystem=full
ProtectHome=yes
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target