name: deploy on: push: branches: - main workflow_dispatch: permissions: contents: read jobs: deploy: if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main') runs-on: ubuntu-latest timeout-minutes: 30 steps: - uses: actions/checkout@v4 - uses: actions/setup-dotnet@v4 with: dotnet-version: '10.0.x' - run: dotnet restore KArtSell.sln - run: dotnet build KArtSell.sln --no-restore -c Release - name: Publish Release Build run: dotnet publish -c Release -o ./publish src/KArtSell.Host - name: Deploy to Gitea server (local filesystem) env: KARTSELL_POSTGRES: ${{ secrets.KARTSELL_POSTGRES }} KRX_OPENAPI: ${{ secrets.KRX_OPENAPI }} OPENDART_API: ${{ secrets.OPENDART_API }} KIS_APP_KEY: ${{ secrets.KIS_APP_KEY }} KIS_APP_SECRET: ${{ secrets.KIS_APP_SECRET }} run: | # Gitea 서버 동일 파일시스템으로 배포 DEPLOY_PATH="/app/kartsell" echo "📦 Deploying to $DEPLOY_PATH..." # 배포 디렉토리 생성 (sudo 사용) if [ ! -d "$DEPLOY_PATH" ]; then sudo mkdir -p "$DEPLOY_PATH" sudo chown -R $USER:$USER "$DEPLOY_PATH" echo "✅ Created $DEPLOY_PATH" fi # 기존 백업 if [ -d "$DEPLOY_PATH/current" ]; then BACKUP_DIR="$DEPLOY_PATH/backup-$(date +%s)" sudo mv "$DEPLOY_PATH/current" "$BACKUP_DIR" echo "✅ Backed up previous version to $BACKUP_DIR" fi # 새 버전 배포 sudo cp -r ./publish "$DEPLOY_PATH/current" sudo chown -R $USER:$USER "$DEPLOY_PATH/current" sudo chmod +x "$DEPLOY_PATH/current/KArtSell.Host" echo "✅ Files deployed successfully" # 마이그레이션 실행 echo "🗄️ Running database migrations..." cd "$DEPLOY_PATH/current" export ASPNETCORE_ENVIRONMENT=Production export KARTSELL_POSTGRES="${KARTSELL_POSTGRES}" dotnet KArtSell.DbMigrator.dll || { echo "⚠️ Migration completed with warnings or errors" } echo "✅ Migrations completed" # systemd 서비스 생성/활성화 (첫 배포 시) echo "🔧 Configuring systemd service..." sudo tee /etc/systemd/system/kartsell.service > /dev/null << 'SYSTEMD_EOF' [Unit] Description=K-ArtSell Aegis - Financial Advisory System After=network-online.target Wants=network-online.target [Service] Type=notify User=$USER WorkingDirectory=$DEPLOY_PATH/current ExecStart=/usr/bin/dotnet KArtSell.Host.dll Restart=always RestartSec=10 StandardOutput=journal StandardError=journal Environment="ASPNETCORE_ENVIRONMENT=Production" Environment="ASPNETCORE_URLS=http://127.0.0.1:5002" Environment="KARTSELL_POSTGRES=$KARTSELL_POSTGRES" Environment="KRX_OPENAPI=$KRX_OPENAPI" Environment="OPENDART_API=$OPENDART_API" Environment="KIS_APP_KEY=$KIS_APP_KEY" Environment="KIS_APP_SECRET=$KIS_APP_SECRET" NoNewPrivileges=true PrivateTmp=true [Install] WantedBy=multi-user.target SYSTEMD_EOF sudo systemctl daemon-reload sudo systemctl enable kartsell # 서비스 시작/재시작 echo "🔄 Starting service..." sudo systemctl restart kartsell # 헬스체크 echo "🏥 Health check..." sleep 5 curl -f http://127.0.0.1:5002/health || { echo "⚠️ Health check inconclusive (service may still be starting)" } echo "✅ Deployment completed" notify: if: always() needs: deploy runs-on: ubuntu-latest steps: - name: Notify deployment status env: TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }} TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} run: | STATUS="${{ needs.deploy.result }}" if [ "$STATUS" = "success" ]; then MESSAGE="✅ K-ArtSell Aegis deployed successfully to production" else MESSAGE="❌ K-ArtSell Aegis deployment failed" fi curl -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \ -d "chat_id=$TELEGRAM_CHAT_ID" \ -d "text=$MESSAGE" \ -d "parse_mode=HTML" || echo "Telegram notification failed"