Merge pull request #145 from hamlsy/refactor/142 #40
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: Deploy to EC2 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - master | |
| paths-ignore: | |
| - "**/*.md" | |
| - "README.md" | |
| - "docs/**" | |
| workflow_dispatch: | |
| env: | |
| AWS_REGION: ap-northeast-2 | |
| S3_BUCKET_NAME: kospot-bucket-deploy | |
| CODE_DEPLOY_APPLICATION_NAME: kospot-deploy | |
| CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: kospot | |
| DOCKER_IMAGE_NAME: kospot-backend | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. 코드 체크아웃 | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| submodules: true | |
| token: ${{ secrets.SUBMODULE_TOKEN }} | |
| # 2. JDK 17 설정 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| cache: gradle | |
| # 3. Gradle 권한 부여 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| # 4. 테스트 실행 | |
| # - name: Run tests | |
| # run: ./gradlew test | |
| # 5. 빌드 (테스트는 이미 완료했으므로 스킵) | |
| - name: Build with Gradle | |
| run: ./gradlew build -x test | |
| # 6. Docker 빌드 설정 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| # 7. Docker 이미지 빌드 | |
| - name: Build Docker image | |
| run: | | |
| docker build --no-cache -t ${{ env.DOCKER_IMAGE_NAME }}:latest . | |
| docker save -o ${{ env.DOCKER_IMAGE_NAME }}.tar ${{ env.DOCKER_IMAGE_NAME }}:latest | |
| # 8. AWS 자격 증명 설정 | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| # 9. 배포 패키지 생성 | |
| - name: Create deployment package | |
| run: | | |
| mkdir -p deploy | |
| cp ${{ env.DOCKER_IMAGE_NAME }}.tar deploy/ | |
| cp docker-compose.yml deploy/ | |
| cp appspec.yml deploy/ | |
| cp -r scripts deploy/ | |
| cd deploy && zip -r deploy.zip . | |
| # 10. S3에 배포 패키지 업로드 | |
| - name: Upload to S3 | |
| run: | | |
| aws s3 cp deploy/deploy.zip s3://${{ env.S3_BUCKET_NAME }}/deploy-${{ github.sha }}.zip | |
| # 11. CodeDeploy를 통한 배포 | |
| - name: Deploy to EC2 with CodeDeploy | |
| run: | | |
| aws deploy create-deployment \ | |
| --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ | |
| --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \ | |
| --deployment-config-name CodeDeployDefault.AllAtOnce \ | |
| --s3-location bucket=${{ env.S3_BUCKET_NAME }},bundleType=zip,key=deploy-${{ github.sha }}.zip \ | |
| --description "Deployment from GitHub Actions - commit: ${{ github.sha }}" | |
| # 12. Slack 알림 (선택사항) | |
| - name: Notify deployment status | |
| if: always() | |
| run: | | |
| if [ "${{ job.status }}" == "success" ]; then | |
| echo "✅ Deployment successful!" | |
| else | |
| echo "❌ Deployment failed!" | |
| fi | |