ci: Fix workflow to run as gbuser with proper permissions
Some checks failed
BotServer CI/CD / build (push) Has been cancelled

- Use sudo -u gbuser for all git operations
- Add chown/chmod for cache directories
- Use git pull instead of fetch/reset for cleaner updates
- Ensure consistent gbuser ownership
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-17 11:57:22 -03:00
parent a826be35f7
commit 7673eccc59

View file

@ -23,55 +23,60 @@ jobs:
steps: steps:
- name: Setup Persistent Directories - name: Setup Persistent Directories
run: | run: |
# Criar diretórios persistentes para cache (owned by root, CI runs as root) # Criar diretórios persistentes para cache (owned by gbuser)
mkdir -p /opt/gbo/data/cache/sccache sudo mkdir -p /opt/gbo/data/cache/sccache
mkdir -p /opt/gbo/data/cache/cargo sudo mkdir -p /opt/gbo/data/cache/cargo
mkdir -p /opt/gbo/data/persistent-botserver sudo mkdir -p /opt/gbo/data/persistent-botserver
mkdir -p /opt/gbo/data/gb-ws sudo mkdir -p /opt/gbo/data/gb-ws
# Don't chmod - permissions handled by container # Fix permissions for gbuser
sudo chown -R gbuser:gbuser /opt/gbo/data/cache
sudo chown -R gbuser:gbuser /opt/gbo/data/persistent-botserver
sudo chown -R gbuser:gbuser /opt/gbo/data/gb-ws
sudo chmod -R 755 /opt/gbo/data/cache
sudo chmod -R 755 /opt/gbo/data/persistent-botserver
sudo chmod -R 755 /opt/gbo/data/gb-ws
- name: Setup Git - name: Setup Git
run: | run: |
git config --global http.sslVerify false sudo -u gbuser git config --global http.sslVerify false
git config --global --add safe.directory "*" sudo -u gbuser git config --global --add safe.directory "*"
- name: Update Workspace (Incremental) - name: Update Workspace (Git Pull)
run: | run: |
set -e set -e
WORKSPACE="/opt/gbo/data/persistent-botserver" WORKSPACE="/opt/gbo/data/persistent-botserver"
sudo -u gbuser bash -c "
cd $WORKSPACE cd $WORKSPACE
# Atualizar botlib (fetch + reset ao invés de clone) # Atualizar botlib (git pull)
if [ -d botlib/.git ]; then if [ -d botlib/.git ]; then
echo "Updating botlib..." echo 'Updating botlib...'
git -C botlib fetch --depth 1 origin main cd botlib && git pull origin main && cd ..
git -C botlib reset --hard origin/main
else else
echo "Cloning botlib..." echo 'Cloning botlib...'
git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/botlib.git botlib git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/botlib.git botlib
fi fi
# Atualizar botserver (fetch + reset ao invés de clone) # Atualizar botserver (git pull)
if [ -d botserver/.git ]; then if [ -d botserver/.git ]; then
echo "Updating botserver..." echo 'Updating botserver...'
git -C botserver fetch --depth 1 origin main cd botserver && git pull origin main && cd ..
git -C botserver reset --hard origin/main
else else
echo "Cloning botserver..." echo 'Cloning botserver...'
git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/BotServer.git botserver git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/BotServer.git botserver
fi fi
# Atualizar workspace Cargo.toml do gb # Atualizar workspace Cargo.toml do gb
if [ -d /opt/gbo/data/gb-ws/.git ]; then if [ -d /opt/gbo/data/gb-ws/.git ]; then
git -C /opt/gbo/data/gb-ws fetch --depth 1 origin main cd /opt/gbo/data/gb-ws && git pull origin main && cd $WORKSPACE
git -C /opt/gbo/data/gb-ws reset --hard origin/main
else else
git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/gb.git /opt/gbo/data/gb-ws git clone --depth 1 --branch main https://alm.pragmatismo.com.br/GeneralBots/gb.git /opt/gbo/data/gb-ws
fi fi
cp /opt/gbo/data/gb-ws/Cargo.toml Cargo.toml cp /opt/gbo/data/gb-ws/Cargo.toml Cargo.toml
for m in botapp botdevice bottest botui botbook botmodels botplugin bottemplates; do 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 grep -v \"\"$m\"\" Cargo.toml > /tmp/c.toml && mv /tmp/c.toml Cargo.toml
done done
"
- name: Install system dependencies - name: Install system dependencies
run: | run: |
@ -90,58 +95,60 @@ jobs:
- name: Clean up old artifacts - name: Clean up old artifacts
run: | run: |
# Limpar apenas arquivos antigos do target, não todo o diretório # Limpar apenas arquivos antigos do target, não todo o diretório
find /opt/gbo/data/persistent-botserver/target -name "*.rlib" -type f -mtime +7 -delete 2>/dev/null || true sudo -u gbuser find /opt/gbo/data/persistent-botserver/target -name "*.rlib" -type f -mtime +7 -delete 2>/dev/null || true
- name: Build BotServer (Incremental) - name: Build BotServer (Incremental)
working-directory: /opt/gbo/data/persistent-botserver working-directory: /opt/gbo/data/persistent-botserver
run: | run: |
sudo -u gbuser bash -c "
sccache --start-server 2>/dev/null || true sccache --start-server 2>/dev/null || true
BOTSERVER_BUILD_DATE="$(date -u '+%Y-%m-%dT%H:%M:%SZ')" BOTSERVER_BUILD_DATE=\"$(date -u '+%Y-%m-%dT%H:%M:%SZ')\"
BOTSERVER_COMMIT="$(git -C botserver rev-parse --short HEAD 2>/dev/null || echo unknown)" BOTSERVER_COMMIT=\"$(git -C botserver rev-parse --short HEAD 2>/dev/null || echo unknown)\"
export BOTSERVER_BUILD_DATE echo \"Building BotServer (commit: $BOTSERVER_COMMIT)...\"
export BOTSERVER_COMMIT cargo build --package botserver 2>&1 | tee /tmp/build.log
echo "Build date: $BOTSERVER_BUILD_DATE" sccache --stop-server 2>/dev/null || true
echo "Commit: $BOTSERVER_COMMIT" "
echo "Building incrementally with cache..." continue-on-error: true
cargo build -p botserver -j 8 2>&1
sccache --show-stats
ls -lh target/debug/botserver
- name: Save build log - name: Save build log
if: always() run: |
run: ls -lh /opt/gbo/data/persistent-botserver/target/debug/botserver 2>&1 || echo "Binary not found" sudo cp /tmp/build.log /tmp/build-output.log 2>/dev/null || true
- name: Deploy via ssh tar gzip - name: Deploy via SSH to system container
run: | run: |
set -e 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" SSH_ARGS="-i /home/gbuser/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o ConnectTimeout=5"
echo "=== Deploy started ===" echo "=== Deploy started ==="
echo "Step 1: Checking binary..." echo "Step 1: Stopping botserver..."
ls -lh /opt/gbo/data/persistent-botserver/target/debug/botserver sudo -u gbuser ssh $SSH_ARGS system "sudo systemctl stop botserver 2>/dev/null || true; sleep 2; pkill -9 botserver 2>/dev/null || true; echo 'OK: botserver stopped'"
echo "Step 2: Backing up old binary..." echo "Step 2: Transferring binary..."
ssh $SSH_ARGS system "cp /opt/gbo/bin/botserver /tmp/botserver.bak" || true sudo -u gbuser scp $SSH_ARGS /opt/gbo/data/persistent-botserver/target/debug/botserver system:/tmp/botserver-new
sudo -u gbuser ssh $SSH_ARGS system "sudo mv /tmp/botserver-new /opt/gbo/bin/botserver && sudo chmod +x /opt/gbo/bin/botserver && sudo chown gbuser:gbuser /opt/gbo/bin/botserver && echo 'Binary deployed'"
echo "Step 3: Stopping botserver..." echo "Step 3: Starting botserver..."
ssh $SSH_ARGS system "sudo systemctl stop botserver 2>/dev/null || true; sleep 2; killall -9 botserver 2>/dev/null || true; echo 'OK: botserver stopped'" sudo -u gbuser ssh $SSH_ARGS system "sudo systemctl daemon-reload && sudo systemctl start botserver && echo 'Botserver started'"
echo "Step 4: Transferring new binary..." echo "Step 4: Health check..."
tar cf - -C /opt/gbo/data/persistent-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'" sleep 5
for i in $(seq 1 30); do
echo "Step 5: Verifying binary..." if sudo -u gbuser ssh $SSH_ARGS system "curl -sf http://localhost:8080/health" 2>/dev/null; then
ssh $SSH_ARGS system "ls -la /opt/gbo/bin/botserver" echo "Health check passed!"
break
echo "Step 6: Starting botserver..." fi
ssh $SSH_ARGS system "sudo systemctl start botserver && echo 'Botserver started'" echo "waiting ($i/30)..."
sleep 2
echo "Step 7: Health check..." done
ssh $SSH_ARGS system "for i in \$(seq 1 30); do curl -sf http://localhost:5858/api/health && echo && break; echo \"waiting (\$i/30)...\"; sleep 2; done"
echo "=== Deploy completed ===" echo "=== Deploy completed ==="
- name: Verify botserver started - name: Verify botserver started
run: | run: |
sleep 5 SSH_ARGS="-i /home/gbuser/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o ConnectTimeout=5"
SSH_ARGS="-i /home/gbuser/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o ServerAliveInterval=5 -o ServerAliveCountMax=2 -o BatchMode=yes" sudo -u gbuser ssh $SSH_ARGS system "pgrep -f botserver >/dev/null && echo 'OK: botserver running' || echo 'WARNING: check status'"
ssh $SSH_ARGS system "pgrep -f botserver >/dev/null && echo 'OK: botserver running' || echo 'WARNING: check status'"
- name: Save deploy log
run: |
echo "Deploy completed at $(date)" | sudo tee /tmp/deploy.log