From b6d69142097f845573cddae2c89031a292acf8e8 Mon Sep 17 00:00:00 2001 From: yoonseopkim Date: Tue, 17 Dec 2024 14:09:57 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20Jenkinsfile,=20=EC=8B=A4?= =?UTF-8?q?=EC=A0=9C=20cicd=20=EB=8A=94=20=ED=8C=8C=EC=9D=B4=ED=94=84?= =?UTF-8?q?=EB=9D=BC=EC=9D=B8=EC=97=90=EC=84=9C=20=ED=95=A0=EA=B1=B0?= =?UTF-8?q?=EB=9D=BC=20=EC=A3=BC=EC=84=9D=EC=B2=98=EB=A6=AC=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..a405ecb --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,133 @@ +// pipeline { +// agent any +// +// environment { +// // 환경 변수 파일 경로 설정 +// ENV_FILE = '/var/lib/jenkins/environments/.env.ai' +// // Docker 이미지 정보 +// DOCKER_IMAGE = 'aida0/gitfolio_ai:test' +// // AWS 리전 +// AWS_REGION = 'ap-northeast-2' +// } +// +// stages { +// stage('Load Environment Variables') { +// steps { +// script { +// // .env.ai 파일에서 환경 변수 로드 +// def envContent = readFile(ENV_FILE).trim() +// envContent.split('\n').each { line -> +// def (key, value) = line.split('=', 2) +// env."${key}" = value +// } +// } +// } +// } +// +// // Git 저장소 체크아웃 단계 +// stage('Checkout') { +// steps { +// // Git 저장소 URL을 직접 지정하여 체크아웃 +// git branch: 'develop', +// url: 'https://github.com/KTB-Sixmen/gitfolio_AI.git' +// } +// } +// +// stage('Docker Build & Push') { +// steps { +// script { +// // Docker Hub 로그인 +// withCredentials([usernamePassword(credentialsId: 'docker-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { +// sh """ +// docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} +// +// # Docker 이미지 빌드 +// docker build \ +// --build-arg OPENAI_API_KEY=${env.OPENAI_API_KEY} \ +// --build-arg GH_TOKEN=${env.GH_TOKEN} \ +// --build-arg HOST=${env.HOST} \ +// --build-arg PORT=${env.PORT} \ +// -t ${DOCKER_IMAGE} . +// +// # Docker 이미지 푸시 +// docker push ${DOCKER_IMAGE} +// """ +// } +// } +// } +// } +// +// stage('Deploy to EC2') { +// steps { +// script { +// withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', +// credentialsId: 'aws-credentials', +// accessKeyVariable: 'AWS_ACCESS_KEY_ID', +// secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { +// +// // EC2 인스턴스 ID 조회 +// def instanceIds = sh( +// script: """ +// aws ec2 describe-instances \ +// --region ${AWS_REGION} \ +// --filters 'Name=tag:Service,Values=ai' 'Name=instance-state-name,Values=running' \ +// --query 'Reservations[].Instances[].InstanceId' \ +// --output text +// """, +// returnStdout: true +// ).trim() +// +// if (instanceIds) { +// // docker-compose.yaml 파일 인코딩 +// def dockerComposeContent = sh( +// script: "base64 docker-compose.yaml | tr -d '\n'", +// returnStdout: true +// ).trim() +// +// // SSM 명령 실행 - JSON 형식 수정 +// def commandId = sh( +// script: """ +// aws ssm send-command \ +// --instance-ids "${instanceIds}" \ +// --document-name "AWS-RunShellScript" \ +// --comment "Deploying AI Server" \ +// --parameters '{"commands":["cd /home/ec2-user","echo '\\''${dockerComposeContent}'\\'' | base64 -d > docker-compose.yaml","echo '\\''OPENAI_API_KEY=${env.OPENAI_API_KEY}'\\'' > .env","echo '\\''GH_TOKEN=${env.GH_TOKEN}'\\'' >> .env","echo '\\''HOST=${env.HOST}'\\'' >> .env","echo '\\''PORT=${env.PORT}'\\'' >> .env","chmod 600 .env","docker-compose down -v --rmi all","docker-compose pull","docker-compose up -d"]}' \ +// --timeout-seconds 600 \ +// --region ${AWS_REGION} \ +// --output text \ +// --query 'Command.CommandId' +// """, +// returnStdout: true +// ).trim() +// +// // 명령 실행 완료 대기 +// sh """ +// aws ssm wait command-executed \ +// --command-id ${commandId} \ +// --instance-id ${instanceIds} \ +// --region ${AWS_REGION} +// """ +// +// // 실행 결과 확인 +// sh """ +// aws ssm get-command-invocation \ +// --command-id ${commandId} \ +// --instance-id ${instanceIds} \ +// --region ${AWS_REGION} +// """ +// } else { +// error "No running EC2 instances found with the specified tags" +// } +// } +// } +// } +// } +// } +// +// post { +// always { +// // 작업 완료 후 정리 +// cleanWs() +// } +// } +// } \ No newline at end of file From 8e712e768cd732c6d33c69c6f9359cb1c21df618 Mon Sep 17 00:00:00 2001 From: yoonseopkim Date: Tue, 17 Dec 2024 14:14:36 +0900 Subject: [PATCH 2/2] =?UTF-8?q?delete=20:=20=EC=A0=A0=ED=82=A8=EC=8A=A4=20?= =?UTF-8?q?=EA=B5=AC=EC=84=B1=EC=9C=BC=EB=A1=9C=20=EA=B9=83=ED=97=88?= =?UTF-8?q?=EB=B8=8C=20=EC=95=A1=EC=85=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ai-cicd.yaml | 113 --------------------------------- 1 file changed, 113 deletions(-) delete mode 100644 .github/workflows/ai-cicd.yaml diff --git a/.github/workflows/ai-cicd.yaml b/.github/workflows/ai-cicd.yaml deleted file mode 100644 index 607e887..0000000 --- a/.github/workflows/ai-cicd.yaml +++ /dev/null @@ -1,113 +0,0 @@ -name: FastAPI AI Server CI/CD Pipeline - -on: - push: - branches: - - develop - -jobs: - build_and_deploy: - runs-on: ubuntu-latest - - steps: - # 1단계: 코드 체크아웃 - - name: Checkout code - uses: actions/checkout@v3 - - # 2단계: AWS 자격 증명 구성 - - 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: ap-northeast-2 - - # 3단계: Docker Hub에 로그인 - - name: Log in to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - # 4단계: Docker 이미지 빌드 - - name: Build the Docker image - run: | - docker build \ - --build-arg OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }} \ - --build-arg GH_TOKEN=${{ secrets.GH_TOKEN }} \ - --build-arg HOST=${{ secrets.HOST }} \ - --build-arg PORT=${{ secrets.PORT }} \ - -t ${{ secrets.DOCKER_REPO }}:test . - - # 5단계: Docker 이미지 푸시 - - name: Push the Docker image - run: | - docker push ${{ secrets.DOCKER_REPO }}:test - - # 6단계: EC2 인스턴스 ID 가져오기 - - name: Get EC2 Instance IDs - id: get_instances - run: | - INSTANCE_IDS=$(aws ec2 describe-instances \ - --region ap-northeast-2 \ - --filters '[{"Name":"tag:Service","Values":["ai"]}, {"Name":"instance-state-name","Values":["running"]}]' \ - --query 'Reservations[].Instances[].InstanceId' \ - --output text) - echo "INSTANCE_IDS=$INSTANCE_IDS" - echo "instance_ids=$INSTANCE_IDS" >> $GITHUB_OUTPUT - - # 7단계: AWS SSM을 통해 EC2 인스턴스에 배포 - - name: Deploy to EC2 instances - id: deploy - run: | - if [ -z "${{ steps.get_instances.outputs.instance_ids }}" ]; then - echo "No instance IDs found. Exiting." - exit 1 - fi - - # docker-compose.yaml 파일 내용을 BASE64로 인코딩 - DOCKER_COMPOSE_CONTENT=$(base64 docker-compose.yaml | tr -d '\n') - - # AWS SSM 명령어 실행 - COMMAND_OUTPUT=$(aws ssm send-command \ - --instance-ids "${{ steps.get_instances.outputs.instance_ids }}" \ - --document-name "AWS-RunShellScript" \ - --comment "Deploying AI Server" \ - --parameters '{ - "commands": [ - "cd /home/ec2-user", - "echo '"${DOCKER_COMPOSE_CONTENT}"' | base64 -d > docker-compose.yaml", - "echo '\''OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}'\'' > .env", - "echo '\''GH_TOKEN=${{ secrets.GH_TOKEN }}'\'' >> .env", - "echo '\''HOST=${{ secrets.HOST }}'\'' >> .env", - "echo '\''PORT=${{ secrets.PORT }}'\'' >> .env", - "chmod 600 .env", - "docker-compose down -v --rmi all", - "docker-compose pull", - "docker-compose up -d" - ], - "workingDirectory": ["/home/ec2-user"] - }' \ - --timeout-seconds 600 \ - --region ap-northeast-2) - - echo "COMMAND_OUTPUT=$COMMAND_OUTPUT" - COMMAND_ID=$(echo "$COMMAND_OUTPUT" | jq -r '.Command.CommandId') - echo "COMMAND_ID=$COMMAND_ID" - echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT - - # 8단계: 명령 실행 완료 대기 - - name: Wait for command to complete - run: | - aws ssm wait command-executed \ - --command-id ${{ steps.deploy.outputs.command_id }} \ - --instance-id ${{ steps.get_instances.outputs.instance_ids }} \ - --region ap-northeast-2 - - # 9단계: 명령 결과 확인 - - name: Get command result - run: | - aws ssm get-command-invocation \ - --command-id ${{ steps.deploy.outputs.command_id }} \ - --instance-id ${{ steps.get_instances.outputs.instance_ids }} \ - --region ap-northeast-2 \ No newline at end of file