diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fbd46f5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +node_modules +.git +.idea +.vscode +.agents +.claude +.windsurf +.yarn +.nx +.gitea +test +src +prisma +docs +.env +.env.* +*.md +docker-compose*.yml diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..5b28091 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,117 @@ +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: Setup Yarn 4 + run: npm install -g corepack && corepack enable + + - name: Install dependencies + run: yarn install --immutable + + - name: Build + run: yarn build + + - name: Build api docker image + uses: docker/build-push-action@v7 + with: + context: . + file: Dockerfile + push: true + tags: git.jt4d.ru/${{ github.repository }}/api:${{ github.head_ref || github.ref_name }} + cache-from: git.jt4d.ru/${{ github.repository }}/api:latest + labels: | + org.opencontainers.image.ref.name=${{ github.head_ref || github.ref_name }} + + deploy-staging: + name: Деплой на стейджинг + runs-on: ubuntu-latest + needs: build + env: + DEPLOY_URL: https://${{ github.head_ref || github.ref_name }}.it-smi.jt4d.ru.net + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Deploy via SSH + uses: jt4d-lab/ssh-action@v1 + env: + 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 }} + workdir: /home/${{ vars.STAGING_USER }}/${{ github.event.repository.name }}_${{ github.head_ref || github.ref_name }} + files: docker-compose.staging.yml + script_stop: true + envs: BRANCH + script: | + mv -f docker-compose.staging.yml docker-compose.yml + # \${BRANCH} — литеральный плейсхолдер в docker-compose.staging.yml, + # $BRANCH — имя ветки из окружения. + sed -i "s/\${BRANCH}/$BRANCH/g" docker-compose.yml + docker compose pull --quiet + staging-db-postgres create "${{ github.repository }}" "$BRANCH" + docker compose down + docker compose up -d + + docker system prune -f + + - name: Set deployment status + env: + BRANCH: ${{ github.head_ref || github.ref_name }} + 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 }}" + + - name: Write stand URL to summary + run: echo "**Стенд:** $DEPLOY_URL" >> "$GITHUB_STEP_SUMMARY" + + check: + name: Проверка кода + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Yarn 4 + run: npm install -g corepack && corepack enable + + - name: Install dependencies + run: yarn install --immutable + + - name: Lint + run: yarn lint diff --git a/.gitea/workflows/cleanup.yaml b/.gitea/workflows/cleanup.yaml new file mode 100644 index 0000000..12d4eb1 --- /dev/null +++ b/.gitea/workflows/cleanup.yaml @@ -0,0 +1,63 @@ +name: Cleanup + +on: + delete: + pull_request: + types: [closed] + workflow_dispatch: + +concurrency: + group: cleanup-${{ github.event.pull_request.head.ref || github.event.ref || github.ref_name }} + +jobs: + cleanup: + name: Удаление стенда + runs-on: ubuntu-latest + if: >- + github.event_name == 'workflow_dispatch' || + (github.event_name == 'delete' && github.event.ref_type == 'branch') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name == github.repository) + steps: + - name: Удаление стенда + uses: jt4d-lab/ssh-action@v1 + with: + host: ${{ vars.STAGING_HOST }} + username: ${{ vars.STAGING_USER }} + key: ${{ secrets.STAGING_SSH_PRIVATE_KEY }} + port: ${{ vars.STAGING_PORT }} + script_stop: true + script: | + BRANCH_NAME="${{ github.event.ref || github.event.pull_request.head.ref || github.ref_name }}" + 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 + # Стек гасим до удаления базы: DROP DATABASE не пройдёт при живом подключении api. + if [ -d "$DEPLOY_DIR" ]; then + cd "$DEPLOY_DIR" + docker compose down --rmi all || true + fi + staging-db-postgres delete "${{ github.repository }}" "$BRANCH_NAME" + cd "$HOME" + rm -rf "$DEPLOY_DIR" + docker system prune -f + + - name: Удаление docker образов + run: | + BRANCH_NAME="${{ github.event.ref || github.event.pull_request.head.ref || github.ref_name }}" + BRANCH_NAME="${BRANCH_NAME#refs/heads/}" + OWNER="${{ github.repository_owner }}" + REPO_NAME="${{ github.event.repository.name }}" + SERVER="${{ github.server_url }}" + + for IMAGE in 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/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2159ed1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM git.jt4d.ru/jt4d/node:24-alpine + +ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0 + +WORKDIR /app + +RUN corepack enable + +COPY package.json yarn.lock .yarnrc.yml ./ +RUN yarn install --immutable && yarn cache clean + +COPY ./dist /app + +RUN apk add --no-cache curl + +ENV NODE_ENV=production +ENV PORT=5000 + +EXPOSE 5000 + +HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=15s \ + CMD curl -f http://localhost:5000/ || exit 1 + +CMD ["node", "main.js"] diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..29dfbbc --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,23 @@ +services: + api: + container_name: it-smi_${BRANCH}_api + image: git.jt4d.ru/public/it-smi/api:${BRANCH} + environment: + # POSTGRES_* пишет staging-db-postgres в .env каталога деплоя. + # Синтаксис ${X:-} намеренно необязательный: ${X:?} валит и pull, и down. + POSTGRES_USER: ${POSTGRES_USER:-} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-} + POSTGRES_DB: ${POSTGRES_DB:-} + POSTGRES_HOST: ${POSTGRES_HOST:-} + POSTGRES_PORT: ${POSTGRES_PORT:-5432} + labels: + caddy: ${BRANCH}.it-smi.jt4d.ru.net + caddy.reverse_proxy: '{{upstreams 5000}}' + restart: always + init: true + networks: + - 'net' + +networks: + net: + external: true diff --git a/package.json b/package.json index 0e7f7ad..759f70e 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "private": true, "license": "UNLICENSED", "type": "module", + "packageManager": "yarn@4.18.0", "scripts": { "build": "nest build", "deploy": "nest deploy",