From 566db308b3e598574110a7d40147f8041d9abe78 Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Fri, 17 Oct 2025 05:54:32 +0300 Subject: [PATCH] add cicd --- .gitea/workflows/auto-deploy.yml | 32 ++++ .gitea/workflows/deploy.yml | 312 +++++++++++++++++++++++++++++++ .gitea/workflows/test.yml | 2 +- 3 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 .gitea/workflows/auto-deploy.yml create mode 100644 .gitea/workflows/deploy.yml diff --git a/.gitea/workflows/auto-deploy.yml b/.gitea/workflows/auto-deploy.yml new file mode 100644 index 0000000..264b55f --- /dev/null +++ b/.gitea/workflows/auto-deploy.yml @@ -0,0 +1,32 @@ +name: Auto Deploy + +on: + workflow_run: + workflows: ["Deploy"] + types: [completed] + branches: [master] + +jobs: + auto-deploy: + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + + steps: + - name: Download deployment package + uses: actions/download-artifact@v3 + with: + name: chatbot-deployment + path: ./deployment + + - name: Deploy to server + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SERVER_USER }} + key: ${{ secrets.SERVER_SSH_KEY }} + script: | + cd /tmp + wget ${{ github.event.workflow_run.artifacts_url }} + tar -xzf chatbot-deployment-*.tar.gz + sudo ./deploy.sh + echo "Deployment completed successfully!" diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..cc1e04e --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,312 @@ +name: Deploy + +on: + push: + branches: + - master + workflow_dispatch: + +jobs: + deploy: + name: Deploy to Production + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '9.0.x' + + - name: Restore dependencies + run: dotnet restore --verbosity normal + + - name: Build application + run: dotnet build --no-restore --configuration Release --verbosity normal + + - name: Publish application + run: dotnet publish ChatBot/ChatBot.csproj --configuration Release --output ./publish --verbosity normal + + - name: Create deployment package + run: | + mkdir -p ./deployment + cp -r ./publish/* ./deployment/ + cp -r ./ChatBot/Prompts ./deployment/ + cp ./ChatBot/appsettings.json ./deployment/ + cp ./ChatBot/appsettings.Production.json ./deployment/ 2>/dev/null || echo "Production config not found, using default" + + # Create deployment script + cat > ./deployment/deploy.sh << 'EOF' + #!/bin/bash + set -e + + echo "Starting ChatBot deployment..." + + # Stop existing service if running + sudo systemctl stop chatbot || echo "Service not running" + + # Backup current version + if [ -d "/opt/chatbot" ]; then + sudo mv /opt/chatbot /opt/chatbot.backup.$(date +%Y%m%d_%H%M%S) + fi + + # Create application directory + sudo mkdir -p /opt/chatbot + + # Copy new files + sudo cp -r ./* /opt/chatbot/ + + # Set permissions + sudo chown -R chatbot:chatbot /opt/chatbot + sudo chmod +x /opt/chatbot/ChatBot + + # Start service + sudo systemctl start chatbot + sudo systemctl enable chatbot + + echo "Deployment completed successfully!" + EOF + + chmod +x ./deployment/deploy.sh + + # Create systemd service file + cat > ./deployment/chatbot.service << 'EOF' + [Unit] + Description=ChatBot Telegram Service + After=network.target postgresql.service + Wants=postgresql.service + + [Service] + Type=notify + User=chatbot + Group=chatbot + WorkingDirectory=/opt/chatbot + ExecStart=/opt/chatbot/ChatBot + Restart=always + RestartSec=10 + Environment=ASPNETCORE_ENVIRONMENT=Production + Environment=ASPNETCORE_URLS=http://localhost:5000 + + # Environment variables (set these in /etc/systemd/system/chatbot.service.d/override.conf) + # Environment=TELEGRAM_BOT_TOKEN=your_bot_token + # Environment=OLLAMA_URL=http://localhost:11434 + # Environment=OLLAMA_DEFAULT_MODEL=llama3.2 + # Environment=DB_HOST=localhost + # Environment=DB_PORT=5432 + # Environment=DB_NAME=chatbot + # Environment=DB_USER=postgres + # Environment=DB_PASSWORD=your_password + + [Install] + WantedBy=multi-user.target + EOF + + # Create Docker Compose file for easy deployment + cat > ./deployment/docker-compose.yml << 'EOF' + version: '3.8' + + services: + chatbot: + build: . + container_name: chatbot + restart: unless-stopped + environment: + - ASPNETCORE_ENVIRONMENT=Production + - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN} + - OLLAMA_URL=${OLLAMA_URL:-http://ollama:11434} + - OLLAMA_DEFAULT_MODEL=${OLLAMA_DEFAULT_MODEL:-llama3.2} + - DB_HOST=postgres + - DB_PORT=5432 + - DB_NAME=${DB_NAME:-chatbot} + - DB_USER=${DB_USER:-postgres} + - DB_PASSWORD=${DB_PASSWORD} + depends_on: + - postgres + - ollama + networks: + - chatbot-network + + postgres: + image: postgres:15-alpine + container_name: chatbot-postgres + restart: unless-stopped + environment: + - POSTGRES_DB=${DB_NAME:-chatbot} + - POSTGRES_USER=${DB_USER:-postgres} + - POSTGRES_PASSWORD=${DB_PASSWORD} + volumes: + - postgres_data:/var/lib/postgresql/data + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + networks: + - chatbot-network + + ollama: + image: ollama/ollama:latest + container_name: chatbot-ollama + restart: unless-stopped + volumes: + - ollama_data:/root/.ollama + networks: + - chatbot-network + ports: + - "11434:11434" + + volumes: + postgres_data: + ollama_data: + + networks: + chatbot-network: + driver: bridge + EOF + + # Create Dockerfile + cat > ./deployment/Dockerfile << 'EOF' + FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base + WORKDIR /app + EXPOSE 8080 + EXPOSE 8081 + + FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build + WORKDIR /src + COPY ["ChatBot/ChatBot.csproj", "ChatBot/"] + RUN dotnet restore "ChatBot/ChatBot.csproj" + COPY . . + WORKDIR "/src/ChatBot" + RUN dotnet build "ChatBot.csproj" -c Release -o /app/build + + FROM build AS publish + RUN dotnet publish "ChatBot.csproj" -c Release -o /app/publish /p:UseAppHost=false + + FROM base AS final + WORKDIR /app + COPY --from=publish /app/publish . + COPY --from=publish /src/ChatBot/Prompts ./Prompts + ENTRYPOINT ["dotnet", "ChatBot.dll"] + EOF + + # Create .env.example file + cat > ./deployment/.env.example << 'EOF' + # Telegram Bot Configuration + TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here + + # Ollama Configuration + OLLAMA_URL=http://localhost:11434 + OLLAMA_DEFAULT_MODEL=llama3.2 + + # Database Configuration + DB_HOST=localhost + DB_PORT=5432 + DB_NAME=chatbot + DB_USER=postgres + DB_PASSWORD=your_secure_password_here + EOF + + # Create README for deployment + cat > ./deployment/README.md << 'EOF' + # ChatBot Deployment + + ## System Requirements + - Ubuntu 20.04+ or similar Linux distribution + - .NET 9.0 Runtime + - PostgreSQL 12+ + - Ollama (for AI functionality) + + ## Deployment Options + + ### Option 1: Direct Deployment (Recommended for VPS) + 1. Copy all files to your server + 2. Install dependencies: + ```bash + sudo apt update + sudo apt install -y postgresql postgresql-contrib + sudo apt install -y dotnet-runtime-9.0 + curl -fsSL https://ollama.ai/install.sh | sh + ``` + 3. Create chatbot user: + ```bash + sudo useradd -r -s /bin/false chatbot + ``` + 4. Set up environment variables in systemd override: + ```bash + sudo mkdir -p /etc/systemd/system/chatbot.service.d + sudo nano /etc/systemd/system/chatbot.service.d/override.conf + ``` + 5. Run deployment script: + ```bash + sudo ./deploy.sh + ``` + + ### Option 2: Docker Deployment + 1. Install Docker and Docker Compose + 2. Copy .env.example to .env and configure + 3. Run: + ```bash + docker-compose up -d + ``` + + ## Configuration + Set the following environment variables: + - TELEGRAM_BOT_TOKEN: Your Telegram bot token + - OLLAMA_URL: Ollama server URL (default: http://localhost:11434) + - OLLAMA_DEFAULT_MODEL: Model to use (default: llama3.2) + - DB_*: Database connection settings + + ## Health Checks + The application includes health checks at: + - /health/ollama + - /health/telegram + - /health (overall) + EOF + + - name: Create deployment archive + run: | + cd deployment + tar -czf ../chatbot-deployment-$(date +%Y%m%d_%H%M%S).tar.gz . + cd .. + ls -la *.tar.gz + + - name: Upload deployment artifacts + uses: actions/upload-artifact@v3 + with: + name: chatbot-deployment + path: | + ./deployment/ + ./chatbot-deployment-*.tar.gz + retention-days: 30 + + - name: Create release + if: github.ref == 'refs/heads/master' + uses: softprops/action-gh-release@v1 + with: + tag_name: v${{ github.run_number }} + name: ChatBot Release v${{ github.run_number }} + body: | + ## ChatBot Release v${{ github.run_number }} + + ### Changes + - Automated deployment package + - Includes Docker and systemd deployment options + - Ready for production deployment + + ### Deployment + 1. Download the deployment package + 2. Follow instructions in README.md + 3. Configure environment variables + 4. Deploy using your preferred method + + ### Files Included + - Application binaries + - Docker configuration + - Systemd service file + - Deployment scripts + - Documentation + files: | + ./chatbot-deployment-*.tar.gz + draft: false + prerelease: false diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 3c5e927..c425fc3 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -41,7 +41,7 @@ jobs: - name: Upload test results if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v3 with: name: test-results path: ./TestResults/