This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Production Server | |
env: | |
SERVER_USER: 'swissh' | |
SERVER_IP: 'swis.sh' | |
DEPLOY_PATH: '/home/swissh/swis.sh/' | |
BACKUP_PATH: '/home/swissh/backup.swis.sh/' | |
on: | |
push: | |
branches: | |
- master # Adjust if your main branch is named differently | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Setup Bun | |
uses: oven-sh/setup-bun@v1 | |
with: | |
bun-version: latest | |
- name: Install dependencies | |
run: bun install | |
- name: Build project | |
run: bun run build | |
- name: Set up SSH agent | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Add SSH Key to Known Hosts | |
run: | | |
echo "$(ssh-keyscan -t rsa ${{ env.SERVER_IP }} 2>/dev/null)" >> ~/.ssh/known_hosts | |
- name: Deploy using rsync | |
run: | | |
ssh ${{ env.SERVER_USER }}@${{ env.SERVER_IP }} << EOF | |
set -euo pipefail | |
TIMESTAMP=\$(date +%Y%m%d%H%M%S) | |
# Create backup directory if it doesn't exist | |
mkdir -p ${{ env.BACKUP_PATH }} | |
# Backup existing deployment if it exists | |
if [ -d "${{ env.DEPLOY_PATH }}" ]; then | |
mv ${{ env.DEPLOY_PATH }} ${{ env.BACKUP_PATH }}/backup-\$TIMESTAMP | |
fi | |
# Create new deployment directory | |
mkdir -p ${{ env.DEPLOY_PATH }} | |
EOF | |
rsync -avz --delete .output/public/ ${{ env.SERVER_USER }}@${{ env.SERVER_IP }}:${{ env.DEPLOY_PATH }}/ |