Merge pull request #11 from ktb3-team4/feature/clay/db #28
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
| name: Spring Push CI/CD (v 1.1) | |
| env: | |
| IMAGE_TAG: 1.0.0 | |
| on: | |
| pull_request: | |
| branches: | |
| - develop | |
| push: | |
| branches: | |
| - develop | |
| - infra/ci-cd-test | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 # 깃허브 액션이 코드가져옴 | |
| - name: Docker Hub Login | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{secrets.DOCKER_USERNAME}} | |
| password: ${{secrets.DOCKER_TOKEN}} | |
| - name: Spring Image Build and Push | |
| run: | | |
| docker build --platform linux/amd64 \ | |
| -t ${{ secrets.IMAGE }} \ | |
| --push . | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. SSH 초기 설정 | |
| - name: Set up SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| # 2. 깃허브 액션에서 env 생성 | |
| - name: Create .env file | |
| run: | | |
| echo "RDS_DNS=${{ secrets.RDS_DNS }}" >> .env | |
| echo "MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }}" >> .env | |
| echo "MYSQL_ROOT_PASSWORD=${{ secrets.MYSQL_ROOT_PASSWORD }}" >> .env | |
| echo "IMAGE=${{ secrets.IMAGE }}" >> .env | |
| # 3. 깃허브 액션에 AWS CLI 설치 | |
| - name: Install AWS CLI | |
| uses: unfor19/install-aws-cli-action@v1 | |
| with: | |
| version: 2 | |
| # 4. 깃허브 액션 CLI 환경변수 설정 | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v3 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ap-northeast-2 | |
| # 5. Healthy instance IDs 가져오기 | |
| - name: Get Healthy BE Instance IDs | |
| run: | | |
| BE_INSTANCE_IDS=$(aws elbv2 describe-target-health \ | |
| --target-group-arn "${{ secrets.BE_TARGET_GROUP_ARN }}" \ | |
| --query "TargetHealthDescriptions[?TargetHealth.State=='healthy'].Target.Id" \ | |
| --output text) | |
| echo "BE_INSTANCE_IDS=$BE_INSTANCE_IDS" >> $GITHUB_ENV | |
| echo "Healthy Instance IDs: $BE_INSTANCE_IDS" | |
| # 6. Healthy instance들의 Public IP 가져오기 | |
| - name: Get BE Public IPs | |
| run: | | |
| BE_IPS=$(aws ec2 describe-instances \ | |
| --filters "Name=tag:aws:autoscaling:groupName,Values=ou-be" \ | |
| --query "Reservations[].Instances[].PublicIpAddress" \ | |
| --output text) | |
| echo "BE_IPS=$BE_IPS" >> $GITHUB_ENV | |
| echo "BE IPs: $BE_IPS" | |
| - name: Deploy to all BE instances | |
| run: | | |
| for IP in $BE_IPS; do | |
| echo "Deploying to $IP ..." | |
| # env 파일 전송 | |
| scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa .env \ | |
| ubuntu@$IP:/home/ubuntu/AWS_DOCKER/be | |
| # Docker 배포 (heredoc 제거) | |
| ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@$IP \ | |
| "set -e; cd /home/ubuntu/AWS_DOCKER/be && docker compose down && docker compose pull && docker compose up -d" | |
| done | |