|
| 1 | +name: Deploy to production environment |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - prod |
| 6 | +env: |
| 7 | + AWS_REGION: ap-northeast-2 |
| 8 | + CONTAINER_NAME: gotchai-server |
| 9 | +jobs: |
| 10 | + deploy-image: |
| 11 | + name: Deploy image |
| 12 | + runs-on: ubuntu-latest |
| 13 | + environment: prod |
| 14 | + outputs: |
| 15 | + image: ${{ steps.set-image.outputs.image }} |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + - name: Set up JDK |
| 20 | + uses: actions/setup-java@v4 |
| 21 | + with: |
| 22 | + distribution: corretto |
| 23 | + java-version: 21 |
| 24 | + - name: Cache Gradle packages |
| 25 | + uses: actions/cache@v3 |
| 26 | + with: |
| 27 | + path: | |
| 28 | + ~/.gradle/caches |
| 29 | + ~/.gradle/wrapper |
| 30 | + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} |
| 31 | + restore-keys: | |
| 32 | + ${{ runner.os }}-gradle- |
| 33 | + - name: Configure AWS credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@v1 |
| 35 | + with: |
| 36 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 37 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 38 | + aws-region: ${{ env.AWS_REGION }} |
| 39 | + - name: Set image |
| 40 | + id: set-image |
| 41 | + run: echo "image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT |
| 42 | + - name: Build and deploy image to AWS ECR |
| 43 | + run: | |
| 44 | + ./gradlew :api:jib \ |
| 45 | + -Djib.to.image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }} \ |
| 46 | + -Djib.to.auth.username=AWS \ |
| 47 | + -Djib.to.auth.password=$(aws ecr get-login-password) |
| 48 | + deploy-container: |
| 49 | + name: Deploy container |
| 50 | + runs-on: ubuntu-latest |
| 51 | + needs: deploy-image |
| 52 | + environment: prod |
| 53 | + steps: |
| 54 | + - name: Checkout |
| 55 | + uses: actions/checkout@v4 |
| 56 | + - name: Send necessary files to EC2 |
| 57 | + |
| 58 | + with: |
| 59 | + host: ${{ secrets.EC2_HOST }} |
| 60 | + username: ${{ secrets.EC2_USER }} |
| 61 | + key: ${{ secrets.EC2_PRIVATE_KEY }} |
| 62 | + source: docker/docker-compose-prod.yml |
| 63 | + target: "~" |
| 64 | + strip_components: 1 |
| 65 | + - name: Build and deploy container to AWS EC2 |
| 66 | + |
| 67 | + with: |
| 68 | + host: ${{ secrets.EC2_HOST }} |
| 69 | + username: ${{ secrets.EC2_USER }} |
| 70 | + key: ${{ secrets.EC2_PRIVATE_KEY }} |
| 71 | + script: | |
| 72 | + aws secretsmanager get-secret-value --secret-id prod-env --region ap-northeast-2 --query SecretString --output text | jq -r '. | to_entries | map("\(.key)=\(.value)") | .[]' > .env |
| 73 | +
|
| 74 | + export IMAGE_URI=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }} |
| 75 | + docker-compose -f docker-compose-prod.yml --env-file .env up -d --build |
| 76 | + notify-discord: |
| 77 | + name: Notify Discord |
| 78 | + runs-on: ubuntu-latest |
| 79 | + needs: deploy-container |
| 80 | + steps: |
| 81 | + - name: Checkout |
| 82 | + uses: actions/checkout@v4 |
| 83 | + - name: Get commit |
| 84 | + id: get_commit |
| 85 | + run: echo "commit_message=$(git log -1 --pretty=%s)" >> "$GITHUB_OUTPUT" |
| 86 | + - name: Send Discord notification |
| 87 | + run: | |
| 88 | + curl -X POST -H "Content-Type: application/json" -d '{ |
| 89 | + "username": "GitHub Actions", |
| 90 | + "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png", |
| 91 | + "embeds": [ |
| 92 | + { |
| 93 | + "title": "**프로덕션 환경 배포 성공**", |
| 94 | + "description": "프로덕션 서버가 성공적으로 배포되었습니다.", |
| 95 | + "color": 3066993, |
| 96 | + "fields": [ |
| 97 | + { |
| 98 | + "name": "Repository", |
| 99 | + "value": "[${{ github.repository }}](https://github.com/${{ github.repository }})", |
| 100 | + "inline": true |
| 101 | + }, |
| 102 | + { |
| 103 | + "name": "Branch", |
| 104 | + "value": "${{ github.ref_name }}", |
| 105 | + "inline": true |
| 106 | + }, |
| 107 | + { |
| 108 | + "name": "Commit", |
| 109 | + "value": "[${{ steps.get_commit.outputs.commit_message }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})", |
| 110 | + "inline": true |
| 111 | + }, |
| 112 | + { |
| 113 | + "name": "Author", |
| 114 | + "value": "[${{ github.actor }}](${{ github.event.sender.html_url }})", |
| 115 | + "inline": true |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
| 119 | + ] |
| 120 | + }' ${{ secrets.DISCORD_WEBHOOK_URL }} |
0 commit comments