ci: сборка, деплой и стенды в Gitea Actions
Workflows CI, деплоя на прод и очистки стендов веток, docker-compose для прода и стейджинга, публикация документации.
This commit is contained in:
1 parent
80808a1e75
commit
7ffba57a13
6 files changed
+483
No files matched your search
@@ -0,0 +1,4 @@
|
||||
title: 'GEO Helm'
|
||||
project:
|
||||
org: 'geo-helm'
|
||||
repo: 'geo-platform'
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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:
|
||||
@@ -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
|
||||
Reference in new issue
Block a user