Skip to content

Commit

Permalink
use docker for deploys
Browse files Browse the repository at this point in the history
  • Loading branch information
hitchhooker committed Dec 13, 2024
1 parent 7402bab commit 0e7d2ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
55 changes: 27 additions & 28 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ 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
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: 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/[email protected]
Expand All @@ -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 }}/
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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;"]
19 changes: 19 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0e7d2ca

Please sign in to comment.