File size: 1,641 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# Build the static showcase and copy it to the Lightsail server.
# Run from the project root in Git Bash:
#   export WEB_HOST=<static IP>  WEB_KEY=<path to .pem>  [STATUS_URL=<Lambda function URL>]
#   bash deployment/aws/deploy_site.sh
set -euo pipefail

: "${WEB_HOST:?Set WEB_HOST to the Lightsail static IP or domain}"
: "${WEB_KEY:?Set WEB_KEY to the path of the SSH private key}"
USER_NAME="${WEB_USER:-ubuntu}"
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
STAGE="$(mktemp -d)"
ARCHIVE="$(mktemp -u).tgz"
trap 'rm -rf "$STAGE" "$ARCHIVE"' EXIT

echo "1/4 Building the static showcase..."
( cd "$ROOT/frontend" && npm ci && npm run build:showcase )

echo "2/4 Preparing files..."
cp -r "$ROOT/frontend/dist-static/." "$STAGE/"
sed "s#__STATUS_URL__#${STATUS_URL:-}#g" "$ROOT/deployment/aws/site/status.html" > "$STAGE/status.html"
[ -f "$STAGE/index.html" ] || { echo "index.html missing. Build failed?"; exit 1; }
tar -C "$STAGE" -czf "$ARCHIVE" .

echo "3/4 Uploading to $WEB_HOST ..."
scp -i "$WEB_KEY" -o StrictHostKeyChecking=accept-new "$ARCHIVE" "$USER_NAME@$WEB_HOST:/tmp/site.tgz"

echo "4/4 Switching the live site..."
ssh -i "$WEB_KEY" "$USER_NAME@$WEB_HOST" '
  set -e
  NEW=$(sudo mktemp -d /var/www/defectrag-new.XXXXXX)
  sudo tar -xzf /tmp/site.tgz -C "$NEW" && rm -f /tmp/site.tgz
  sudo rm -rf /var/www/defectrag-old
  [ -d /var/www/defectrag ] && sudo mv /var/www/defectrag /var/www/defectrag-old
  sudo mv "$NEW" /var/www/defectrag
  sudo chown -R www-data:www-data /var/www/defectrag
  sudo chmod 755 /var/www/defectrag
  sudo nginx -t && sudo systemctl reload nginx
'
echo "Done. Open http://$WEB_HOST"