Files
compress/gitea-hooks/post-receive

86 lines
3.9 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
# ==============================================================================
# Post-receive hook для Gitea
# Автоматически синхронизирует изменения с GitHub после push
#
# Установка:
# 1. Разместить в: /path/to/gitea/data/gitea-repositories/username/repo.git/hooks/
# 2. Переименовать в: post-receive
# 3. chmod +x post-receive
# 4. Настроить переменные ниже
# ==============================================================================
# Конфигурация
GITHUB_REPO="git@github.com:username/compress.git"
# Или с HTTPS и токеном:
# GITHUB_REPO="https://YOUR_GITHUB_TOKEN@github.com/fofanov.dmitry/compress.git"
LOG_FILE="/var/log/gitea/github-sync.log"
# Цвета для логов
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# ==============================================================================
# Функция логирования
# ==============================================================================
log() {
echo -e "${2:-$NC}[$(date +'%d.%m.%Y %H:%M:%S')] $1${NC}" | tee -a "$LOG_FILE"
}
# ==============================================================================
# Основная логика
# ==============================================================================
log "═══════════════════════════════════════════════════════════════" "$GREEN"
log "🔄 Начало синхронизации с GitHub" "$GREEN"
log "═══════════════════════════════════════════════════════════════" "$GREEN"
# Читаем информацию о push
while read oldrev newrev refname; do
log "📝 Изменения обнаружены:" "$YELLOW"
log " Branch: ${refname#refs/heads/}"
log " Old commit: ${oldrev:0:8}"
log " New commit: ${newrev:0:8}"
# Проверяем наличие GitHub remote
if ! git remote | grep -q github; then
log " Добавление GitHub remote..." "$YELLOW"
git remote add github "$GITHUB_REPO" 2>&1 | tee -a "$LOG_FILE"
fi
# Пушим в GitHub
log "⬆️ Отправка изменений в GitHub..." "$YELLOW"
# Только для main/master веток
if [[ "$refname" == "refs/heads/main" ]] || [[ "$refname" == "refs/heads/master" ]]; then
if git push github "$refname" --force 2>&1 | tee -a "$LOG_FILE"; then
log "✅ Успешно синхронизировано с GitHub" "$GREEN"
else
log "❌ Ошибка при синхронизации с GitHub" "$RED"
exit 1
fi
# Пушим теги
log "🏷️ Отправка тегов..." "$YELLOW"
if git push github --tags 2>&1 | tee -a "$LOG_FILE"; then
log "✅ Теги синхронизированы" "$GREEN"
else
log "⚠️ Не удалось синхронизировать теги" "$YELLOW"
fi
else
log " Ветка ${refname#refs/heads/} игнорируется (не main/master)" "$YELLOW"
fi
done
log "═══════════════════════════════════════════════════════════════" "$GREEN"
log "✅ Синхронизация завершена" "$GREEN"
log "═══════════════════════════════════════════════════════════════" "$GREEN"
exit 0