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: Build and Push Docker Image to ECR and Deploy on EC2 | |
| on: | |
| pull_request: | |
| branches: | |
| - main | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: ✔️ 리포지토리 가져오기 | |
| uses: actions/checkout@v4 | |
| - name: ✔️ JDK 17 설치 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: ✔️ Gradle 명령 실행 권한 설정 | |
| run: chmod +x gradlew | |
| - 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: ${{ secrets.AWS_DEFAULT_REGION }} | |
| - name: ✔️ Amazon ECR에 로그인 | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| - name: ✔️ Docker Image 빌드 및 ECR에 push | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| ./gradlew jib \ | |
| -Djib.to.image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| run-images-on-ec2: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: ✔️ EC2 접속 및 배포 스크립트 실행 | |
| uses: appleboy/ssh-action@v0.1.6 | |
| with: | |
| host: ${{ secrets.SSH_HOST_NAME }} | |
| username: ${{ secrets.SSH_USER_NAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| port: ${{ secrets.SSH_PORT }} | |
| script: | | |
| cd deploy-script | |
| sh ./deploy.sh |