name: BotServer CI/CD on: push: branches: ["main"] pull_request: branches: ["main"] env: CARGO_BUILD_JOBS: 8 CARGO_NET_RETRY: 10 RUSTC_WRAPPER: sccache WORKSPACE: /opt/gbo/data/botserver CARGO_TARGET_DIR: /opt/gbo/data/botserver/target PATH: /home/gbuser/.cargo/bin:/home/gbuser/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin jobs: build: runs-on: gbo steps: - name: Setup Git run: | git config --global http.sslVerify false git config --global --add safe.directory "*" - name: Setup Workspace run: | mkdir -p $WORKSPACE cd $WORKSPACE # Fetch latest changes first echo "=== Checking for updates ===" # Update or clone botlib if [ -d botlib/.git ]; then echo "Updating botlib..." git -C botlib fetch origin main if ! git -C botlib diff origin/main --quiet 2>/dev/null; then echo "botlib has changes, pulling..." git -C botlib pull --ff-only origin main else echo "botlib up to date" fi else git clone --branch main https://alm.pragmatismo.com.br/GeneralBots/botlib.git botlib fi # Update or clone botserver if [ -d botserver/.git ]; then echo "Updating botserver..." git -C botserver fetch origin main if ! git -C botserver diff origin/main --quiet 2>/dev/null; then echo "botserver has changes, pulling..." git -C botserver pull --ff-only origin main else echo "botserver up to date" fi else git clone --branch main https://alm.pragmatismo.com.br/GeneralBots/BotServer.git botserver fi # Get workspace Cargo.toml from gb if [ -d /opt/gbo/data/gb-ws/.git ]; then git -C /opt/gbo/data/gb-ws fetch origin main if ! git -C /opt/gbo/data/gb-ws diff origin/main --quiet 2>/dev/null; then echo "gb-ws has changes, pulling..." git -C /opt/gbo/data/gb-ws pull --ff-only origin main else echo "gb-ws up to date" fi else git clone --branch main https://alm.pragmatismo.com.br/GeneralBots/gb.git /opt/gbo/data/gb-ws fi # Only rebuild if there were actual changes echo "$WORKSPACE" > /tmp/workspace_path cp /opt/gbo/data/gb-ws/Cargo.toml Cargo.toml for m in botapp botdevice bottest botui botbook botmodels botplugin bottemplates; do grep -v "\"$m\"" Cargo.toml > /tmp/c.toml && mv /tmp/c.toml Cargo.toml done # Ensure target dir exists (use persistent location for sccache) mkdir -p /opt/gbo/data/botserver/target # Pre-fetch dependencies cargo fetch 2>/dev/null || true - name: Cache sccache uses: actions/cache@v4 with: path: ~/.cache/sccache key: sccache-botserver-${{ github.sha }} restore-keys: sccache-botserver- - name: Install system dependencies run: | PKGS="libpq-dev libssl-dev liblzma-dev pkg-config" MISSING="" for pkg in $PKGS; do dpkg -s "$pkg" >/dev/null 2>&1 || MISSING="$MISSING $pkg" done if [ -n "$MISSING" ]; then sudo apt-get update -qq -o Acquire::Retries=3 -o Acquire::http::Timeout=30 sudo apt-get install -y --no-install-recommends $MISSING else echo "All system dependencies already installed" fi - name: Build BotServer working-directory: /opt/gbo/data/botserver run: | set -e export SCCACHE_IDLE_TIMEOUT=300 export SCCACHE_CACHE_SIZE=10G sccache --stop-server 2>/dev/null || true sleep 1 sccache --start-server # Check if there are changes to rebuild CHANGES=$(git -C /opt/gbo/data/botserver/botserver diff origin/main --name-only 2>/dev/null || echo "changed") if [ -z "$CHANGES" ]; then echo "No changes detected in botserver, checking if binary exists..." if [ -f /opt/gbo/data/botserver/target/debug/botserver ]; then echo "Binary already exists, skipping build" echo "=== sccache stats (before) ===" sccache --show-stats || true exit 0 fi fi echo "=== Starting build in background ===" cargo build -p botserver -j 8 > /tmp/build.log 2>&1 & BUILD_PID=$! echo "Build PID: $BUILD_PID" echo "=== Waiting for build to complete (PID: $BUILD_PID) ===" while kill -0 $BUILD_PID 2>/dev/null; do echo "Build in progress... (sleeping 30s)" sleep 30 done wait $BUILD_PID EXIT_CODE=$? echo "=== Build finished with exit code: $EXIT_CODE ===" if [ $EXIT_CODE -ne 0 ]; then echo "=== Build FAILED ===" tail -100 /tmp/build.log exit $EXIT_CODE fi echo "=== sccache stats ===" sccache --show-stats echo "=== Binary info ===" ls -lh target/debug/botserver stat -c '%y' target/debug/botserver - name: Save build log if: always() run: cp /tmp/build.log /tmp/botserver-$(date +%Y%m%d-%H%M%S).log || true - name: Deploy via ssh tar gzip run: | set -e SSH_ARGS="-i /home/gbuser/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o ServerAliveInterval=5 -o ServerAliveCountMax=2 -o BatchMode=yes" echo "=== Deploy started ===" echo "Step 1: Checking binary..." ls -lh /opt/gbo/data/botserver/target/debug/botserver echo "Step 2: Backing up old binary (ignore if not exists)..." ssh $SSH_ARGS system "cp /opt/gbo/bin/botserver /tmp/botserver.bak 2>/dev/null || true" echo "Step 3: Stopping botserver service..." ssh $SSH_ARGS system "sudo systemctl stop botserver || true" echo "Step 4: Transferring new binary..." tar cf - -C /opt/gbo/data/botserver/target/debug botserver | gzip -1 | ssh $SSH_ARGS system "gzip -d | tar xf - -C /opt/gbo/bin && chmod +x /opt/gbo/bin/botserver && chown gbuser:gbuser /opt/gbo/bin/botserver && echo 'Transfer complete'" echo "Step 5: Starting botserver service..." ssh $SSH_ARGS system "sudo systemctl start botserver && echo 'Botserver started'" echo "=== Deploy completed ===" - name: Verify botserver started run: | sleep 30 SSH_ARGS="-i /home/gbuser/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o ServerAliveInterval=5 -o ServerAliveCountMax=2 -o BatchMode=yes" ssh $SSH_ARGS system "pgrep -f botserver >/dev/null && echo 'OK: botserver is running' || echo 'WARNING: botserver may still be starting'" - name: Save deploy log if: always() run: cp /tmp/deploy.log /tmp/deploy-$(date +%Y%m%d-%H%M%S).log || true