|
| 1 | +name: Deploy to Sandbox |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + deploy_module: |
| 7 | + description: '배포할 모듈을 선택하세요' |
| 8 | + required: true |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - api |
| 12 | + |
| 13 | +env: |
| 14 | + AWS_REGION: ap-northeast-2 |
| 15 | + PHASE: sandbox |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + |
| 20 | +jobs: |
| 21 | + test: |
| 22 | + name: Test & Lint |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Set up JDK 17 |
| 30 | + uses: actions/setup-java@v4 |
| 31 | + with: |
| 32 | + java-version: '17' |
| 33 | + distribution: 'temurin' |
| 34 | + cache: 'gradle' |
| 35 | + |
| 36 | + - name: Grant execute permission for gradlew |
| 37 | + run: chmod +x gradlew |
| 38 | + |
| 39 | + - name: Check ktlint format |
| 40 | + run: ./gradlew ktlintCheck |
| 41 | + |
| 42 | + - name: Run tests |
| 43 | + run: ./gradlew test |
| 44 | + |
| 45 | + deploy: |
| 46 | + name: Deploy ${{ github.event.inputs.deploy_module }} |
| 47 | + runs-on: ubuntu-latest |
| 48 | + needs: test |
| 49 | + if: ${{ !failure() }} |
| 50 | + environment: sandbox |
| 51 | + |
| 52 | + env: |
| 53 | + ECS_CLUSTER: sandbox-ecs-cluster |
| 54 | + |
| 55 | + steps: |
| 56 | + - name: Set deployment config |
| 57 | + run: | |
| 58 | + case "${{ github.event.inputs.deploy_module }}" in |
| 59 | + api) |
| 60 | + echo "ECR_REPOSITORY=sandbox-app" >> $GITHUB_ENV |
| 61 | + echo "TASK_DEFINITION=sandbox-app" >> $GITHUB_ENV |
| 62 | + echo "ECS_SERVICE=sandbox-app-svc" >> $GITHUB_ENV |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "❌ Unknown deploy module: ${{ github.event.inputs.deploy_module }}" |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | + esac |
| 69 | +
|
| 70 | + - name: Checkout |
| 71 | + uses: actions/checkout@v4 |
| 72 | + |
| 73 | + - name: Set up Docker Buildx |
| 74 | + uses: docker/setup-buildx-action@v3 |
| 75 | + |
| 76 | + - name: Configure AWS credentials |
| 77 | + uses: aws-actions/configure-aws-credentials@v4 |
| 78 | + with: |
| 79 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 80 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 81 | + aws-region: ${{ env.AWS_REGION }} |
| 82 | + |
| 83 | + - name: Login to Amazon ECR |
| 84 | + id: login-ecr |
| 85 | + uses: aws-actions/amazon-ecr-login@v2 |
| 86 | + |
| 87 | + - name: Build, tag, and push image to Amazon ECR |
| 88 | + id: build-image |
| 89 | + env: |
| 90 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 91 | + IMAGE_TAG: ${{ github.sha }} |
| 92 | + uses: docker/build-push-action@v5 |
| 93 | + with: |
| 94 | + context: . |
| 95 | + file: ./app/api/Dockerfile |
| 96 | + push: true |
| 97 | + build-args: | |
| 98 | + PHASE=${{ env.PHASE }} |
| 99 | + tags: | |
| 100 | + ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }} |
| 101 | + ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest |
| 102 | + cache-from: type=gha |
| 103 | + cache-to: type=gha,mode=max |
| 104 | + |
| 105 | + - name: Set image output |
| 106 | + id: set-image |
| 107 | + run: | |
| 108 | + echo "image=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT |
| 109 | +
|
| 110 | + - name: Download task definition |
| 111 | + run: | |
| 112 | + aws ecs describe-task-definition \ |
| 113 | + --task-definition $TASK_DEFINITION \ |
| 114 | + --query taskDefinition > task-def.json |
| 115 | +
|
| 116 | + - name: Update task definition |
| 117 | + run: | |
| 118 | + jq --arg IMAGE "${{ steps.set-image.outputs.image }}" \ |
| 119 | + '.containerDefinitions[0].image = $IMAGE' \ |
| 120 | + task-def.json > new-task-def.json |
| 121 | +
|
| 122 | + # 불필요한 필드 제거 |
| 123 | + jq 'del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy)' \ |
| 124 | + new-task-def.json > final-task-def.json |
| 125 | +
|
| 126 | + - name: Register task definition |
| 127 | + id: register |
| 128 | + run: | |
| 129 | + TASK_DEF_ARN=$(aws ecs register-task-definition \ |
| 130 | + --cli-input-json file://final-task-def.json \ |
| 131 | + --query 'taskDefinition.taskDefinitionArn' \ |
| 132 | + --output text) |
| 133 | + echo "task-def-arn=$TASK_DEF_ARN" >> $GITHUB_OUTPUT |
| 134 | +
|
| 135 | + - name: Update ECS service |
| 136 | + run: | |
| 137 | + aws ecs update-service \ |
| 138 | + --cluster $ECS_CLUSTER \ |
| 139 | + --service $ECS_SERVICE \ |
| 140 | + --task-definition ${{ steps.register.outputs.task-def-arn }} |
| 141 | + echo "✅ API deployed successfully" |
| 142 | +
|
| 143 | + - name: Send Discord Notification (Success) |
| 144 | + if: success() |
| 145 | + env: |
| 146 | + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 147 | + run: | |
| 148 | + if [ -n "$DISCORD_WEBHOOK_URL" ]; then |
| 149 | + THREAD_NAME="[sandbox] ${{ github.event.inputs.deploy_module }} 배포 성공" |
| 150 | + MESSAGE="- Actor: ${{ github.actor }}\n- Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 151 | + curl -X POST "$DISCORD_WEBHOOK_URL" \ |
| 152 | + -H "Content-Type: application/json" \ |
| 153 | + -d "{\"content\": \"$MESSAGE\", \"thread_name\": \"$THREAD_NAME\"}" |
| 154 | + fi |
| 155 | +
|
| 156 | + # ❌ Plan 실패 → 스레드 이름은 동일, 메시지만 실패 표시 |
| 157 | + - name: Send Discord Notification (Failure) |
| 158 | + if: failure() |
| 159 | + env: |
| 160 | + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 161 | + run: | |
| 162 | + if [ -n "$DISCORD_WEBHOOK_URL" ]; then |
| 163 | + THREAD_NAME="❌ [sandbox] ${{ github.event.inputs.deploy_module }} 배포 실패" |
| 164 | + MESSAGE="- Actor: ${{ github.actor }}\n- Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 165 | + curl -X POST "$DISCORD_WEBHOOK_URL" \ |
| 166 | + -H "Content-Type: application/json" \ |
| 167 | + -d "{\"content\": \"$MESSAGE\", \"thread_name\": \"$THREAD_NAME\"}" |
| 168 | + fi |
0 commit comments