diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..6bd661b --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,104 @@ +name: Build and Deploy + +on: + push: + branches: [main] + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install Docker CLI + run: | + apt-get update + apt-get install -y ca-certificates curl gnupg + install -m 0755 -d /etc/apt/keyrings + curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + chmod a+r /etc/apt/keyrings/docker.gpg + + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \ + bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + + apt-get update + apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Harbor Registry + uses: docker/login-action@v2 + with: + registry: harbor.galpodlipnik.com + username: ${{ secrets.HARBOR_USERNAME }} + password: ${{ secrets.HARBOR_PASSWORD }} + + - name: Build and push Backend image + uses: docker/build-push-action@v4 + with: + context: ./backend + push: true + tags: harbor.galpodlipnik.com/chat-app/backend:latest + + - name: Build and push Frontend image + uses: docker/build-push-action@v4 + with: + context: ./frontend + push: true + tags: harbor.galpodlipnik.com/chat-app/frontend:latest + + - name: Create docker-compose.yml + run: | + cat > docker-compose.yml << 'EOL' + version: '3.8' + services: + backend: + image: harbor.galpodlipnik.com/chat-app/backend:latest + restart: always + env_file: + - ./backend/.env + ports: + - "3000:3000" + networks: + - chat-network + + frontend: + image: harbor.galpodlipnik.com/chat-app/frontend:latest + restart: always + ports: + - "80:80" + depends_on: + - backend + networks: + - chat-network + + networks: + chat-network: + driver: bridge + EOL + + - name: Deploy with SSH + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.DEPLOY_HOST }} + username: ${{ secrets.DEPLOY_USERNAME }} + key: ${{ secrets.DEPLOY_SSH_KEY }} + script: | + mkdir -p ~/services/chat-app + cd ~/services/chat-app + + # Copy docker-compose.yml to server + echo "${{ github.workspace }}/docker-compose.yml" > docker-compose.yml + + # Copy environment file if needed + # This assumes .env file is in the repository + echo "${{ secrets.ENV_CONTENT }}" > .env + + # Pull latest images and deploy + docker compose pull + docker compose down + docker compose up -d \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..0fb0b6d --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,12 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package.json package-lock.json ./ +RUN npm ci + +COPY . . +RUN npm run build + +EXPOSE 3000 +CMD ["npm", "start"] \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..8b64b9d --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,15 @@ +FROM node:20-alpine AS builder + +WORKDIR /app + +COPY package.json package-lock.json ./ +RUN npm ci + +COPY . . +RUN npm run build + +FROM nginx:alpine +COPY --from=builder /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..e7ad039 --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,29 @@ +d/nginx.conf +server { + listen 80; + server_name _; + + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + location /api { + proxy_pass http://backend:3000/api; + 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; + } + + location /socket.io { + proxy_pass http://backend:3000/socket.io; + 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; + } +} \ No newline at end of file