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

[#24]chore: 자동 배포화 적용 #26

Merged
merged 2 commits into from
Oct 24, 2024
Merged
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
85 changes: 85 additions & 0 deletions .github/workflows/cicd-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Deploy to Amazon ECS

on:
push:
branches: [ "develop" ]

env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: edupi_assist
ECS_SERVICE: assist
ECS_CLUSTER: BE-fargate
ECS_TASK_DEFINITION: edupi_assist.json
CONTAINER_NAME: assist

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "export PATH=\"$HOME/.local/bin:\$PATH\"" >> $GITHUB_ENV

- name: Install dependencies with Poetry
run: poetry install --no-interaction --no-ansi

- name: Copy dot env
env:
CREATE_SECRET: ${{secrets.EDUPI_ASSIT_DEV_DOTENV}}
CREATE_SECRET_DIR_FILE_NAME: .env
run: echo $CREATE_SECRET | base64 --decode > /$CREATE_SECRET_DIR_FILE_NAME

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

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
34 changes: 34 additions & 0 deletions .github/workflows/pull-request-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Pull Request build

on:
pull_request:
branches: [ "develop" ]

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "export PATH=\"$HOME/.local/bin:\$PATH\"" >> $GITHUB_ENV

- name: Install dependencies with Poetry
run: poetry install --no-interaction --no-ansi

- name: pytest
run: poetry run pytest
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.11-alpine

WORKDIR /home/assist
RUN pip install poetry

COPY pyproject.toml /home/assist
COPY poetry.lock /home/assist
COPY .env /home/assist

COPY app /home/assist/app

RUN poetry install --no-root

EXPOSE 8001

ENTRYPOINT [ "poetry" ,"run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001", "--reload"]
51 changes: 51 additions & 0 deletions edupi_assist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"containerDefinitions": [
{
"name": "assist",
"image": "590184013289.dkr.ecr.ap-northeast-2.amazonaws.com/edupi_assist:latest",
"cpu": 0,
"portMappings": [
{
"name": "assist-port",
"containerPort": 8001,
"hostPort": 8001,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/edupi_assist",
"mode": "non-blocking",
"awslogs-create-group": "true",
"max-buffer-size": "25m",
"awslogs-region": "ap-northeast-2",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
},
"systemControls": []
}
],
"family": "edupi_assist",
"executionRoleArn": "arn:aws:iam::590184013289:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"volumes": [],
"placementConstraints": [],
"requiresCompatibilities": [
"EC2",
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"tags": []
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python-dotenv = "^1.0.1"
pytest-mock = "^3.14.0"
openai = "^1.50.2"
restrictedpython = "^7.3"
aiofiles = "^24.1.0"

[tool.black]
line-length = 120
Expand Down
Loading