ci: deploy persistent systemd service
Verify and deploy production / verify-and-deploy (push) Failing after 2s
Verify and deploy production / verify-and-deploy (push) Failing after 2s
This commit is contained in:
+82
-24
@@ -13,12 +13,17 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
verify-and-deploy:
|
verify-and-deploy:
|
||||||
runs-on: ces-control
|
runs-on: ces-control
|
||||||
container: node:24-bookworm-slim
|
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 24
|
||||||
|
cache: npm
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
|
|
||||||
@@ -31,40 +36,93 @@ jobs:
|
|||||||
- name: Production build
|
- name: Production build
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
|
||||||
- name: Deploy persistent production container
|
- name: Deploy persistent production service
|
||||||
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
||||||
env:
|
env:
|
||||||
STUDIO_USERNAME: studio
|
STUDIO_USERNAME: studio
|
||||||
STUDIO_PASSWORD: ${{ secrets.STUDIO_PASSWORD }}
|
STUDIO_PASSWORD: ${{ secrets.STUDIO_PASSWORD }}
|
||||||
DEPLOY_PORT: ${{ vars.DEPLOY_PORT }}
|
DEPLOY_PORT: ${{ vars.DEPLOY_PORT }}
|
||||||
DEPLOY_DOCKERFILE: Dockerfile.deploy
|
RELEASE_SHA: ${{ gitea.sha }}
|
||||||
GITHUB_SHA: ${{ gitea.sha }}
|
|
||||||
run: |
|
run: |
|
||||||
|
set -eu
|
||||||
if [ -z "$STUDIO_PASSWORD" ]; then
|
if [ -z "$STUDIO_PASSWORD" ]; then
|
||||||
echo "STUDIO_PASSWORD repository secret is required for production deployment"
|
echo "STUDIO_PASSWORD repository secret is required for production deployment"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
node scripts/deploy.mjs
|
|
||||||
|
|
||||||
- name: Verify persistent deployment settings
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
SUDO=""
|
||||||
run: |
|
elif sudo -n true 2>/dev/null; then
|
||||||
node --input-type=module <<'JS'
|
SUDO="sudo -n"
|
||||||
import Docker from "dockerode";
|
else
|
||||||
|
echo "The ces-control runner needs root or passwordless sudo for persistent systemd deployment"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
|
PORT="${DEPLOY_PORT:-3000}"
|
||||||
const container = docker.getContainer("rednote-tool-studio");
|
APP_ROOT=/opt/rednote-tool-studio
|
||||||
const state = await container.inspect();
|
RELEASE_DIR="$APP_ROOT/releases/$RELEASE_SHA"
|
||||||
const restartPolicy = state.HostConfig.RestartPolicy?.Name;
|
DATA_ROOT=/var/lib/rednote-tool-studio
|
||||||
const health = state.State.Health?.Status;
|
SERVICE_USER=rednote-studio
|
||||||
const dataMount = state.Mounts?.find((mount) => mount.Destination === "/app/data/projects");
|
|
||||||
|
|
||||||
if (!state.State.Running) throw new Error("Production container is not running");
|
if ! id "$SERVICE_USER" >/dev/null 2>&1; then
|
||||||
if (health !== "healthy") throw new Error(`Production container health is ${health || "unavailable"}`);
|
$SUDO useradd --system --home "$DATA_ROOT" --shell /usr/sbin/nologin "$SERVICE_USER"
|
||||||
if (restartPolicy !== "unless-stopped") throw new Error(`Unexpected restart policy: ${restartPolicy || "none"}`);
|
fi
|
||||||
if (dataMount?.Type !== "volume" || dataMount.Name !== "rednote-tool-studio-data") {
|
|
||||||
throw new Error("Persistent project data volume is not mounted correctly");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Persistent deployment verified: health=${health}, restart=${restartPolicy}, volume=${dataMount.Name}`);
|
$SUDO mkdir -p "$APP_ROOT/releases" "$APP_ROOT/bin" "$DATA_ROOT/projects"
|
||||||
JS
|
$SUDO install -m 0755 "$(command -v node)" "$APP_ROOT/bin/node"
|
||||||
|
$SUDO mkdir -p "$RELEASE_DIR/.next" "$RELEASE_DIR/data"
|
||||||
|
$SUDO cp -a .next/standalone/. "$RELEASE_DIR/"
|
||||||
|
$SUDO cp -a .next/static "$RELEASE_DIR/.next/static"
|
||||||
|
$SUDO cp -a templates vendor "$RELEASE_DIR/"
|
||||||
|
$SUDO ln -sfn "$DATA_ROOT/projects" "$RELEASE_DIR/data/projects"
|
||||||
|
$SUDO chown -R root:root "$APP_ROOT"
|
||||||
|
$SUDO chown -R "$SERVICE_USER:$SERVICE_USER" "$DATA_ROOT"
|
||||||
|
$SUDO ln -sfn "$RELEASE_DIR" "$APP_ROOT/current"
|
||||||
|
|
||||||
|
printf '%s' "$STUDIO_PASSWORD" | $SUDO tee /etc/rednote-tool-studio.password >/dev/null
|
||||||
|
$SUDO chmod 600 /etc/rednote-tool-studio.password
|
||||||
|
|
||||||
|
cat > rednote-tool-studio.service <<EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Rednote Content Studio
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$SERVICE_USER
|
||||||
|
Group=$SERVICE_USER
|
||||||
|
WorkingDirectory=$APP_ROOT/current
|
||||||
|
Environment=NODE_ENV=production
|
||||||
|
Environment=HOSTNAME=0.0.0.0
|
||||||
|
Environment=PORT=$PORT
|
||||||
|
Environment=STUDIO_USERNAME=$STUDIO_USERNAME
|
||||||
|
ExecStart=/bin/sh -c 'export STUDIO_PASSWORD="\$(cat /etc/rednote-tool-studio.password)"; exec $APP_ROOT/bin/node server.js'
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
$SUDO install -m 0644 rednote-tool-studio.service /etc/systemd/system/rednote-tool-studio.service
|
||||||
|
$SUDO systemctl daemon-reload
|
||||||
|
$SUDO systemctl enable --now rednote-tool-studio.service
|
||||||
|
$SUDO systemctl restart rednote-tool-studio.service
|
||||||
|
|
||||||
|
for attempt in $(seq 1 30); do
|
||||||
|
if "$APP_ROOT/bin/node" -e "fetch('http://127.0.0.1:$PORT/api/health').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))"; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
if [ "$attempt" -eq 30 ]; then
|
||||||
|
$SUDO systemctl status rednote-tool-studio.service --no-pager || true
|
||||||
|
$SUDO journalctl -u rednote-tool-studio.service -n 100 --no-pager || true
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
$SUDO systemctl is-enabled rednote-tool-studio.service
|
||||||
|
$SUDO systemctl is-active rednote-tool-studio.service
|
||||||
|
echo "Persistent deployment verified: service=rednote-tool-studio port=$PORT data=$DATA_ROOT/projects release=$RELEASE_SHA"
|
||||||
|
|||||||
Reference in New Issue
Block a user