Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate from Azure Static Web Apps to Azure Container Apps #16

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
npm-debug.log
build
.env
.git
.gitignore
README.md
.vscode
62 changes: 62 additions & 0 deletions .github/workflows/azure-container-apps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Build and Deploy to Azure Container Apps

on:
push:
branches: ['main']
workflow_dispatch:

env:
CONTAINER_APP_NAME: microblog-ai
CONTAINER_APP_RESOURCE_GROUP: aswa-remix
REGISTRY_NAME: microblogairegistry

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]

# Add Azure Login
- name: Azure Login
uses: azure/login@92a5484dfaf04ca78a94597f4f19fea633851fa2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Login to Azure Container Registry
uses: docker/login-action@465a07811f14bebb1438181024f63163c1dae4d9
with:
registry: ${{ secrets.REGISTRY_LOGIN_SERVER }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

# Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@8c0edbc76e98fa90f69d9a2c020dcb50019dc325

- name: Build and push Docker image
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825
with:
context: .
push: true
tags: ${{ secrets.REGISTRY_LOGIN_SERVER }}/microblog-ai:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max

deploy:
Fixed Show fixed Hide fixed
needs: build
runs-on: ubuntu-latest
steps:
- name: Azure Login
uses: azure/login@92a5484dfaf04ca78a94597f4f19fea633851fa2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Deploy to Azure Container Apps
uses: azure/container-apps-deploy-action@8b255db282f6e6999b5ac16967445dfeb3492f5c
with:
containerAppName: ${{ env.CONTAINER_APP_NAME }}
resourceGroup: ${{ env.CONTAINER_APP_RESOURCE_GROUP }}
imageToDeploy: ${{ secrets.REGISTRY_LOGIN_SERVER }}/microblog-ai:${{ github.sha }}
targetPort: 3000
ingress: external
environmentName: env-microblog-ai
Fixed Show fixed Hide fixed
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#Build stage
FROM node:20-alpine AS builder

# Set environment variables to optimize Node.js container
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

WORKDIR /app

# Copy package* files
COPY package*.json ./
COPY server/package*.json ./server/

# Install dependencies
RUN npm ci
RUN cd server && npm ci

# Copy source code
COPY . .

# Build the application
RUN npm run build:all

# Production stage
FROM node:20-alpine

# Set environment variables to production
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

WORKDIR /app

# Copy built files from builder stage
COPY --from=builder /app/build ./build
COPY --from=builder /app/server/dist ./server/dist
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/server/package*.json ./server/

# Install production dependencies only
RUN npm ci --only=production && \
cd server && npm ci --only=production && \
cd .. && \
# Cleaning cache and temporary files to reduce image size
npm cache clean --force && \
rm -rf /root/.npm

# Set up a non-root user for security reasons
USER node

# Expose port
EXPOSE 3000

# Healthcheck to monitor the container
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD node -e "try { require('http').get('http://localhost:3000/health', (r) => r.statusCode === 200 ? process.exit(0) : process.exit(1)); } catch (e) { process.exit(1); }"

# Start the server with proper signal handling
CMD ["node", "--expose-gc", "./build/server/index.js"]
Loading
Loading