Files

55 lines
1.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 把 gitea-mail-notify 装成 systemd 服务(需要 Node >= 18)
set -euo pipefail
[ "$(id -u)" = 0 ] || { echo "用 root 跑:sudo bash install.sh"; exit 1; }
DIR="${DIR:-/opt/gitea-mail-notify}"
NODE_BIN="$(command -v node || true)"
[ -n "$NODE_BIN" ] || { echo "没找到 node,先装 Node 18+"; exit 1; }
NODE_BIN="$(readlink -f "$NODE_BIN")"
mkdir -p "$DIR"
cp -f server.mjs add-hooks.mjs package.json README.md "$DIR"/
if [ ! -f /etc/gitea-mail-notify.json ]; then
SECRET="$(head -c 18 /dev/urandom | base64 | tr -d '/+=' | cut -c1-24)"
cat > /etc/gitea-mail-notify.json <<JSON
{
"secret": "${SECRET}",
"port": 8095,
"smtpHost": "127.0.0.1",
"smtpPort": 25,
"mailFrom": "gitea@localhost",
"mailTo": "gitea@localhost",
"subjectPrefix": "[Gitea]"
}
JSON
chmod 600 /etc/gitea-mail-notify.json
echo "已生成 /etc/gitea-mail-notify.json(记得改 mailFrom / mailTo)"
fi
cat > /etc/systemd/system/gitea-mail-notify.service <<UNIT
[Unit]
Description=Gitea push → 邮件通知
After=network-online.target docker.service
Wants=network-online.target
[Service]
Type=simple
Environment=NODE_ENV=production
ExecStart=${NODE_BIN} ${DIR}/server.mjs
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now gitea-mail-notify
sleep 1
systemctl is-active gitea-mail-notify
SECRET_SHOWN="$(node -e "console.log(require('/etc/gitea-mail-notify.json').secret)" 2>/dev/null || jq -r .secret /etc/gitea-mail-notify.json)"
echo "服务已启动。Gitea 里填:http://127.0.0.1:8095/hook/${SECRET_SHOWN}"