feat : ๊ฒ์๊ธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๊ธฐ๋ฅ ์ถ๊ฐ #47
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.3) | |
| env: | |
| IMAGE_TAG: 1.0.0 | |
| on: | |
| pull_request: | |
| branches: | |
| - main # ์ค์ ์ด์๋๋ ๋ธ๋์น develop -> main ์ผ๋ก ๋ณ๊ฒฝ | |
| 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. BE1 ์ธ์คํด์ค ํผ๋ธ๋ฆญ IP ๋ฐํ | |
| - name: Get BE1 Public IP | |
| run: | | |
| BE1_IP=$(aws ec2 describe-instances \ | |
| --filters "Name=tag:Name,Values=Our-Universe-BE1" \ | |
| "Name=instance-state-name,Values=running" \ | |
| --query "Reservations[].Instances[].PublicIpAddress" \ | |
| --output text \ | |
| ) | |
| echo "BE1_IP=$BE1_IP" >> $GITHUB_ENV | |
| echo "๋ถ๋ฌ์จ BE1 IP => $BE1_IP" | |
| # 6. B2 IP ๊ฐ์ ธ์ค๊ธฐ | |
| - name: Get BE2 Public IP | |
| run: | | |
| BE2_IP=$(aws ec2 describe-instances \ | |
| --filters "Name=tag:Name,Values=Our-Universe-BE2" \ | |
| "Name=instance-state-name,Values=running" \ | |
| --query "Reservations[].Instances[].PublicIpAddress" \ | |
| --output text \ | |
| ) | |
| echo "BE2_IP=$BE2_IP" >> $GITHUB_ENV | |
| echo "๋ถ๋ฌ์จ BE2 IP => $BE2_IP" | |
| # 7. B1์ .env ์ ์ก | |
| - name: Upload .env to BE1 | |
| run: | | |
| scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa .env \ | |
| ubuntu@$BE1_IP:/home/${{secrets.SERVER_USER}}/AWS_DOCKER/be | |
| # 8. B2์ .env ์ ์ก | |
| - name: Upload .env to BE2 | |
| run: | | |
| scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa .env \ | |
| ubuntu@$BE2_IP:/home/${{secrets.SERVER_USER}}/AWS_DOCKER/be | |
| # 9. BE1 ๋ฐฐํฌ | |
| - name: Deploy to BE1 | |
| run: | | |
| ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@$BE1_IP 'bash -s' <<'EOF' | |
| set -e | |
| cd /home/ubuntu/AWS_DOCKER/be | |
| docker compose down | |
| docker compose pull | |
| docker compose up -d | |
| EOF | |
| # 10. BE2 ๋ฐฐํฌ | |
| - name: Deploy to BE2 | |
| run: | | |
| ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@$BE2_IP 'bash -s' <<'EOF' | |
| set -e | |
| cd /home/ubuntu/AWS_DOCKER/be | |
| docker compose down | |
| docker compose pull | |
| docker compose up -d | |
| EOF | |