diff --git a/.github/workflows/backend-cicd.yml b/.github/workflows/backend-cicd.yml new file mode 100644 index 0000000..d395b91 --- /dev/null +++ b/.github/workflows/backend-cicd.yml @@ -0,0 +1,61 @@ +name: Backend CI/CD + +on: + push: + branches: [ main, feature/my-active-tasks ] + paths: + - 'backend/**' + pull_request: + branches: [ main ] + paths: + - 'backend/**' + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '18' + cache: 'npm' + cache-dependency-path: backend/package-lock.json + + - name: Install Dependencies + run: | + cd backend + npm ci + + - name: Build Backend + run: | + cd backend + npm run build + + + deploy: + needs: build-and-test + runs-on: ubuntu-latest + if: github.event_name == 'push' && (github.ref == 'refs/heads/main') + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + + - name: Deploy to EC2 via SSH + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_SSH_KEY }} + script: | + cd ~/teamup + git pull origin main + cd backend + docker-compose down + docker-compose up --build -d + docker image prune -f diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..a60d323 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,10 @@ +node_modules +dist +src +.env +.git +.github +*.md +Dockerfile +.dockerignore +npm-debug.log diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..5f849fa --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,23 @@ +FROM node:18-alpine AS builder + +WORKDIR /app + +COPY package*.json ./ + +RUN npm ci + +COPY . . +RUN npm run build + +FROM node:18-alpine AS runner + +WORKDIR /app +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/node_modules ./node_modules + +EXPOSE 5000 + +ENV NODE_ENV=production + +CMD ["npm", "start"] diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml new file mode 100644 index 0000000..b1fc62a --- /dev/null +++ b/backend/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.8' + +services: + backend: + build: + context: . + dockerfile: Dockerfile + ports: + - "5000:5000" + env_file: + - .env + environment: + - NODE_ENV=production + restart: always