-
Notifications
You must be signed in to change notification settings - Fork 0
[OT-296][FEAT]: workflow 작성 및 서버 간 통신 연결 설정 #166
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd6dd28
[CHORE]: 환경변수 통합
marulog 862206a
[FEAT]: AI 태깅 비동기 호출을 EventListener 도입
marulog 9621b9b
[FEAT]: AI 서버 런타임/도커 설정
marulog bf5657a
[FEAT]: AI 서버 배포 워크플로우 추가
marulog 286a78f
[FIX]: 트랜잭션 추가
marulog 417d620
[CHORE]: 비동기, 블로킹 전략
marulog 34092c7
[REFACTOR] 트랜잭션 범위를 DB 저장과 AI 호출부 분리
marulog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| name: Deploy AI Service To EC2 | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| image_tag: | ||
| description: "Docker image tag to deploy (default: commit SHA)" | ||
| required: false | ||
| type: string | ||
| pull_request: | ||
| types: | ||
| - closed | ||
|
|
||
| env: | ||
| AWS_REGION: ap-northeast-2 | ||
| SERVICE_NAME: machine | ||
| ECR_REPO: oplust-machine | ||
|
|
||
| jobs: | ||
| build-and-push: | ||
| if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && github.event.pull_request.head.ref == 'develop') }} | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v4 | ||
| 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 ECR | ||
| uses: aws-actions/amazon-ecr-login@v2 | ||
|
|
||
| - name: Ensure ECR repository exists | ||
| run: | | ||
| aws ecr describe-repositories --repository-names "${{ env.ECR_REPO }}" >/dev/null 2>&1 || \ | ||
| aws ecr create-repository --repository-name "${{ env.ECR_REPO }}" >/dev/null | ||
|
|
||
| - name: Download tagging model | ||
| run: aws s3 cp "${{ secrets.AI_TAGGING_MODEL_S3_URI }}" ./apps/machine/models/tagging/ --recursive | ||
|
|
||
| - name: Download recommend model | ||
| run: aws s3 cp "${{ secrets.AI_RECOMMEND_MODEL_S3_URI }}" ./apps/machine/models/recommend/ --recursive | ||
|
|
||
| - name: Build and push image | ||
| env: | ||
| ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com | ||
| IMAGE_TAG_INPUT: ${{ github.event.inputs.image_tag }} | ||
| run: | | ||
| IMAGE_TAG="${IMAGE_TAG_INPUT:-${GITHUB_SHA}}" | ||
| IMAGE_URI="${ECR_REGISTRY}/${ECR_REPO}:${IMAGE_TAG}" | ||
| IMAGE_URI_LATEST="${ECR_REGISTRY}/${ECR_REPO}:latest" | ||
|
|
||
| docker build \ | ||
| -f "apps/machine/Dockerfile" \ | ||
| -t "${IMAGE_URI}" \ | ||
| -t "${IMAGE_URI_LATEST}" \ | ||
| . | ||
|
|
||
| docker push "${IMAGE_URI}" | ||
| docker push "${IMAGE_URI_LATEST}" | ||
|
|
||
| deploy: | ||
| if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && github.event.pull_request.head.ref == 'develop') }} | ||
| runs-on: ubuntu-latest | ||
| needs: build-and-push | ||
|
|
||
| steps: | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v4 | ||
| 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: Deploy AI service to EC2 via SSM | ||
| env: | ||
| ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com | ||
| IMAGE_TAG_INPUT: ${{ github.event.inputs.image_tag }} | ||
| PROJECT_NAME: oplust | ||
| SSM_MACHINE_ENV_PARAM: /oplust/machine/env | ||
| run: | | ||
| set -euo pipefail | ||
|
|
||
| IMAGE_TAG="${IMAGE_TAG_INPUT:-${GITHUB_SHA}}" | ||
| IMAGE_URI="${ECR_REGISTRY}/${ECR_REPO}:${IMAGE_TAG}" | ||
| TARGET_TAG="${PROJECT_NAME}-machine-ec2" | ||
| CONTAINER_NAME="oplust-machine" | ||
| ENV_FILE="/etc/oplust/machine.env" | ||
| PORT="8000" | ||
|
|
||
| MONITORING_PRIVATE_IP=$(aws ec2 describe-instances \ | ||
| --region "$AWS_REGION" \ | ||
| --filters "Name=tag:Name,Values=${PROJECT_NAME}-monitoring-ec2" "Name=instance-state-name,Values=running" \ | ||
| --query "Reservations[0].Instances[0].PrivateIpAddress" \ | ||
| --output text) | ||
|
|
||
| if [ -z "$MONITORING_PRIVATE_IP" ] || [ "$MONITORING_PRIVATE_IP" = "None" ]; then | ||
| echo "No running monitoring instance found for tag: ${PROJECT_NAME}-monitoring-ec2" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| INSTANCE_ID=$(aws ec2 describe-instances \ | ||
| --region "$AWS_REGION" \ | ||
| --filters "Name=tag:Name,Values=${TARGET_TAG}" "Name=instance-state-name,Values=running" \ | ||
| --query "Reservations[0].Instances[0].InstanceId" \ | ||
| --output text) | ||
|
|
||
| if [ -z "$INSTANCE_ID" ] || [ "$INSTANCE_ID" = "None" ]; then | ||
| echo "No running instance found for tag: ${TARGET_TAG}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| PROMTAIL_CONFIG_B64=$(printf '%s\n' \ | ||
| 'server:' \ | ||
| ' http_listen_port: 9080' \ | ||
| ' grpc_listen_port: 0' \ | ||
| '' \ | ||
| 'positions:' \ | ||
| ' filename: /tmp/positions.yaml' \ | ||
| '' \ | ||
| 'clients:' \ | ||
| " - url: http://${MONITORING_PRIVATE_IP}:3100/loki/api/v1/push" \ | ||
| '' \ | ||
| 'scrape_configs:' \ | ||
| ' - job_name: docker' \ | ||
| ' static_configs:' \ | ||
| ' - targets: [localhost]' \ | ||
| ' labels:' \ | ||
| ' job: docker' \ | ||
| ' role: ai' \ | ||
| ' __path__: /var/lib/docker/containers/*/*-json.log' \ | ||
| ' pipeline_stages:' \ | ||
| ' - docker: {}' \ | ||
| | base64 | tr -d '\n') | ||
|
|
||
| PARAMS_FILE=$(mktemp) | ||
| COMMANDS=( | ||
| "set -e" | ||
| "sudo mkdir -p /etc/oplust" | ||
| "SERVICE_ENV=\$(aws ssm get-parameter --region $AWS_REGION --name '$SSM_MACHINE_ENV_PARAM' --with-decryption --query 'Parameter.Value' --output text)" | ||
| "printf '%s\n' \"\$SERVICE_ENV\" | sudo tee ${ENV_FILE} >/dev/null" | ||
| "sudo chmod 600 ${ENV_FILE}" | ||
| "aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_REGISTRY" | ||
| "sudo docker pull ${IMAGE_URI}" | ||
| "sudo docker rm -f ${CONTAINER_NAME} || true" | ||
| "sudo docker run -d --name ${CONTAINER_NAME} --restart unless-stopped -p ${PORT}:${PORT} --env-file ${ENV_FILE} ${IMAGE_URI}" | ||
| "sudo mkdir -p /opt/oplust-promtail /opt/oplust-promtail/positions" | ||
| "echo '${PROMTAIL_CONFIG_B64}' | base64 -d | sudo tee /opt/oplust-promtail/promtail.yml >/dev/null" | ||
| "sudo docker rm -f promtail || true" | ||
| "sudo docker run -d --name promtail --restart unless-stopped -v /opt/oplust-promtail/promtail.yml:/etc/promtail/config.yml:ro -v /opt/oplust-promtail/positions:/tmp -v /var/lib/docker/containers:/var/lib/docker/containers:ro grafana/promtail:2.9.8 -config.file=/etc/promtail/config.yml" | ||
| ) | ||
| printf '%s\n' "${COMMANDS[@]}" | jq -R . | jq -s '{commands: .}' > "$PARAMS_FILE" | ||
|
|
||
| COMMAND_ID=$(aws ssm send-command \ | ||
| --region "$AWS_REGION" \ | ||
| --instance-ids "$INSTANCE_ID" \ | ||
| --document-name "AWS-RunShellScript" \ | ||
| --comment "Deploy ${CONTAINER_NAME}:${IMAGE_TAG}" \ | ||
| --parameters "file://${PARAMS_FILE}" \ | ||
| --query 'Command.CommandId' \ | ||
| --output text) | ||
|
|
||
| rm -f "$PARAMS_FILE" | ||
|
|
||
| echo "[${CONTAINER_NAME}] command id: ${COMMAND_ID} (instance: ${INSTANCE_ID})" | ||
|
|
||
| for _ in $(seq 1 120); do | ||
| STATUS=$(aws ssm get-command-invocation \ | ||
| --region "$AWS_REGION" \ | ||
| --command-id "$COMMAND_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query 'Status' \ | ||
| --output text 2>/dev/null || true) | ||
|
|
||
| case "$STATUS" in | ||
| Success) | ||
| echo "[${CONTAINER_NAME}] deployment success" | ||
| exit 0 | ||
| ;; | ||
| Failed|Cancelled|TimedOut) | ||
| echo "[${CONTAINER_NAME}] deployment failed with status: ${STATUS}" >&2 | ||
| aws ssm get-command-invocation --region "$AWS_REGION" --command-id "$COMMAND_ID" --instance-id "$INSTANCE_ID" --query '{StdOut:StandardOutputContent,StdErr:StandardErrorContent}' --output json || true | ||
| exit 1 | ||
| ;; | ||
| Pending|InProgress|Delayed|"") | ||
| sleep 5 | ||
| ;; | ||
| *) | ||
| echo "[${CONTAINER_NAME}] unexpected status: ${STATUS}" >&2 | ||
| sleep 5 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| echo "[${CONTAINER_NAME}] deployment timed out waiting for SSM command completion" >&2 | ||
| aws ssm get-command-invocation --region "$AWS_REGION" --command-id "$COMMAND_ID" --instance-id "$INSTANCE_ID" --query '{StdOut:StandardOutputContent,StdErr:StandardErrorContent}' --output json || true | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
apps/api-admin/src/main/java/com/ott/api_admin/tagging/event/AiTaggingRequestedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.ott.api_admin.tagging.event; | ||
|
|
||
| public record AiTaggingRequestedEvent(Long mediaId, String description) { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.