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 5 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/checkout@v3

# Azure Login
- name: Azure Login
uses: azure/login@v1
Fixed Show fixed Hide fixed

Check warning

Code scanning / CodeQL

Unpinned tag for a non-immutable Action in workflow Medium

Unpinned 3rd party Action 'Azure Container Apps CD' step
Uses Step
uses 'azure/login' with ref 'v1', not a pinned commit hash
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Login to Azure Container Registry
uses: docker/login-action@v2
Fixed Show fixed Hide fixed
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@v2
Fixed Show fixed Hide fixed

- name: Build and push Docker image
uses: docker/build-push-action@v4
Fixed Show fixed Hide fixed
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@v1

Check warning

Code scanning / CodeQL

Unpinned tag for a non-immutable Action in workflow Medium

Unpinned 3rd party Action 'Azure Container Apps CD' step
Uses Step
uses 'azure/login' with ref 'v1', not a pinned commit hash
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Deploy to Azure Container Apps
uses: azure/container-apps-deploy-action@v1

Check warning

Code scanning / CodeQL

Unpinned tag for a non-immutable Action in workflow Medium

Unpinned 3rd party Action 'Azure Container Apps CD' step
Uses Step
uses 'azure/container-apps-deploy-action' with ref 'v1', not a pinned commit hash
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