feat | sprint2 | FRB-137 | EC2 배포 파이프라인 추가 | 조수빈 #21
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: Build Image to ECR with Certs | |
| on: | |
| push: | |
| branches: | |
| - chore/build-and-deploy | |
| jobs: | |
| build-and-push-image: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'corretto' | |
| - name: Cache Gradle packages | |
| uses: actions/cache@v3 | |
| with: | |
| path: ~/.gradle/caches | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| - name: Test | |
| run: ./gradlew test | |
| - name: Create application.properties | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_PROPERTIES }}" > src/main/resources/application.properties | |
| shell: bash | |
| - name: Create certificate files | |
| run: | | |
| mkdir -p src/main/resources/certs | |
| echo "${{ secrets.AWS_IOT_ROOT }}" > src/main/resources/certs/root.pem | |
| echo "${{ secrets.AWS_IOT_PRIVATE_KEY }}" > src/main/resources/certs/private.pem.key | |
| echo "${{ secrets.AWS_IOT_CERT }}" > src/main/resources/certs/certificate.pem.crt | |
| shell: bash | |
| - 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: ap-northeast-2 | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| - name: Build, tag, and push image to ECR | |
| env: | |
| ECR_REGISTRY: ${{ secrets.AWS_ECR_REGISTRY }} | |
| ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPOSITORY }} | |
| IMAGE_TAG: flink-latest | |
| run: | | |
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| - name: Clean up sensitive files from runner | |
| if: always() | |
| run: | | |
| echo "Cleaning up sensitive files from GitHub Runner..." | |
| rm -f src/main/resources/application.properties | |
| rm -rf resources/cert | |
| echo "Cleanup complete." | |
| shell: bash | |
| deploy-and-push-image: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push-image | |
| steps: | |
| - 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: ap-northeast-2 | |
| - name: Login to Amazon ECR | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| - name: Deploy to EC2 via SSH | |
| uses: appleboy/ssh-action@v1 | |
| with: | |
| host: ${{ secrets.AWS_EC2_HOST }} | |
| username: ${{ secrets.AWS_EC2_USER }} | |
| key: ${{ secrets.AWS_EC2_SSH_KEY }} | |
| script: | | |
| # ECR 로그인 | |
| aws ecr get-login-password --region ap-northeast-2 | \ | |
| docker login --username AWS --password-stdin 853660505909.dkr.ecr.ap-northeast-2.amazonaws.com | |
| # 기존 컨테이너 정리 | |
| cd datastream | |
| docker-compose -f docker-compose-source.yml down -v --rmi all | |
| # 새 이미지 풀 & 실행 | |
| docker-compose -f docker-compose-source.yml pull | |
| docker-compose -f docker-compose-source.yml up -d |