AI Server CI/CD #19
This file contains 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
name: AI Server CI/CD | |
on: | |
push: | |
workflow_dispatch: | |
permissions: | |
contents: read | |
jobs: | |
AI-Server-CI: | |
runs-on: ubuntu-latest | |
# strategy: | |
# matrix: | |
# module: | |
# - flows/autobiographies/standard/generate_autobiography | |
# - flows/autobiographies/standard/generate_correction | |
# - flows/chapters/standard/generate_chapter | |
# - flows/interviews/chat/interview_chat | |
# - flows/interviews/standard/generate_interview_questions | |
steps: | |
- name: 체크아웃 | |
uses: actions/checkout@v3 | |
- name: Set up Python 3.9 | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.9" | |
# - name: Install dependencies for ${{ matrix.module }} | |
# run: | | |
# if [ -f ${{ matrix.module }}/requirements.txt ]; then | |
# pip install -r ${{ matrix.module }}/requirements.txt | |
# else | |
# echo "No requirements.txt found for ${{ matrix.module }}. Skipping dependency installation." | |
# fi | |
# - name: Batch Run Tests for ${{ matrix.module }} | |
# # TODO: Batct Run Tests logic 추가 | |
# run: | | |
# cd ${{ matrix.module }} | |
# python test_script.py | |
- name: Cache pip packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/pip | |
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
- name: Install Dedendency for FastAPI Server | |
run: | | |
cd serve | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Verify Application Build and Run | |
run: | | |
cd serve | |
echo ${{ secrets.ENV_PRODUCTION }} | base64 -d > .env.production | |
# uvicorn main:app --env-file .env.production --port 3000 & | |
# sleep 10 # Give some time for the server to start | |
# curl -f http://127.0.0.1:3000 || (echo "Application failed to start" && exit 1) | |
# pkill -f "uvicorn main:app --env-file .env.production --port 3000" | |
shell: bash | |
- name: Configure AWS credentials | |
if: ${{ github.ref == 'refs/heads/main' }} | |
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 }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Build and push Docker image to Amazon ECR | |
if: ${{ github.ref == 'refs/heads/main' }} | |
env: | |
REGISTRY: 211125363878.dkr.ecr.ap-northeast-2.amazonaws.com | |
REPOSITORY: lifebookshelf-ai | |
IMAGE_TAG: latest | |
run: | | |
cp -r flows deploy-main/ | |
cp -r serve deploy-main/ | |
cd deploy-main | |
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin $REGISTRY | |
docker buildx build --platform linux/amd64,linux/arm64 -t $REGISTRY/$REPOSITORY:$IMAGE_TAG --push . | |
rm -rf flows/ | |
- name: Upload deployment package to S3 and trigger CodeDeploy | |
if: ${{ github.ref == 'refs/heads/main' }} | |
run: | | |
mkdir -p deploy && cp -r deploy-main/* deploy/ | |
zip -r deploy.zip deploy | |
aws s3 cp deploy.zip s3://${{ secrets.AWS_S3_DEPLOY_MAIN_BUCKET_NAME }}/deploy.zip | |
DEPLOYMENT_ID=$(aws deploy create-deployment \ | |
--application-name ${{ secrets.AWS_CODEDEPLOY_MAIN_APP_NAME }} \ | |
--deployment-config-name CodeDeployDefault.AllAtOnce \ | |
--deployment-group-name ${{ secrets.AWS_CODEDEPLOY_MAIN_GROUP_NAME }} \ | |
--file-exists-behavior OVERWRITE \ | |
--s3-location bucket=${{ secrets.AWS_S3_DEPLOY_MAIN_BUCKET_NAME }},bundleType=zip,key=deploy.zip \ | |
--output text --query 'deploymentId') | |
echo "DEPLOYMENT_ID=$DEPLOYMENT_ID" >> $GITHUB_ENV | |
- name: Check Deployment Status and Notify | |
if: ${{ github.ref == 'refs/heads/main' }} | |
run: | | |
TIMEOUT=600 | |
INTERVAL=15 | |
ELAPSED=0 | |
while [ $ELAPSED -le $TIMEOUT ]; do | |
STATUS=$(aws deploy get-deployment --deployment-id $DEPLOYMENT_ID --output text --query 'deploymentInfo.status') | |
if [[ "$STATUS" == "Succeeded" ]]; then | |
curl -H "Content-Type: application/json" \ | |
-d '{"content": "AI Server Deployment succeeded! 🚀"}' \ | |
${{ secrets.DISCORD_WEBHOOK_URL }} | |
exit 0 | |
elif [[ "$STATUS" == "Failed" || "$STATUS" == "Stopped" ]]; then | |
curl -H "Content-Type: application/json" \ | |
-d '{"content": "AI Server Deployment failed. 🚨"}' \ | |
${{ secrets.DISCORD_WEBHOOK_URL }} | |
exit 1 | |
fi | |
sleep $INTERVAL | |
let ELAPSED=ELAPSED+INTERVAL | |
done | |
curl -H "Content-Type: application/json" \ | |
-d '{"content": "AI Server Deployment timed out. ⏲️"}' \ | |
${{ secrets.DISCORD_WEBHOOK_URL }} | |
exit 1 | |
shell: bash |