feat : test 4 #44
Workflow file for this run
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.5) | ||
| env: | ||
| IMAGE_TAG: 1.0.0 | ||
| on: | ||
| push: | ||
| branches: | ||
| - infra/ci-cd | ||
| # pull_request: | ||
| # branches: | ||
| # - main | ||
| # types: [ closed ] | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: 1. checkout repo | ||
| uses: actions/checkout@v2 # ๊นํ๋ธ ์ก์ ์ด ์ฝ๋๊ฐ์ ธ์ด | ||
| - name: 2. run test | ||
| run: ./gradlew test | ||
| - name: 3. set up JDK 21 | ||
| uses: actions/setup-java@v2 | ||
| with: | ||
| java-version: 21 | ||
| distribution: 'temurin' | ||
| build-and-push: | ||
| needs: test | ||
| 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.DOCKER_USERNAME }}/not-me-be:1.0.0 \ | ||
| --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 \ | ||
| --instance-ids $BE_INSTANCE_IDS \ | ||
| --query "Reservations[].Instances[].PublicIpAddress" \ | ||
| --output text) | ||
| echo "BE_IPS=$BE_IPS" >> $GITHUB_ENV | ||
| echo "Healthy BE IPs: $BE_IPS" | ||
| # 7. ๋ชจ๋ Healthy ์๋ฒ์ env ์ ์ก + ๋ฐฐํฌ | ||
| - 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 ๋ฐฐํฌ | ||
| ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@$IP <<'EOF' | ||
| set -e | ||
| cd /home/ubuntu/AWS_DOCKER/be | ||
| docker compose down | ||
| docker compose pull | ||
| docker compose up -d | ||
| EOF | ||
| done | ||