From 7ffba57a135d8a550ea60dd0408a8079b9640ca4 Mon Sep 17 00:00:00 2001 From: Arswarog Date: Mon, 26 Oct 2026 17:16:13 +0300 Subject: [PATCH] =?UTF-8?q?ci:=20=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B0,=20?= =?UTF-8?q?=D0=B4=D0=B5=D0=BF=D0=BB=D0=BE=D0=B9=20=D0=B8=20=D1=81=D1=82?= =?UTF-8?q?=D0=B5=D0=BD=D0=B4=D1=8B=20=D0=B2=20Gitea=20Actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Workflows CI, деплоя на прод и очистки стендов веток, docker-compose для прода и стейджинга, публикация документации. --- .docuservix.yml | 4 + .gitea/workflows/ci.yaml | 188 +++++++++++++++++++++++++++++++ .gitea/workflows/cleanup.yaml | 50 ++++++++ .gitea/workflows/production.yaml | 154 +++++++++++++++++++++++++ docker-compose.production.yml | 55 +++++++++ docker-compose.staging.yml | 32 ++++++ 6 files changed, 483 insertions(+) create mode 100644 .docuservix.yml create mode 100644 .gitea/workflows/ci.yaml create mode 100644 .gitea/workflows/cleanup.yaml create mode 100644 .gitea/workflows/production.yaml create mode 100644 docker-compose.production.yml create mode 100644 docker-compose.staging.yml diff --git a/.docuservix.yml b/.docuservix.yml new file mode 100644 index 0000000..beaa68e --- /dev/null +++ b/.docuservix.yml @@ -0,0 +1,4 @@ +title: 'GEO Helm' +project: + org: 'geo-helm' + repo: 'geo-platform' diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..3f00c13 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,188 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + types: [opened, reopened, synchronize] + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }} + cancel-in-progress: true + +jobs: + build: + name: Сборка + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Login to Gitea Registry + uses: docker/login-action@v3 + with: + registry: git.jt4d.ru + username: ${{ vars.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Check branch name + run: yarn lint:branch-name + + - name: Check branch name characters + env: + BRANCH: ${{ github.head_ref || github.ref_name }} + run: | + if ! printf '%s' "$BRANCH" | grep -Eq '^[A-Za-z0-9][A-Za-z0-9.-]*$'; then + echo "::error::Недопустимое имя ветки: '$BRANCH'. Разрешены только A-Z a-z 0-9 . -, имя не может быть пустым и начинаться с '-'" + exit 1 + fi + + - name: Check migrations list + run: yarn ts-node tools/buildDataSource.ts --check + + - name: Build + run: yarn build + + - name: Check data-source + run: yarn ts-node tools/checkDataSource.ts + + - name: Build web docker image + uses: docker/build-push-action@v7 + with: + context: apps/web + file: apps/web/Dockerfile + push: true + tags: git.jt4d.ru/geo-helm/geo-platform/web:${{ github.head_ref || github.ref_name }} + cache-from: git.jt4d.ru/geo-helm/geo-platform/web:latest + labels: | + org.opencontainers.image.ref.name=${{ github.head_ref || github.ref_name }} + + - name: Copy + run: cp yarn.lock apps/api/ + + - name: Build api docker image + uses: docker/build-push-action@v7 + with: + context: apps/api + file: apps/api/Dockerfile + push: true + tags: git.jt4d.ru/geo-helm/geo-platform/api:${{ github.head_ref || github.ref_name }} + cache-from: git.jt4d.ru/geo-helm/geo-platform/api:latest + labels: | + org.opencontainers.image.ref.name=${{ github.head_ref || github.ref_name }} + + deploy-staging: + name: Деплой на стейджинг + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Copy docker-compose file + uses: appleboy/scp-action@v1 + with: + host: ${{ vars.STAGING_HOST }} + username: ${{ vars.STAGING_USER }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: ${{ vars.STAGING_PORT }} + source: 'docker-compose.staging.yml' + target: /home/${{ vars.STAGING_USER }}/${{ github.event.repository.name }}_${{ github.head_ref || github.ref_name }} + + - name: Deploy via SSH + uses: appleboy/ssh-action@v1 + env: + DEPLOY_DIR: ${{ github.event.repository.name }}_${{ github.head_ref || github.ref_name }} + BRANCH: ${{ github.head_ref || github.ref_name }} + with: + host: ${{ vars.STAGING_HOST }} + username: ${{ vars.STAGING_USER }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: ${{ vars.STAGING_PORT }} + script_stop: true + envs: DEPLOY_DIR,BRANCH + script: | + set -e + cd "/home/${{ vars.STAGING_USER }}/$DEPLOY_DIR" + mv -f docker-compose.*.yml docker-compose.yml + # \${BRANCH} — литеральный плейсхолдер в docker-compose.staging.yml, + # $BRANCH — имя ветки из окружения. + sed -i "s/\${BRANCH}/$BRANCH/g" docker-compose.yml + docker compose pull --quiet + docker compose down + docker compose up -d + + # echo "Список миграций" + # docker compose exec -T api node_modules/.bin/typeorm migration:show -d data-source.js + # echo "Применение миграций" + # docker compose exec -T api node_modules/.bin/typeorm migration:run -t each -d data-source.js + + # Если migration:run упадёт, prune под `set -e` не выполнится — + # почистится на следующем деплое. + docker system prune -f + + - name: Set deployment status + env: + BRANCH: ${{ github.head_ref || github.ref_name }} + DEPLOY_URL: http://${{ github.head_ref || github.ref_name }}.geo-platform.geo-helm.jt4d.ru.net + run: | + curl -s -X POST \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d '{ + "state": "success", + "context": "Стенд", + "description": "'"$BRANCH"'", + "target_url": "'"$DEPLOY_URL"'" + }' \ + "${{ github.server_url }}/api/v1/repos/${{ github.repository }}/statuses/${{ github.sha }}" + + check: + name: Проверка кода + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Login to Gitea Registry + uses: docker/login-action@v3 + with: + registry: git.jt4d.ru + username: ${{ vars.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Lint + run: yarn lint:check + + docs: + name: Документация + runs-on: ubuntu-latest + needs: build + container: + image: git.jt4d.ru/jt4d/gitea-runner:latest + credentials: + username: ${{ vars.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + volumes: + - '/usr/local/share/.cache:/usr/local/share/.cache' + + env: + YARN_CACHE_FOLDER: /usr/local/share/.cache/yarn + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Build and deploy docs + uses: https://git.jt4d.ru/docuservix/docs@next + with: + prefix: wiki + on-broken-links: warn diff --git a/.gitea/workflows/cleanup.yaml b/.gitea/workflows/cleanup.yaml new file mode 100644 index 0000000..ec78108 --- /dev/null +++ b/.gitea/workflows/cleanup.yaml @@ -0,0 +1,50 @@ +name: Cleanup + +on: + delete: + +jobs: + cleanup: + name: Удаление стенда + runs-on: ubuntu-latest + if: github.event.ref_type == 'branch' + steps: + - name: Удаление стенда + uses: appleboy/ssh-action@v1 + with: + host: ${{ vars.STAGING_HOST }} + username: ${{ vars.STAGING_USER }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: ${{ vars.STAGING_PORT }} + script: | + BRANCH_NAME="${{ github.event.ref }}" + BRANCH_NAME="${BRANCH_NAME#refs/heads/}" + DEPLOY_DIR="/home/${{ vars.STAGING_USER }}/${{ github.event.repository.name }}_${BRANCH_NAME}" + echo "Deleting stand for branch: ${BRANCH_NAME}" + echo $DEPLOY_DIR + if [ -d "$DEPLOY_DIR" ]; then + cd "$DEPLOY_DIR" + docker compose down --rmi all || true + cd .. + rm -rf "$DEPLOY_DIR" + docker system prune -f + fi + + - name: Удаление docker образов + run: | + BRANCH_NAME="${{ github.event.ref }}" + BRANCH_NAME="${BRANCH_NAME#refs/heads/}" + OWNER="${{ github.repository_owner }}" + REPO_NAME="${{ github.event.repository.name }}" + SERVER="${{ github.server_url }}" + + for IMAGE in web api; do + PACKAGE="${REPO_NAME}%2F${IMAGE}" + URL="${SERVER}/api/v1/packages/${OWNER}/container/${PACKAGE}/${BRANCH_NAME}" + echo "Deleting ${URL}" + curl -i -sS \ + -X DELETE \ + -H "Authorization: token ${{ secrets.SERVICE_BOT_TOKEN }}" \ + "${URL}" || true + echo + done diff --git a/.gitea/workflows/production.yaml b/.gitea/workflows/production.yaml new file mode 100644 index 0000000..2ca6a2d --- /dev/null +++ b/.gitea/workflows/production.yaml @@ -0,0 +1,154 @@ +name: Production + +on: + push: + tags: + - v* + workflow_dispatch: + inputs: + logLevel: + description: 'Log level' + required: true + default: 'info' + type: choice + options: + - verbose + - debug + - info + - warn + +concurrency: + group: production-deploy + cancel-in-progress: false + +jobs: + build: + name: Сборка + runs-on: ubuntu-latest + outputs: + version: ${{ steps.version.outputs.VERSION }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Login to Gitea Registry + uses: docker/login-action@v3 + with: + registry: git.jt4d.ru + username: ${{ vars.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Compute version + id: version + env: + REF_NAME: ${{ github.ref_name }} + run: | + VERSION=$(git describe --tags --exact-match 2>/dev/null \ + || echo "$REF_NAME-$(git rev-parse --short HEAD)") + # Единственная проверка VERSION в пайплайне: дальше он уходит и в шаги + # этого job, и в deploy-production через needs.build.outputs.version. + if ! printf '%s' "$VERSION" | grep -Eq '^[A-Za-z0-9][A-Za-z0-9.-]*$'; then + echo "::error::Недопустимый VERSION: '$VERSION'. Разрешены только A-Z a-z 0-9 . -, значение не может быть пустым и начинаться с '-'" + exit 1 + fi + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT + echo "**Версия:** \`$VERSION\`" >> $GITHUB_STEP_SUMMARY + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - name: Check migrations list + run: yarn ts-node tools/buildDataSource.ts --check + + - name: Build + run: yarn build + env: + APP_VERSION: ${{ steps.version.outputs.version }} + + - name: Check data-source + run: yarn ts-node tools/checkDataSource.ts + + - name: Build web docker image + uses: docker/build-push-action@v7 + with: + context: apps/web + file: apps/web/Dockerfile + push: true + tags: git.jt4d.ru/geo-helm/geo-platform/web:${{ steps.version.outputs.VERSION }} + cache-from: git.jt4d.ru/geo-helm/geo-platform/web:master + labels: | + org.opencontainers.image.ref.name=${{ steps.version.outputs.VERSION }} + + - name: Copy + run: cp yarn.lock apps/api/ + + - name: Build api docker image + uses: docker/build-push-action@v7 + with: + context: apps/api + file: apps/api/Dockerfile + push: true + tags: git.jt4d.ru/geo-helm/geo-platform/api:${{ steps.version.outputs.VERSION }} + cache-from: git.jt4d.ru/geo-helm/geo-platform/api:master + labels: | + org.opencontainers.image.ref.name=${{ steps.version.outputs.VERSION }} + + deploy-production: + name: Деплой на прод + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup SSH key + uses: shimataro/ssh-key-action@v2 + with: + key: ${{ secrets.PROD_SSH_PRIVATE_KEY }} + known_hosts: unnecessary + + - name: Add prod host to known_hosts + run: ssh-keyscan -p ${{ vars.PROD_PORT }} ${{ vars.PROD_HOST }} >> ~/.ssh/known_hosts + + - name: Copy docker-compose file + run: | + ssh -p ${{ vars.PROD_PORT }} \ + ${{ vars.PROD_USER }}@${{ vars.PROD_HOST }} \ + "mkdir -p /srv/geo-platform" + scp -P ${{ vars.PROD_PORT }} \ + docker-compose.production.yml \ + ${{ vars.PROD_USER }}@${{ vars.PROD_HOST }}:/srv/geo-platform/ + + - name: Deploy via SSH + env: + VERSION: ${{ needs.build.outputs.version }} + LOG_LEVEL: ${{ inputs.logLevel }} + run: | + # VERSION проверен regexp-ом в job build (шаг Compute version) и приходит + # сюда как его output; LOG_LEVEL — свободный ввод, проверяется здесь. + case "$LOG_LEVEL" in + '' | verbose | debug | info | warn) ;; + *) + echo "::error::Недопустимый LOG_LEVEL: '$LOG_LEVEL'" + exit 1 + ;; + esac + + ssh -p ${{ vars.PROD_PORT }} \ + ${{ vars.PROD_USER }}@${{ vars.PROD_HOST }} \ + "VERSION=$VERSION LOG_LEVEL=$LOG_LEVEL bash -s" << 'EOF' + set -e + export VERSION LOG_LEVEL + cd /srv/geo-platform + mv -f docker-compose.production.yml docker-compose.yml + + docker-compose pull --quiet + docker-compose down + docker-compose up -d + + # Ожидания готовности нет намеренно + echo "Список миграций" + docker exec geo-platform_api node_modules/.bin/typeorm migration:show -d data-source.js + echo "Применение миграций" + docker exec geo-platform_api node_modules/.bin/typeorm migration:run -t each -d data-source.js + EOF diff --git a/docker-compose.production.yml b/docker-compose.production.yml new file mode 100644 index 0000000..0f666cf --- /dev/null +++ b/docker-compose.production.yml @@ -0,0 +1,55 @@ +services: + web: + container_name: geo-platform_web + image: git.jt4d.ru/geo-helm/geo-platform/web:${VERSION} + restart: unless-stopped + ports: + - '80:80' + - '443:443' + volumes: + - caddy_data:/data + - caddy_config:/config + + api: + container_name: geo-platform_api + image: git.jt4d.ru/geo-helm/geo-platform/api:${VERSION} + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + env_file: + - .env + environment: + NODE_ENV: production + LOG_LEVEL: ${LOG_LEVEL:-warn} + + POSTGRES_HOST: postgres + POSTGRES_PORT: 5432 + POSTGRES_USER: aigeo_app + POSTGRES_PASSWORD: ${POSTGRES_APP_PASSWORD} + POSTGRES_DB: aigeo + + postgres: + container_name: geo-platform_postgres + image: postgres:16 + restart: unless-stopped + env_file: + - .env + environment: + POSTGRES_DB: ${POSTGRES_DB:-aigeo} + POSTGRES_USER: ${POSTGRES_USER:-postgres} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} + POSTGRES_APP_USER: ${POSTGRES_APP_USER:-aigeo_app} + POSTGRES_APP_PASSWORD: ${POSTGRES_APP_PASSWORD:-postgres} + volumes: + - pg_data:/var/lib/postgresql/data + healthcheck: + test: ['CMD-SHELL', 'pg_isready -U ${POSTGRES_USER:-postgres}'] + interval: 5s + timeout: 5s + retries: 5 + +volumes: + pg_data: + caddy_data: + caddy_config: diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..d846d3f --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,32 @@ +services: + web: + container_name: geo-helm_${BRANCH}_web + image: git.jt4d.ru/geo-helm/geo-platform/web:${BRANCH} + environment: + VIRTUAL_PORT: 80 + VIRTUAL_HOST: ${BRANCH}.geo-platform.geo-helm.jt4d.ru.net + HTTPS_METHOD: noredirect + restart: always + networks: + - 'net' + + api: + container_name: geo-helm_${BRANCH}_api + image: git.jt4d.ru/geo-helm/geo-platform/api:${BRANCH} + environment: + # Приложение и typeorm CLI читают только POSTGRES_*; в .env каталога + # деплоя переменные исторически называются DB_*. Синтаксис ${X:-} + # намеренно необязательный, заданность проверяет ci.yaml (job + # deploy-staging, шаг "Deploy via SSH"): docs/90-deploy/10-migrations.md + POSTGRES_USER: ${DB_USERNAME:-} + POSTGRES_PASSWORD: ${DB_PASSWORD:-} + POSTGRES_DB: ${DB_DATABASE:-} + POSTGRES_HOST: ${DB_HOST:-} + POSTGRES_PORT: ${DB_PORT:-5432} + restart: always + networks: + - 'net' + +networks: + net: + external: true