72 lines
2.2 KiB
YAML
72 lines
2.2 KiB
YAML
name: Deploy to VPS
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout Repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
submodules: recursive
|
|
|
|
- name: Deploy to VPS
|
|
env:
|
|
VPS_HOST: ${{ secrets.VPS_HOST }}
|
|
VPS_USERNAME: ${{ secrets.VPS_USERNAME }}
|
|
VPS_SSH_KEY: ${{ secrets.VPS_SSH_KEY }}
|
|
ENV_FILE: ${{ secrets.ENV_FILE }}
|
|
REPO_URL: ${{ github.server_url }}/${{ github.repository }}.git
|
|
GITHUB_TOKEN: ${{ github.token }}
|
|
run: |
|
|
# 1. Setup SSH Key
|
|
mkdir -p ~/.ssh
|
|
echo "$VPS_SSH_KEY" > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts
|
|
|
|
# 2. Deploy using git clone and docker
|
|
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no "$VPS_USERNAME@$VPS_HOST" "
|
|
set -e
|
|
APP_DIR=/opt/imphenbot
|
|
|
|
# Configure git safe directory
|
|
git config --global --add safe.directory \$APP_DIR
|
|
|
|
# Configure git to use token for authentication
|
|
git config --global url.\"https://x-access-token:$GITHUB_TOKEN@github.com/\".insteadOf \"https://github.com/\"
|
|
|
|
# Clone or update repository
|
|
if [ -d \"\$APP_DIR/.git\" ]; then
|
|
echo 'Repository exists, pulling latest changes...'
|
|
cd \$APP_DIR
|
|
git fetch origin master
|
|
git reset --hard origin/master
|
|
else
|
|
echo 'Cloning repository...'
|
|
mkdir -p \$APP_DIR
|
|
git clone -b master --depth 1 $REPO_URL \$APP_DIR
|
|
cd \$APP_DIR
|
|
fi
|
|
|
|
# Inject environment variables
|
|
echo \"$ENV_FILE\" > .env
|
|
|
|
# Build and start containers with docker compose
|
|
if command -v docker-compose &> /dev/null; then
|
|
docker-compose down || true
|
|
docker-compose up -d --build
|
|
else
|
|
docker compose down || true
|
|
docker compose up -d --build
|
|
fi
|
|
"
|