Skip to content
Open

Son #52

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
46 changes: 23 additions & 23 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
name: Deploy to Server
# name: Deploy to Server

on:
workflow_run:
workflows: ["Docker Build and Push"]
types:
- completed
# on:
# workflow_run:
# workflows: ["Docker Build and Push"]
# types:
# - completed

jobs:
deploy:
name: Deploy Container
runs-on: ubuntu-latest
# jobs:
# deploy:
# name: Deploy Container
# runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
# steps:
# - name: Checkout repository
# uses: actions/checkout@v3

- name: Pull latest Docker image
run: docker jungwoochan/fastapi-app:latest
# - name: Pull latest Docker image
# run: docker jungwoochan/fastapi-app:latest

- name: Stop and remove old container
run: |
docker stop fastapi-app || true
docker rm fastapi-app || true
# - name: Stop and remove old container
# run: |
# docker stop fastapi-app || true
# docker rm fastapi-app || true

- name: Run new container
run: |
docker run -d --name fastapi-app -p 8000:8000 \
your-dockerhub-username/fastapi-app:latest
# - name: Run new container
# run: |
# docker run -d --name fastapi-app -p 8000:8000 \
# your-dockerhub-username/fastapi-app:latest
38 changes: 29 additions & 9 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: Docker Build and Push
name: Docker Build and Push to ECR

on:
push:
branches:
- main

env:
DOCKER_HUB_REPOSITORY: sonyeoul/fastapi-app
AWS_REGION: ap-northeast-2
ECR_REPOSITORY_URI: 784616196610.dkr.ecr.ap-northeast-2.amazonaws.com/fastapi-app

jobs:
build:
Expand All @@ -23,16 +24,35 @@ jobs:
echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .env
echo "UPSTAGE_API_KEY=${{ secrets.UPSTAGE_API_KEY }}" >> .env
echo "ENV=production" >> .env

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
run: |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | \
docker login --username AWS --password-stdin ${{ env.ECR_REPOSITORY_URI }}

- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin

- name: Build and Push Docker Image
- name: Build and Push Docker Image to ECR
run: |
docker buildx build --platform linux/amd64 \
--tag ${{ env.DOCKER_HUB_REPOSITORY }}:latest \
--tag ${{ env.DOCKER_HUB_REPOSITORY }}:${{ github.sha }} \
--cache-to=type=inline \
--tag ${{ env.ECR_REPOSITORY_URI }}:latest \
--tag ${{ env.ECR_REPOSITORY_URI }}:${{ github.sha }} \
--push .
- name: Notify Discord
if: success()
run: |
curl -X POST "https://discord.com/api/v10/channels/1357169309050212432/messages" \
-H "Authorization: Bot ${{ secrets.DISCORD_BOT_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"content": "[FastAPI] ECR 이미지 빌드 및 푸시 완료!\n Repo: ${{ github.repository }}\n🔀 Commit: ${{ github.sha }}\n👤 By: ${{ github.actor }}"
}'

60 changes: 22 additions & 38 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
# 1단계: 빌드 단계 (Poetry 설치 및 의존성 설정)
FROM python:3.12-slim AS builder

# 필수 패키지 설치
RUN apt-get update && apt-get install -y curl build-essential

# Poetry 설치
ENV POETRY_VERSION=2.1.2
RUN curl -sSL https://install.python-poetry.org | python3 -

# Poetry 경로 설정
ENV PATH="/root/.local/bin:$PATH"
ENV POETRY_VIRTUALENVS_CREATE=false

# 작업 디렉토리 설정
WORKDIR /

# pyproject.toml 및 poetry.lock 복사
COPY pyproject.toml poetry.lock ./

# 의존성 설치
RUN poetry install --no-root --only main

# 2단계: 실행 단계
FROM python:3.12-slim

# 작업 디렉토리
WORKDIR /

# 위에서 설치한 패키지를 복사
COPY --from=builder /usr/local/lib/python3.12 /usr/local/lib/python3.12
COPY --from=builder /root/.local /root/.local
ENV PATH="/root/.local/bin:$PATH"

# 앱 소스 코드 복사
COPY . .

# FastAPI 앱 실행
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

RUN apt-get update && apt-get install -y curl build-essential

ENV POETRY_VERSION=2.1.2
RUN curl -sSL https://install.python-poetry.org | python3 -
ENV PATH="/root/.local/bin:$PATH"
ENV POETRY_VIRTUALENVS_CREATE=false

WORKDIR /app
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-root --only main

FROM python:3.12-slim
WORKDIR /app

COPY --from=builder /usr/local/lib/python3.12 /usr/local/lib/python3.12
COPY --from=builder /root/.local /root/.local
ENV PATH="/root/.local/bin:$PATH"

COPY . .

CMD ["poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Loading