From 0e7d2ca4f9817a207c909a1acc0d66d31bb4059c Mon Sep 17 00:00:00 2001 From: hitchhooker Date: Fri, 13 Dec 2024 14:37:51 +0700 Subject: [PATCH] use docker for deploys --- .github/workflows/deploy.yaml | 55 +++++++++++++++++------------------ Dockerfile | 16 ++++++++++ nginx.conf | 19 ++++++++++++ 3 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 Dockerfile create mode 100644 nginx.conf diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 3c5a727..8442bd2 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -3,11 +3,13 @@ env: SERVER_USER: 'swissh' SERVER_IP: 'swis.sh' DEPLOY_PATH: '/home/swissh/swis.sh/' - BACKUP_PATH: '/home/swissh/backup.swis.sh/' + DOCKER_IMAGE: 'swissh-app' + on: push: branches: - - master # Adjust if your main branch is named differently + - master + jobs: build-and-deploy: runs-on: ubuntu-latest @@ -15,16 +17,12 @@ jobs: - 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: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 - - name: Build project - run: bun run build + - name: Build Docker image + run: | + docker build -t ${{ env.DOCKER_IMAGE }} . - name: Set up SSH agent uses: webfactory/ssh-agent@v0.5.3 @@ -35,22 +33,23 @@ jobs: run: | echo "$(ssh-keyscan -t rsa ${{ env.SERVER_IP }} 2>/dev/null)" >> ~/.ssh/known_hosts - - name: Deploy using rsync + - name: Save Docker image + run: docker save ${{ env.DOCKER_IMAGE }} | gzip > image.tar.gz + + - name: Copy Docker image to server + run: | + scp image.tar.gz ${{ env.SERVER_USER }}@${{ env.SERVER_IP }}:${{ env.DEPLOY_PATH }}/ + + - name: Deploy using Docker 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 }} + ssh ${{ env.SERVER_USER }}@${{ env.SERVER_IP }} << 'EOF' + cd ${{ env.DEPLOY_PATH }} + docker load < image.tar.gz + docker stop ${{ env.DOCKER_IMAGE }} || true + docker rm ${{ env.DOCKER_IMAGE }} || true + docker run -d --name ${{ env.DOCKER_IMAGE }} \ + -p 80:80 \ + --restart unless-stopped \ + ${{ env.DOCKER_IMAGE }} + rm image.tar.gz EOF - - rsync -avz --delete .output/public/ ${{ env.SERVER_USER }}@${{ env.SERVER_IP }}:${{ env.DEPLOY_PATH }}/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c5f444f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +# Build stage +FROM oven/bun:1 as builder +WORKDIR /app +COPY package.json bun.lockb ./ +RUN bun install +COPY . . +RUN bun run build + +# Production stage +FROM nginx:alpine +COPY --from=builder /app/.output/public /usr/share/nginx/html +COPY --from=builder /app/.output/server /app/server +COPY nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..f82c672 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,19 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + + location / { + try_files $uri $uri/ /index.html; + } + + # If you need to proxy to the Node server + location /api { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } +}