name: Deploy Telegram Bot to VPS
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy:
name: Deploy to VPS
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Download dependencies
run: |
echo "Downloading Go modules..."
go mod tidy
go mod download
echo "Dependencies downloaded successfully"
- name: Build and test locally
run: |
echo "Building application..."
go build -o bot
echo "Build completed successfully"
ls -lh bot
- name: Configure SSH key
env:
VPS_HOST: ${{ secrets.VPS_HOST }}
VPS_PORT: ${{ secrets.VPS_PORT }}
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -p ${VPS_PORT} -H ${VPS_HOST} >> ~/.ssh/known_hosts 2>/dev/null || true
# Configure SSH to skip host key verification on first run
echo "Host ${VPS_HOST}" >> ~/.ssh/config
echo " StrictHostKeyChecking accept-new" >> ~/.ssh/config
echo " Port ${VPS_PORT}" >> ~/.ssh/config
chmod 600 ~/.ssh/config
echo "SSH key configured"
- name: Deploy to VPS
env:
VPS_HOST: ${{ secrets.VPS_HOST }}
VPS_USER: ${{ secrets.VPS_USER }}
VPS_PORT: ${{ secrets.VPS_PORT }}
VPS_APP_PATH: ${{ secrets.VPS_APP_PATH }}
run: |
# Create deploy script
cat > /tmp/deploy.sh << 'DEPLOY_EOF'
#!/bin/bash
set -e
echo "=========================================="
echo "Starting GitHub Actions"
echo "=========================================="
# Use environment variables directly instead of positional parameters
VPS_USER_NAME="${VPS_USER}"
VPS_APP_PATH_VAR="${VPS_APP_PATH}"
if [ -z "$VPS_USER_NAME" ] || [ -z "$VPS_APP_PATH_VAR" ]; then
echo "Error: VPS_USER or VPS_APP_PATH environment variable not set"
exit 1
fi
echo "Deploying as user: $VPS_USER_NAME"
echo "Deploy path: $VPS_APP_PATH_VAR"
# Navigate to parent directory and ensure repo exists
PARENT_DIR=$(dirname "${VPS_APP_PATH_VAR}")
REPO_NAME=$(basename "${VPS_APP_PATH_VAR}")
mkdir -p "${PARENT_DIR}"
cd "${PARENT_DIR}"
# If directory doesn't exist, clone it with the correct user
if [ ! -d "${VPS_APP_PATH_VAR}" ]; then
echo "Directory does not exist, cloning repository..."
# Use sudo with password from stdin (user has password set in VPS init script)
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git clone https://github.com/reikwei/telegram-bot-vps-install.git "${REPO_NAME}"
fi
cd "${VPS_APP_PATH_VAR}"
echo "Current directory: $(pwd)"
# Configure git safe directory for the user
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git config --global --add safe.directory "${VPS_APP_PATH_VAR}"
# Ensure we have the remote configured
if ! echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git remote | grep -q origin; then
echo "Adding git remote..."
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git remote add origin https://github.com/reikwei/telegram-bot-vps-install.git
fi
# Fetch and pull latest code with correct user
echo "Fetching latest code from main branch..."
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git fetch origin main
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" git reset --hard origin/main
echo "Code updated successfully"
echo "Building bot application..."
# Create bin directory
mkdir -p "${VPS_APP_PATH_VAR}/bin"
# Change ownership - need to use sudo with password
echo "xiewei123" | sudo -S chown -R "${VPS_USER_NAME}:${VPS_USER_NAME}" "${VPS_APP_PATH_VAR}"
# Build the application with proper environment
# Source the user's profile to ensure Go environment variables are set
echo "xiewei123" | sudo -S -u "${VPS_USER_NAME}" bash -c "
source ~/.bashrc 2>/dev/null || true
cd '${VPS_APP_PATH_VAR}'
echo 'Go path check:'
which go || echo 'Go not in PATH, trying /usr/local/go/bin/go'
/usr/local/go/bin/go version
export PATH=/usr/local/go/bin:\$PATH
export GOPATH=\$HOME/go
export PATH=\$PATH:\$GOPATH/bin
go mod download && go build -o bin/tg-bot
"
if [ $? -ne 0 ]; then
echo "Build failed!"
exit 1
fi
# Verify the binary was created
if [ ! -f "${VPS_APP_PATH_VAR}/bin/tg-bot" ]; then
echo "Binary file not found after build!"
exit 1
fi
echo "Build completed successfully"
ls -lh "${VPS_APP_PATH_VAR}/bin/tg-bot"
echo "Stopping bot service..."
sudo -n systemctl stop tg-bot.service 2>/dev/null || echo "Service was not running"
sleep 2
echo "Starting bot service..."
sudo -n systemctl start tg-bot.service
sleep 3
if sudo -n systemctl is-active --quiet tg-bot.service; then
echo "Bot service started successfully"
sudo -n systemctl status tg-bot.service --no-pager
else
echo "Bot service failed to start!"
echo ""
echo "Service status:"
sudo -n systemctl status tg-bot.service --no-pager || true
echo ""
echo "Recent logs:"
sudo -n journalctl -u tg-bot.service -n 30 --no-pager
exit 1
fi
echo "=========================================="
echo "Deployment completed successfully"
echo "=========================================="
DEPLOY_EOF
# Ensure the script was created
if [ ! -f /tmp/deploy.sh ]; then
echo "ERROR: Failed to create deploy script"
exit 1
fi
chmod +x /tmp/deploy.sh
# Verify the deploy script exists and is readable
echo "Deploy script created successfully:"
ls -lh /tmp/deploy.sh
file /tmp/deploy.sh
wc -l /tmp/deploy.sh
# Copy deploy script to VPS
echo ""
echo "Copying deploy script to VPS..."
echo "VPS details - Host: ${VPS_HOST}, User: ${VPS_USER}, Port: ${VPS_PORT}"
scp -i ~/.ssh/deploy_key -P ${VPS_PORT} /tmp/deploy.sh "${VPS_USER}@${VPS_HOST}:/tmp/deploy.sh" || {
echo "ERROR: Failed to copy deploy script to VPS"
echo "Trying alternative method..."
ssh -i ~/.ssh/deploy_key -p ${VPS_PORT} "${VPS_USER}@${VPS_HOST}" "cat > /tmp/deploy.sh" < /tmp/deploy.sh
if [ $? -ne 0 ]; then
exit 1
fi
}
echo "Deploy script copied successfully"
# Execute deploy script on VPS with environment variables
echo ""
echo "Executing deploy script on VPS..."
ssh -i ~/.ssh/deploy_key -p ${VPS_PORT} "${VPS_USER}@${VPS_HOST}" "export VPS_USER=${VPS_USER}; export VPS_APP_PATH=${VPS_APP_PATH}; bash /tmp/deploy.sh"
- name: Check service status
if: success()
env:
VPS_HOST: ${{ secrets.VPS_HOST }}
VPS_USER: ${{ secrets.VPS_USER }}
VPS_PORT: ${{ secrets.VPS_PORT }}
run: |
ssh -i ~/.ssh/deploy_key -p ${VPS_PORT} "${VPS_USER}@${VPS_HOST}" << 'STATUS_EOF'
echo "=========================================="
echo "Checking bot service status"
echo "=========================================="
sudo systemctl status tg-bot.service --no-pager
echo ""
echo "Recent service logs:"
sudo journalctl -u tg-bot.service -n 10 --no-pager
STATUS_EOF
- name: Deployment success notification
if: success()
run: |
echo "=========================================="
echo "SUCCESS: Bot deployed to VPS"
echo "=========================================="
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Commit: ${{ github.sha }}"
echo "Author: ${{ github.actor }}"
echo "Branch: ${{ github.ref_name }}"
- name: Deployment failure notification
if: failure()
run: |
echo "=========================================="
echo "FAILED: Bot deployment failed"
echo "=========================================="
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Commit: ${{ github.sha }}"
echo "Author: ${{ github.actor }}"
echo "Branch: ${{ github.ref_name }}"
echo "Please check the logs above for details"