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 all 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
94 changes: 94 additions & 0 deletions .github/workflows/azure-container-apps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: 'Azure Container Apps CD'

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

permissions:
id-token: write
contents: read

env:
CONTAINER_APP_NAME: microblog-ai
CONTAINER_APP_RESOURCE_GROUP: aswa-remix
REGISTRY_NAME: microblogairegistry
NODE_VERSION: '20'

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- uses: actions/[email protected]

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'

- 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: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

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 'docker/setup-buildx-action' with ref 'v3', not a pinned commit hash
with:
buildkitd-flags: --debug

- name: Login to Azure Container Registry
uses: docker/login-action@v3

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 'docker/login-action' with ref 'v3', not a pinned commit hash
with:
registry: ${{ secrets.REGISTRY_LOGIN_SERVER }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
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 'docker/build-push-action' with ref 'v5', not a pinned commit hash
with:
context: .
push: true
tags: |
${{ secrets.REGISTRY_LOGIN_SERVER }}/microblog-ai:${{ github.sha }}
${{ secrets.REGISTRY_LOGIN_SERVER }}/microblog-ai:latest
build-args: |
NODE_ENV=production
NEXT_TELEMETRY_DISABLED=1
AZURE_OPENAI_API_KEY=${{ secrets.AZURE_OPENAI_API_KEY }}
AZURE_OPENAI_ENDPOINT=${{ secrets.AZURE_OPENAI_ENDPOINT }}
AZURE_OPENAI_DEPLOYMENT_NAME=${{ secrets.AZURE_OPENAI_DEPLOYMENT_NAME }}
AZURE_OPENAI_API_VERSION=${{ secrets.AZURE_OPENAI_API_VERSION }}
cache-from: type=gha
cache-to: type=gha,mode=max
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}

deploy:
needs: build
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
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
78 changes: 78 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Build stage
FROM node:20-alpine AS builder

# Accept build arguments
ARG AZURE_OPENAI_API_KEY
ARG AZURE_OPENAI_ENDPOINT
ARG AZURE_OPENAI_DEPLOYMENT_NAME
ARG AZURE_OPENAI_API_VERSION
ARG NEXT_TELEMETRY_DISABLED=1

# Set environment variables for build stage
ENV AZURE_OPENAI_API_KEY=${AZURE_OPENAI_API_KEY} \
AZURE_OPENAI_ENDPOINT=${AZURE_OPENAI_ENDPOINT} \
AZURE_OPENAI_DEPLOYMENT_NAME=${AZURE_OPENAI_DEPLOYMENT_NAME} \
AZURE_OPENAI_API_VERSION=${AZURE_OPENAI_API_VERSION} \
NODE_ENV=development \
NEXT_TELEMETRY_DISABLED=${NEXT_TELEMETRY_DISABLED}

WORKDIR /app

# Instalar rimraf globalmente para evitar erros
RUN npm install -g rimraf

# Copiar arquivos de dependência e instalar todas as dependências (incluindo devDependencies)
COPY package*.json ./
COPY server/package*.json ./server/

RUN npm ci && \
cd server && \
npm ci && \
cd ..

# Copiar o restante dos arquivos
COPY . .

# Rodar o build
RUN npm run build:all

# Production stage
FROM node:20-alpine

# Set production environment variables
ENV AZURE_OPENAI_API_KEY=${AZURE_OPENAI_API_KEY} \
AZURE_OPENAI_ENDPOINT=${AZURE_OPENAI_ENDPOINT} \
AZURE_OPENAI_DEPLOYMENT_NAME=${AZURE_OPENAI_DEPLOYMENT_NAME} \
AZURE_OPENAI_API_VERSION=${AZURE_OPENAI_API_VERSION} \
NODE_ENV=production \
NEXT_TELEMETRY_DISABLED=1

WORKDIR /app

# Copiar arquivos necessários da fase de build
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/

# Instalar apenas as dependências de produção
RUN npm ci --only=production && \
cd server && \
npm ci --only=production && \
cd .. && \
npm cache clean --force && \
rm -rf /root/.npm

# Security and runtime configuration
USER node
EXPOSE 3000

# Health check configuration
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 command with garbage collection enabled
CMD ["node", "--expose-gc", "./build/server/index.js"]
Loading
Loading