|
| 1 | +name: "Terraform V3 K8S" |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + paths: ["IaC/3-v3/aws/**"] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + action: |
| 10 | + description: "실행할 Terraform 작업" |
| 11 | + required: true |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - plan |
| 15 | + - apply |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: terraform-v3-k8s-dev |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +permissions: |
| 22 | + id-token: write |
| 23 | + contents: read |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | +env: |
| 27 | + TF_VERSION: "1.14.6" |
| 28 | + WORKING_DIR: "IaC/3-v3/aws/environments/k8s-dev" |
| 29 | + AWS_REGION: "ap-northeast-2" |
| 30 | + |
| 31 | +jobs: |
| 32 | + # ────────────────────────────────────────────── |
| 33 | + # Plan: PR 또는 수동 트리거 |
| 34 | + # ────────────────────────────────────────────── |
| 35 | + plan: |
| 36 | + name: "Terraform Plan" |
| 37 | + runs-on: ubuntu-latest |
| 38 | + outputs: |
| 39 | + plan_exitcode: ${{ steps.plan.outputs.exitcode }} |
| 40 | + steps: |
| 41 | + - uses: actions/checkout@v4 |
| 42 | + |
| 43 | + - uses: aws-actions/configure-aws-credentials@v4 |
| 44 | + with: |
| 45 | + role-to-assume: ${{ secrets.TF_AWS_ROLE_ARN }} |
| 46 | + aws-region: ${{ env.AWS_REGION }} |
| 47 | + |
| 48 | + - uses: hashicorp/setup-terraform@v3 |
| 49 | + with: |
| 50 | + terraform_version: ${{ env.TF_VERSION }} |
| 51 | + |
| 52 | + - name: Terraform fmt |
| 53 | + id: fmt |
| 54 | + run: terraform fmt -check -recursive -diff |
| 55 | + working-directory: IaC/3-v3/aws |
| 56 | + continue-on-error: true |
| 57 | + |
| 58 | + - name: Terraform Init |
| 59 | + id: init |
| 60 | + run: terraform init -backend-config=backend.hcl -backend-config="profile=" -input=false |
| 61 | + working-directory: ${{ env.WORKING_DIR }} |
| 62 | + |
| 63 | + - name: Terraform Validate |
| 64 | + id: validate |
| 65 | + run: terraform validate -no-color |
| 66 | + working-directory: ${{ env.WORKING_DIR }} |
| 67 | + |
| 68 | + - name: Terraform Plan |
| 69 | + id: plan |
| 70 | + run: | |
| 71 | + terraform plan \ |
| 72 | + -var="vpc_id=${{ secrets.K8S_DEV_VPC_ID }}" \ |
| 73 | + -var="ssl_certificate_arn=${{ secrets.K8S_DEV_SSL_CERT_ARN }}" \ |
| 74 | + -input=false -no-color -detailed-exitcode -out=tfplan \ |
| 75 | + 2>&1 | tee plan_output.txt |
| 76 | + working-directory: ${{ env.WORKING_DIR }} |
| 77 | + continue-on-error: true |
| 78 | + |
| 79 | + - name: Comment Plan on PR |
| 80 | + if: github.event_name == 'pull_request' |
| 81 | + uses: actions/github-script@v7 |
| 82 | + with: |
| 83 | + script: | |
| 84 | + const fs = require('fs'); |
| 85 | + const planPath = '${{ env.WORKING_DIR }}/plan_output.txt'; |
| 86 | + let plan = fs.existsSync(planPath) ? fs.readFileSync(planPath, 'utf8') : 'Plan output not available'; |
| 87 | + if (plan.length > 60000) plan = plan.substring(0, 60000) + '\n... (truncated)'; |
| 88 | +
|
| 89 | + const body = `### Terraform Plan Result |
| 90 | + | Step | Status | |
| 91 | + |------|--------| |
| 92 | + | Format | ${'${{ steps.fmt.outcome }}' === 'success' ? '✅' : '❌'} | |
| 93 | + | Init | ${'${{ steps.init.outcome }}' === 'success' ? '✅' : '❌'} | |
| 94 | + | Validate | ${'${{ steps.validate.outcome }}' === 'success' ? '✅' : '❌'} | |
| 95 | + | Plan | ${'${{ steps.plan.outcome }}' === 'success' ? '✅ (no changes)' : '⚠️ (has changes)'} | |
| 96 | +
|
| 97 | + <details><summary>Plan Output</summary> |
| 98 | +
|
| 99 | + \`\`\`terraform |
| 100 | + ${plan} |
| 101 | + \`\`\` |
| 102 | +
|
| 103 | + </details> |
| 104 | +
|
| 105 | + *Pushed by: @${{ github.actor }}*`; |
| 106 | +
|
| 107 | + const { data: comments } = await github.rest.issues.listComments({ |
| 108 | + owner: context.repo.owner, repo: context.repo.repo, |
| 109 | + issue_number: context.issue.number |
| 110 | + }); |
| 111 | + const bot = comments.find(c => c.user.type === 'Bot' && c.body.includes('Terraform Plan Result')); |
| 112 | + const params = { owner: context.repo.owner, repo: context.repo.repo, body }; |
| 113 | + if (bot) { await github.rest.issues.updateComment({ ...params, comment_id: bot.id }); } |
| 114 | + else { await github.rest.issues.createComment({ ...params, issue_number: context.issue.number }); } |
| 115 | +
|
| 116 | + - name: Fail on Error |
| 117 | + if: steps.plan.outputs.exitcode == '1' |
| 118 | + run: exit 1 |
| 119 | + |
| 120 | + - name: Upload Plan |
| 121 | + if: steps.plan.outputs.exitcode == '2' |
| 122 | + uses: actions/upload-artifact@v4 |
| 123 | + with: |
| 124 | + name: tfplan |
| 125 | + path: ${{ env.WORKING_DIR }}/tfplan |
| 126 | + retention-days: 5 |
| 127 | + |
| 128 | + # ────────────────────────────────────────────── |
| 129 | + # Apply: 수동 트리거만 (workflow_dispatch + action=apply) |
| 130 | + # ────────────────────────────────────────────── |
| 131 | + apply: |
| 132 | + name: "Terraform Apply" |
| 133 | + needs: plan |
| 134 | + if: github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'apply' && needs.plan.outputs.plan_exitcode == '2' |
| 135 | + runs-on: ubuntu-latest |
| 136 | + steps: |
| 137 | + - uses: actions/checkout@v4 |
| 138 | + |
| 139 | + - uses: aws-actions/configure-aws-credentials@v4 |
| 140 | + with: |
| 141 | + role-to-assume: ${{ secrets.TF_AWS_ROLE_ARN }} |
| 142 | + aws-region: ${{ env.AWS_REGION }} |
| 143 | + |
| 144 | + - uses: hashicorp/setup-terraform@v3 |
| 145 | + with: |
| 146 | + terraform_version: ${{ env.TF_VERSION }} |
| 147 | + terraform_wrapper: false |
| 148 | + |
| 149 | + - name: Terraform Init |
| 150 | + run: terraform init -backend-config=backend.hcl -backend-config="profile=" -input=false |
| 151 | + working-directory: ${{ env.WORKING_DIR }} |
| 152 | + |
| 153 | + - uses: actions/download-artifact@v4 |
| 154 | + with: |
| 155 | + name: tfplan |
| 156 | + path: ${{ env.WORKING_DIR }} |
| 157 | + |
| 158 | + - name: Terraform Apply |
| 159 | + run: terraform apply -input=false tfplan |
| 160 | + working-directory: ${{ env.WORKING_DIR }} |
0 commit comments