diff --git a/deploy/runner/docker-compose.forgejo.yml b/deploy/runner/docker-compose.forgejo.yml index 7cda440..5c35464 100644 --- a/deploy/runner/docker-compose.forgejo.yml +++ b/deploy/runner/docker-compose.forgejo.yml @@ -18,23 +18,21 @@ services: forgejo: - image: codeberg.org/forgejo/forgejo:latest + image: gitea/gitea:latest container_name: forgejo restart: always environment: USER_UID: 1000 USER_GID: 1000 - FORGEJO__actions__ENABLED: "true" - FORGEJO__server__ROOT_URL: ${FORGEJO_ROOT_URL:-http://localhost:3000} - FORGEJO__server__SSH_DOMAIN: ${NAS_IP:-localhost} - FORGEJO__server__SSH_PORT: 2222 + GITEA__actions__ENABLED: "true" + GITEA__server__ROOT_URL: ${FORGEJO_ROOT_URL:-http://localhost:3000} + GITEA__server__SSH_DOMAIN: ${NAS_IP:-localhost} + GITEA__server__SSH_PORT: 2222 ports: - "3000:3000" - "2222:22" volumes: - /volume1/docker/forgejo/data:/data - - /etc/timezone:/etc/timezone:ro - - /etc/localtime:/etc/localtime:ro act-runner: image: gitea/act_runner:latest # Forgejo 兼容 Gitea act_runner @@ -50,4 +48,4 @@ services: volumes: - /volume1/docker/forgejo/runner:/data - /var/run/docker.sock:/var/run/docker.sock - - /volume1/backups:/volume1/backups + - /volume1/docker/backups:/volume1/docker/backups diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..dcbb14a --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -e + +cd "$(dirname "$0")/.." + +EC2_HOST="${EC2_HOST:-$(grep EC2_HOST ~/.env 2>/dev/null | cut -d= -f2)}" +EC2_USER="${EC2_USER:-$(grep EC2_USER ~/.env 2>/dev/null | cut -d= -f2)}" +EC2_KEY="${EC2_KEY:-~/.ssh/wangjia.pem}" +FORGEJO_URL="http://192.168.3.200:3000" +FORGEJO_TOKEN="${FORGEJO_TOKEN:-$(grep FORGEJO_TOKEN ~/.env 2>/dev/null | cut -d= -f2)}" + +echo "==> 1/5 Go 测试" +cd backend && go test ./... && cd .. + +echo "==> 2/5 编译 Go 后端 (linux/amd64)" +cd backend +GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o jiu-server . +cd .. + +echo "==> 3/5 编译 Flutter Web" +cd client +flutter build web --release \ + --dart-define=BASE_URL=https://jiu.51yanmei.com \ + --dart-define=PUBLIC_URL=https://jiu.51yanmei.com +cd .. + +echo "==> 4/5 上传到 Forgejo Releases" +tar -czf /tmp/web.tar.gz -C client/build web +VERSION="v$(date +%Y%m%d.%H%M)" +RESP=$(curl -s -X POST "${FORGEJO_URL}/api/v1/repos/wangjia/jiu/releases" \ + -H "Authorization: token ${FORGEJO_TOKEN}" \ + -H "Content-Type: application/json" \ + -d "{\"tag_name\":\"${VERSION}\",\"name\":\"Release ${VERSION}\",\"draft\":false,\"prerelease\":false}") +RELEASE_ID=$(echo "${RESP}" | python3 -c "import sys,json; print(json.load(sys.stdin)['id'])") +curl -sf -X POST "${FORGEJO_URL}/api/v1/repos/wangjia/jiu/releases/${RELEASE_ID}/assets" \ + -H "Authorization: token ${FORGEJO_TOKEN}" -F "attachment=@backend/jiu-server" +curl -sf -X POST "${FORGEJO_URL}/api/v1/repos/wangjia/jiu/releases/${RELEASE_ID}/assets" \ + -H "Authorization: token ${FORGEJO_TOKEN}" -F "attachment=@/tmp/web.tar.gz" +echo "Release ${VERSION} 已创建" + +echo "==> 5/5 部署到 EC2" +scp -O -i ${EC2_KEY} backend/jiu-server ${EC2_USER}@${EC2_HOST}:/tmp/jiu-server +rsync -avz --delete -e "ssh -i ${EC2_KEY}" \ + client/build/web/ ${EC2_USER}@${EC2_HOST}:/tmp/jiu-web-new/ +ssh -i ${EC2_KEY} ${EC2_USER}@${EC2_HOST} << 'ENDSSH' + sudo systemctl stop jiu + cp /tmp/jiu-server /opt/jiu/backend/jiu-server + chmod +x /opt/jiu/backend/jiu-server + sudo systemctl start jiu + for i in $(seq 1 30); do + curl -sf http://localhost:8080/health && break + sleep 2 + done + rm -rf /opt/jiu/web-old + mv /opt/jiu/web /opt/jiu/web-old 2>/dev/null || true + mv /tmp/jiu-web-new /opt/jiu/web + sudo nginx -s reload +ENDSSH + +echo "" +echo "✓ 部署完成!版本: ${VERSION}" +echo " 回滚地址: ${FORGEJO_URL}/wangjia/jiu/releases/tag/${VERSION}"