diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml new file mode 100644 index 0000000..a1b7936 --- /dev/null +++ b/.gitea/workflows/ci-cd.yml @@ -0,0 +1,93 @@ +# Gitea Actions. While .gitea/workflows exists Gitea ignores .github/workflows (GitHub Pages only). +# +# Every push and pull request: install, test and build. +# Push to main: then deploy to the server over SSH and start it with docker compose. +# +# Repository settings → Actions → Secrets: +# DEPLOY_HOST server address (deploy is skipped while this is empty) +# DEPLOY_SSH_KEY private key whose public key is in the server user's ~/.ssh/authorized_keys +# DEPLOY_USER SSH user, default root; it must be able to run docker +# DEPLOY_PORT SSH port, default 22 +# DEPLOY_KNOWN_HOSTS optional output of `ssh-keyscan `; otherwise the host key is scanned on each run +# Repository settings → Actions → Variables (optional): +# DEPLOY_PATH directory on the server, default vending-machine-game (in the user's home) +# SITE_ADDRESS domains served with HTTPS, default "robotmarkets.ir, www.robotmarkets.ir" +# HEALTH_URL URL checked after deploy, e.g. https://robotmarkets.ir/ +name: CI/CD + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +# Deploys run one at a time, in push order. +concurrency: + group: ci-cd-${{ github.ref }} + cancel-in-progress: false + +jobs: + test: + runs-on: ubuntu-latest + container: + image: node:22-bookworm + steps: + - uses: actions/checkout@v4 + - name: Install + run: npm ci --no-audit --no-fund + - name: Test + run: node --test tests/*.test.js + - name: Build + run: npm run build + + deploy: + needs: test + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + container: + image: node:22-bookworm + env: + DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }} + DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }} + DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} + DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} + SITE_ADDRESS: ${{ vars.SITE_ADDRESS }} + HEALTH_URL: ${{ vars.HEALTH_URL }} + steps: + - uses: actions/checkout@v4 + - name: Deploy to the server + run: | + if [ -z "$DEPLOY_HOST" ] || [ -z "$DEPLOY_SSH_KEY" ]; then + echo "DEPLOY_HOST / DEPLOY_SSH_KEY secrets are not set: skipping deploy." + exit 0 + fi + port="${DEPLOY_PORT:-22}" + target="${DEPLOY_USER:-root}@${DEPLOY_HOST}" + dir="${DEPLOY_PATH:-vending-machine-game}" + site="${SITE_ADDRESS:-robotmarkets.ir, www.robotmarkets.ir}" + mkdir -p ~/.ssh && chmod 700 ~/.ssh + printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/deploy_key && chmod 600 ~/.ssh/deploy_key + if [ -n "$DEPLOY_KNOWN_HOSTS" ]; then + printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts + else + ssh-keyscan -p "$port" "$DEPLOY_HOST" > ~/.ssh/known_hosts 2>/dev/null + fi + # Ship the checked-out sources (no git history) and rebuild/restart the container there. + tar czf - --exclude=./.git --exclude=./node_modules --exclude=./dist . \ + | ssh -i ~/.ssh/deploy_key -p "$port" "$target" \ + "mkdir -p '$dir' && tar xzf - -C '$dir' && cd '$dir' && SITE_ADDRESS='$site' docker compose up -d --build --remove-orphans && docker image prune -f" + + - name: Check the site + if: env.HEALTH_URL != '' && env.DEPLOY_HOST != '' + run: | + for attempt in $(seq 1 20); do + if curl -fsS -o /dev/null "$HEALTH_URL"; then + echo "Site is up: $HEALTH_URL" + exit 0 + fi + sleep 6 + done + echo "Site did not answer at $HEALTH_URL" >&2 + exit 1 diff --git a/README.md b/README.md index 8d6a844..3daf05b 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,31 @@ docker compose logs -f ssh user@server 'cd ~/game && gunzip -c robot-market-game.tar.gz | docker load && docker compose up -d --no-build' ``` +## CI/CD روی Gitea + +مخزن اصلی `ssh://git@git.my-rm.com:2222/Hamed/vending-machine-game.git` با نام `origin` است. GitHub با نام `github` باقی مانده. گردش‌کار [.gitea/workflows/ci-cd.yml](.gitea/workflows/ci-cd.yml) با هر push خودش اجرا می‌شود: + +- **هر push و pull request:** نصب، تست‌ها و build +- **push به `main`:** بعد از قبولی تست‌ها، کد از راه SSH به سرور فرستاده می‌شود و `docker compose up -d --build` اجرا می‌شود. + +**راه‌اندازی یک‌باره:** +1. در سرور، Docker و Docker Compose نصب باشد و کاربر SSH بتواند `docker` اجرا کند (عضو گروه `docker` یا root). +2. یک کلید SSH مخصوص انتشار بسازید و کلید عمومی را به `~/.ssh/authorized_keys` سرور اضافه کنید: + ```sh + ssh-keygen -t ed25519 -N '' -f deploy_key -C gitea-deploy + ssh-copy-id -i deploy_key.pub user@server + ``` +3. در Gitea، بخش Settings → Actions → Secrets مخزن، این‌ها را تعریف کنید: + - `DEPLOY_HOST`: آدرس سرور + - `DEPLOY_SSH_KEY`: محتوای فایل `deploy_key` + - در صورت نیاز: `DEPLOY_USER` (پیش‌فرض root)، `DEPLOY_PORT` (پیش‌فرض ۲۲) و `DEPLOY_KNOWN_HOSTS` (خروجی `ssh-keyscan server`) +4. اختیاری، در بخش Variables: + - `DEPLOY_PATH`: پوشهٔ روی سرور + - `SITE_ADDRESS`: دامنه‌ها + - `HEALTH_URL`: مثلاً `https://robotmarkets.ir/`، تا بعد از انتشار بالا آمدن سایت چک شود + +تا وقتی `DEPLOY_HOST` تعریف نشده، مرحلهٔ انتشار رد می‌شود و فقط تست و build اجرا می‌شوند. + ## ساخت دوبارهٔ دادهٔ نقشه `src/data/mellat.json` و `src/data/iran.json` از خروجی خام OpenStreetMap (Overpass API) و Natural Earth ساخته می‌شوند. کوئری‌ها در ابتدای اسکریپت آمده‌اند: