-
Notifications
You must be signed in to change notification settings - Fork 3.7k
257 lines (228 loc) · 10.5 KB
/
deploy.yml
File metadata and controls
257 lines (228 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
name: Terraform Deploy
on:
push:
branches: [ master ]
paths:
- 'terraform/**'
pull_request:
branches: [ master ]
paths:
- 'terraform/**'
workflow_dispatch:
inputs:
image_tag:
description: 'Docker image tag to deploy'
required: false
default: 'latest'
type: string
env:
AWS_REGION: us-east-1
TF_VAR_image_tag: us-east-1
TF_VAR_image_repository: ghcr.io/${{ github.repository }}
REPO_FULL_NAME: ${{ github.repository }}
jobs:
terraform-plan:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract repository name
id: extract_short_name_repo
run: |
REPO_NAME="${REPO_FULL_NAME##*/}"
echo "Repository Short name: $REPO_NAME"
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_OUTPUT
- name: Set default image tag for PR
id: pr-image-tag
run: |
# Use defaults if inputs are empty or not provided
IMAGE_TAG="${{ inputs.image_tag }}"
# Set to 'latest' if empty, null, or not provided
if [ -z "$IMAGE_TAG" ] || [ "$IMAGE_TAG" = "null" ]; then
IMAGE_TAG="latest"
fi
echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
echo "Using image tag: $IMAGE_TAG"
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ~1.5
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::908027381725:role/${{ steps.extract_short_name_repo.outputs.REPO_NAME }}-github-actions-role
aws-region: ${{ env.AWS_REGION }}
- name: Configure Git for private modules
run: |
git config --global url."https://${{ secrets.GH_TOKEN }}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
working-directory: ./terraform
run: terraform init
- name: Terraform Validate
working-directory: ./terraform
run: terraform validate
- name: Terraform Plan
id: plan
working-directory: ./terraform
run: |
terraform plan -input=false -lock=false -no-color -out=tfplan
terraform show -no-color tfplan > plan_output.txt
env:
TF_VAR_image_tag: ${{ steps.pr-image-tag.outputs.image_tag }}
- name: Update Pull Request
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const planOutput = fs.readFileSync('./terraform/plan_output.txt', 'utf8');
const output = `## 🏗️ Terraform Plan for ${{ steps.extract_short_name_repo.outputs.REPO_SHORT_NAME }}
<details>
<summary>Click to expand plan</summary>
\`\`\`hcl
${planOutput}
\`\`\`
</details>
**Plan Status:** ${{ steps.plan.outcome }}
**Service:** ${{ steps.extract_short_name_repo.outputs.REPO_SHORT_NAME }}.ggai:8000
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
terraform-apply:
if: github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract repository name
id: extract_short_name_repo
run: |
REPO_NAME="${REPO_FULL_NAME##*/}"
echo "Repository Short name: $REPO_NAME"
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_OUTPUT
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::908027381725:role/${{ steps.extract_short_name_repo.outputs.REPO_NAME }}-github-actions-role
aws-region: ${{ env.AWS_REGION }}
- name: Determine image tag
id: get-image-tag
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.image_tag }}" ]; then
# Priority 1: Manual input from workflow_dispatch (triggered by build workflow or manual)
IMAGE_TAG="${{ inputs.image_tag }}"
echo "source=manual" >> $GITHUB_OUTPUT
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "Using provided image tag: ${IMAGE_TAG}"
else
# Priority 2: Direct terraform push (no application changes)
# Check if commit contains application changes (non-terraform files)
APP_CHANGES=$(git diff --name-only HEAD~1 HEAD | grep -v "^terraform/" | grep -v "^\.github/workflows/deploy\.yml" | wc -l)
if [ "$APP_CHANGES" -gt 0 ]; then
# Application changes detected but deploy workflow was triggered directly
echo "⚠️ Application changes detected but no image tag provided!"
echo "This deployment may fail because build workflow should have run first."
echo "Attempting to get current image tag from ECS task definition..."
fi
# Get current image tag FE and BE from ECS task definition
CURRENT_IMAGE=$(aws ecs describe-task-definition \
--task-definition ${{ steps.extract_short_name_repo.outputs.REPO_NAME }}-prod-task-def \
--query 'taskDefinition.containerDefinitions[0].image' \
--output text 2>/dev/null || echo "")
if [ -n "$CURRENT_IMAGE" ] && [[ "$CURRENT_IMAGE" != "None" ]]; then
# Extract tag from image URL (format: 908027381725.dkr.ecr.us-east-1.amazonaws.com/${{ steps.extract_short_name_repo.outputs.REPO_NAME }}:TAG)
IMAGE_TAG=$(echo "$CURRENT_IMAGE" | cut -d':' -f2)
echo "source=current_ecs" >> $GITHUB_OUTPUT
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "Using current ECS image tag: ${IMAGE_TAG} (extracted from task definition)"
else
# Fallback to latest if we can't get current task definition
echo "Could not retrieve current task definition, falling back to 'latest'"
IMAGE_TAG="latest"
echo "source=fallback" >> $GITHUB_OUTPUT
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "Using fallback image tag: ${IMAGE_TAG}"
fi
fi
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ~1.5
- name: Configure Git for private modules
run: |
git config --global url."https://${{ secrets.GH_TOKEN }}@github.com/".insteadOf "https://github.com/"
- name: Terraform Init
working-directory: ./terraform
run: terraform init
- name: Terraform Validate
working-directory: ./terraform
run: terraform validate
- name: Terraform Plan
working-directory: ./terraform
run: terraform plan -no-color
env:
TF_VAR_image_tag: ${{ steps.get-image-tag.outputs.image_tag }}
- name: Terraform Apply
working-directory: ./terraform
run: terraform apply -auto-approve
env:
TF_VAR_image_tag: ${{ steps.get-image-tag.outputs.image_tag }}
- name: Get deployment outputs
id: terraform-output
working-directory: ./terraform
run: |
SERVICE_URL=$(terraform output -raw service_discovery_endpoint 2>/dev/null || echo '${{ steps.extract_short_name_repo.outputs.REPO_NAME }}.ggai')
ECR_REPO=$(terraform output -raw ecr_repository_url 2>/dev/null || echo 'N/A')
delimiter=$(openssl rand -hex 8)
echo "service_url<<${delimiter}" >> $GITHUB_OUTPUT
echo "${SERVICE_URL}" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
echo "ecr_repository<<${delimiter}" >> $GITHUB_OUTPUT
echo "${ECR_REPO}" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
- name: Create deployment summary
run: |
echo "## 🚀 ${{ steps.extract_short_name_repo.outputs.REPO_NAME }} Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "| Component | Status |" >> $GITHUB_STEP_SUMMARY
echo "|-----------|--------|" >> $GITHUB_STEP_SUMMARY
echo "| **Terraform Init** | ✅ Success |" >> $GITHUB_STEP_SUMMARY
echo "| **Terraform Validate** | ✅ Success |" >> $GITHUB_STEP_SUMMARY
echo "| **Terraform Apply** | ✅ Success |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Service Information" >> $GITHUB_STEP_SUMMARY
echo "- **Service URL**: ${{ steps.terraform-output.outputs.service_url }}" >> $GITHUB_STEP_SUMMARY
echo "- **Container Port**: 3535" >> $GITHUB_STEP_SUMMARY
echo "- **ECR Repository**: ${{ steps.terraform-output.outputs.ecr_repository }}" >> $GITHUB_STEP_SUMMARY
echo "- **Image Tag**: \`${{ steps.get-image-tag.outputs.image_tag }}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Tag Source**: ${{ steps.get-image-tag.outputs.source }}" >> $GITHUB_STEP_SUMMARY
echo "- **Trigger**: ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
echo "- **Deployment Time**: $(date)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Add deployment trigger notice
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
if [ "${{ steps.get-image-tag.outputs.source }}" = "manual" ]; then
echo "### 🎯 Manual Deployment" >> $GITHUB_STEP_SUMMARY
echo "This deployment was triggered manually with image tag: \`${{ steps.get-image-tag.outputs.image_tag }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
else
echo "### 🤖 Build-Triggered Deployment" >> $GITHUB_STEP_SUMMARY
echo "This deployment was triggered automatically by the build workflow with image tag: \`${{ steps.get-image-tag.outputs.image_tag }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
fi
fi
echo "### Next Steps" >> $GITHUB_STEP_SUMMARY
echo "- Service will be available at \`${{ steps.extract_short_name_repo.outputs.REPO_NAME }}.ggai:8000\`" >> $GITHUB_STEP_SUMMARY
echo "- Check ECS console for service health" >> $GITHUB_STEP_SUMMARY
echo "- Monitor CloudWatch logs: \`/ecs/${{ steps.extract_short_name_repo.outputs.REPO_NAME }}\`" >> $GITHUB_STEP_SUMMARY