Files
mostovik-backend/.gitea/workflows/ci-cd.yml
Aleksandr Meshchriakov d5e09c18bb
All checks were successful
CI/CD Pipeline / Code Quality Checks (pull_request) Successful in 1m57s
CI/CD Pipeline / Run Tests (pull_request) Successful in 2m19s
CI/CD Pipeline / Run API Inventory E2E Tests (pull_request) Successful in 34s
CI/CD Pipeline / Telegram Notify Success (pull_request) Successful in 20s
fix(ci): force uv to use system python
2026-03-24 14:32:41 +01:00

306 lines
9.7 KiB
YAML

name: CI/CD Pipeline
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
env:
PYTHON_VERSION: "3.11"
jobs:
lint:
name: Code Quality Checks
runs-on: ubuntu-latest
timeout-minutes: 15
if: ${{ !contains(github.event.head_commit.message, '#no_lint') }}
env:
TG_BOT_KEY: ${{ secrets.TG_BOT_KEY }}
TG_CHANNEL: ${{ secrets.TG_CHANNEL }}
steps:
- name: Checkout code
run: |
REPO_URL=$(echo "${GITHUB_SERVER_URL}" | sed "s|://|://oauth2:${{ gitea.token }}@|")
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
git clone --depth=1 --branch="${BRANCH}" "${REPO_URL}/${GITHUB_REPOSITORY}.git" .
git checkout "${GITHUB_SHA}"
- name: Install Python and uv
run: |
set -euo pipefail
PROJECT_PYTHON_VERSION="$(cat .python-version 2>/dev/null || printf '%s' "${PYTHON_VERSION}")"
PYTHON_BIN="$(./scripts/ensure-ci-python.sh "${PROJECT_PYTHON_VERSION}")"
printf 'PYTHON_BIN=%s\n' "${PYTHON_BIN}" > .ci-python-env
- name: Create virtual environment and install dependencies
run: |
set -euo pipefail
. ./.ci-python-env
"${PYTHON_BIN}" -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip uv
uv sync \
--dev \
--frozen \
--active \
--python "${PYTHON_BIN}" \
--no-managed-python \
--no-python-downloads
- name: Run Ruff linting
run: |
set -euo pipefail
. .venv/bin/activate
ruff check src
- name: Run Ruff formatting check
run: |
set -euo pipefail
. .venv/bin/activate
ruff format src --check
- name: Telegram notify (lint failed)
if: failure()
continue-on-error: true
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
COMMIT_MESSAGE=$(git log -1 --pretty=%s 2>/dev/null || echo "n/a")
MSG="❌ [mostovik-backend] lint failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}
commit=${COMMIT_MESSAGE}"
curl -fsS \
--connect-timeout 5 \
--max-time 15 \
--retry 2 \
--retry-delay 2 \
--retry-all-errors \
-X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}" \
|| echo "Telegram notification failed; continue pipeline"
test:
name: Run Tests
runs-on: ubuntu-latest
timeout-minutes: 20
if: ${{ !contains(github.event.head_commit.message, '#no_test') }}
env:
TG_BOT_KEY: ${{ secrets.TG_BOT_KEY }}
TG_CHANNEL: ${{ secrets.TG_CHANNEL }}
steps:
- name: Checkout code
run: |
REPO_URL=$(echo "${GITHUB_SERVER_URL}" | sed "s|://|://oauth2:${{ gitea.token }}@|")
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
git clone --depth=1 --branch="${BRANCH}" "${REPO_URL}/${GITHUB_REPOSITORY}.git" .
git checkout "${GITHUB_SHA}"
- name: Install Python and uv
run: |
set -euo pipefail
PROJECT_PYTHON_VERSION="$(cat .python-version 2>/dev/null || printf '%s' "${PYTHON_VERSION}")"
PYTHON_BIN="$(./scripts/ensure-ci-python.sh "${PROJECT_PYTHON_VERSION}")"
printf 'PYTHON_BIN=%s\n' "${PYTHON_BIN}" > .ci-python-env
- name: Create virtual environment and install dependencies
run: |
set -euo pipefail
. ./.ci-python-env
"${PYTHON_BIN}" -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip uv
uv sync \
--dev \
--frozen \
--active \
--python "${PYTHON_BIN}" \
--no-managed-python \
--no-python-downloads
- name: Run regular pytest suite
env:
DJANGO_SETTINGS_MODULE: settings.test
SECRET_KEY: test-secret-key-for-ci
run: |
set -euo pipefail
export PYTHONPATH="${PWD}/src:${PYTHONPATH:-}"
.venv/bin/python -m pytest tests --ignore=tests/test_api_inventory_e2e.py -q
- name: Pack prepared test workspace
if: success()
run: |
set -euo pipefail
WORKSPACE_ARCHIVE="/tmp/ci-test-workspace.tar.gz"
tar \
--exclude='.git' \
--exclude='.pytest_cache' \
--exclude='htmlcov' \
--exclude='__pycache__' \
-czf "${WORKSPACE_ARCHIVE}" \
.
- name: Upload prepared test workspace
if: success()
uses: actions/upload-artifact@v3
with:
name: ci-test-workspace
path: /tmp/ci-test-workspace.tar.gz
if-no-files-found: error
retention-days: 1
- name: Telegram notify (test failed)
if: failure()
continue-on-error: true
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
COMMIT_MESSAGE=$(git log -1 --pretty=%s 2>/dev/null || echo "n/a")
MSG="❌ [mostovik-backend] test failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}
commit=${COMMIT_MESSAGE}"
curl -fsS \
--connect-timeout 5 \
--max-time 15 \
--retry 2 \
--retry-delay 2 \
--retry-all-errors \
-X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}" \
|| echo "Telegram notification failed; continue pipeline"
test_api_inventory_e2e:
name: Run API Inventory E2E Tests
runs-on: ubuntu-latest
timeout-minutes: 10
needs: [test]
if: ${{ needs.test.result == 'success' }}
env:
TG_BOT_KEY: ${{ secrets.TG_BOT_KEY }}
TG_CHANNEL: ${{ secrets.TG_CHANNEL }}
steps:
- name: Download prepared test workspace
uses: actions/download-artifact@v3
with:
name: ci-test-workspace
- name: Extract prepared test workspace
run: |
set -euo pipefail
ARCHIVE_PATH="$(find . -maxdepth 2 -name 'ci-test-workspace.tar.gz' -print -quit)"
if [ -z "${ARCHIVE_PATH}" ]; then
echo "ci-test-workspace.tar.gz not found after artifact download" >&2
exit 1
fi
tar -xzf "${ARCHIVE_PATH}"
- name: Install Python for artifact environment
run: |
set -euo pipefail
PROJECT_PYTHON_VERSION="$(cat .python-version 2>/dev/null || printf '%s' "${PYTHON_VERSION}")"
./scripts/ensure-ci-python.sh "${PROJECT_PYTHON_VERSION}" >/dev/null
- name: Run API inventory pytest suite
env:
DJANGO_SETTINGS_MODULE: settings.test
SECRET_KEY: test-secret-key-for-ci
run: |
set -euo pipefail
export PYTHONPATH="${PWD}/src:${PYTHONPATH:-}"
.venv/bin/python -m pytest tests/test_api_inventory_e2e.py -q
- name: Telegram notify (api inventory e2e failed)
if: failure()
continue-on-error: true
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
MSG="❌ [mostovik-backend] api inventory e2e failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}"
curl -fsS \
--connect-timeout 5 \
--max-time 15 \
--retry 2 \
--retry-delay 2 \
--retry-all-errors \
-X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}" \
|| echo "Telegram notification failed; continue pipeline"
notify_success:
name: Telegram Notify Success
runs-on: ubuntu-latest
timeout-minutes: 1
needs: [lint, test, test_api_inventory_e2e]
if: |
always() &&
needs.lint.result == 'success' &&
needs.test.result == 'success' &&
needs.test_api_inventory_e2e.result == 'success'
env:
TG_BOT_KEY: ${{ secrets.TG_BOT_KEY }}
TG_CHANNEL: ${{ secrets.TG_CHANNEL }}
steps:
- name: Telegram notify (lint+tests+e2e success)
continue-on-error: true
env:
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
MSG="✅ [mostovik-backend] lint + tests + api inventory e2e passed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}
commit=${COMMIT_MESSAGE:-n/a}"
curl -fsS \
--connect-timeout 5 \
--max-time 15 \
--retry 2 \
--retry-delay 2 \
--retry-all-errors \
-X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}" \
|| echo "Telegram notification failed; continue pipeline"