-
Notifications
You must be signed in to change notification settings - Fork 0
215 lines (179 loc) · 7.98 KB
/
deploy-asg.yml
File metadata and controls
215 lines (179 loc) · 7.98 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
name: Deploy to Auto Scaling Group
on:
push:
branches:
- feature/configuracao
workflow_dispatch:
permissions:
id-token: write # Necessário para OIDC
contents: read
env:
AWS_REGION: us-east-1
S3_BUCKET: finance-tracker-releases # Criar este bucket
APP_NAME: finance-tracker
ASG_NAME: finance-tracker-asg
LAUNCH_TEMPLATE_NAME: finance-tracker-launch-template
jobs:
deploy:
name: Build and Deploy to ASG
runs-on: ubuntu-latest
steps:
# 1. Checkout do código
- name: Checkout code
uses: actions/checkout@v4
# 2. Configurar AWS credentials via OIDC
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
# 3. Setup Go
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.25.3'
# 4. Executar testes
- name: Run tests
run: |
go mod download
go test ./... -v
# 5. Build do binário para Linux AMD64
- name: Build binary
run: |
GOOS=linux GOARCH=amd64 go build -o finance-tracker ./cmd/api
# 6. Gerar versão única (hash do commit)
- name: Generate version
id: version
run: |
VERSION="${GITHUB_SHA:0:7}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "📦 Version: $VERSION"
# 7. Upload binário para S3
- name: Upload binary to S3
run: |
aws s3 cp finance-tracker \
s3://${{ env.S3_BUCKET }}/releases/${{ steps.version.outputs.version }}/finance-tracker \
--region ${{ env.AWS_REGION }}
echo "✅ Binário enviado para S3"
# 8. Preparar User Data Script
- name: Prepare User Data
run: |
# Substituir placeholders no user-data.sh
sed -i "s|BUCKET_NAME|${{ env.S3_BUCKET }}|g" infra/user-data.sh
sed -i "s|APP_VERSION|${{ steps.version.outputs.version }}|g" infra/user-data.sh
sed -i "s|DATABASE_URL_PLACEHOLDER|${{ secrets.DATABASE_URL }}|g" infra/user-data.sh
# Codificar em base64
USER_DATA=$(cat infra/user-data.sh | base64 -w 0)
echo "USER_DATA=$USER_DATA" >> $GITHUB_ENV
# 9. Criar nova versão do Launch Template
- name: Create Launch Template version
id: lt_version
run: |
NEW_VERSION=$(aws ec2 create-launch-template-version \
--launch-template-name ${{ env.LAUNCH_TEMPLATE_NAME }} \
--source-version '$Latest' \
--launch-template-data "{
\"UserData\": \"$USER_DATA\",
\"TagSpecifications\": [{
\"ResourceType\": \"instance\",
\"Tags\": [
{\"Key\": \"Name\", \"Value\": \"finance-tracker-${{ steps.version.outputs.version }}\"},
{\"Key\": \"Version\", \"Value\": \"${{ steps.version.outputs.version }}\"},
{\"Key\": \"DeployedBy\", \"Value\": \"GitHub-Actions\"},
{\"Key\": \"CommitSHA\", \"Value\": \"${{ github.sha }}\"}
]
}]
}" \
--query 'LaunchTemplateVersion.VersionNumber' \
--output text)
echo "lt_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "🚀 Launch Template versão $NEW_VERSION criada"
# 10. Atualizar Auto Scaling Group
- name: Update Auto Scaling Group
run: |
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name ${{ env.ASG_NAME }} \
--launch-template "LaunchTemplateName=${{ env.LAUNCH_TEMPLATE_NAME }},Version=${{ steps.lt_version.outputs.lt_version }}"
echo "✅ Auto Scaling Group atualizado"
# 11. Iniciar Instance Refresh (Rolling Update)
- name: Start Instance Refresh
id: refresh
run: |
REFRESH_ID=$(aws autoscaling start-instance-refresh \
--auto-scaling-group-name ${{ env.ASG_NAME }} \
--preferences '{
"MinHealthyPercentage": 50,
"InstanceWarmup": 60,
"CheckpointPercentages": [50, 100],
"CheckpointDelay": 30
}' \
--query 'InstanceRefreshId' \
--output text)
echo "refresh_id=$REFRESH_ID" >> $GITHUB_OUTPUT
echo "🔄 Instance Refresh iniciado: $REFRESH_ID"
# 12. Aguardar conclusão do Instance Refresh
- name: Wait for Instance Refresh
run: |
echo "⏳ Aguardando Instance Refresh concluir..."
while true; do
STATUS=$(aws autoscaling describe-instance-refreshes \
--auto-scaling-group-name ${{ env.ASG_NAME }} \
--instance-refresh-ids ${{ steps.refresh.outputs.refresh_id }} \
--query 'InstanceRefreshes[0].Status' \
--output text)
echo "Status atual: $STATUS"
if [ "$STATUS" == "Successful" ]; then
echo "✅ Instance Refresh concluído com sucesso!"
break
elif [ "$STATUS" == "Failed" ] || [ "$STATUS" == "Cancelled" ]; then
echo "❌ Instance Refresh falhou!"
exit 1
fi
sleep 30
done
# 13. Verificar saúde do Target Group
- name: Check Target Group Health
run: |
TARGET_GROUP_ARN=$(aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names ${{ env.ASG_NAME }} \
--query 'AutoScalingGroups[0].TargetGroupARNs[0]' \
--output text)
echo "🏥 Verificando saúde do Target Group..."
HEALTHY_COUNT=$(aws elbv2 describe-target-health \
--target-group-arn $TARGET_GROUP_ARN \
--query "length(TargetHealthDescriptions[?TargetHealth.State=='healthy'])" \
--output text)
echo "✅ Instâncias saudáveis: $HEALTHY_COUNT"
if [ "$HEALTHY_COUNT" -lt 1 ]; then
echo "❌ Nenhuma instância saudável encontrada!"
exit 1
fi
# 14. Notificação de sucesso
- name: Deployment Summary
if: success()
run: |
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ DEPLOY CONCLUÍDO COM SUCESSO!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 Versão: ${{ steps.version.outputs.version }}"
echo "🚀 Launch Template: v${{ steps.lt_version.outputs.lt_version }}"
echo "🔄 Instance Refresh: ${{ steps.refresh.outputs.refresh_id }}"
echo "⏰ Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC")"
echo "👤 Deployed by: ${{ github.actor }}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# 15. Rollback em caso de falha
- name: Rollback on failure
if: failure()
run: |
echo "❌ Deploy falhou! Iniciando rollback..."
# Cancelar instance refresh
aws autoscaling cancel-instance-refresh \
--auto-scaling-group-name ${{ env.ASG_NAME }} || true
# Voltar para versão anterior do Launch Template
PREVIOUS_VERSION=$(( ${{ steps.lt_version.outputs.lt_version }} - 1 ))
if [ "$PREVIOUS_VERSION" -gt 0 ]; then
aws ec2 modify-launch-template \
--launch-template-name ${{ env.LAUNCH_TEMPLATE_NAME }} \
--default-version $PREVIOUS_VERSION
echo "🔙 Rollback para Launch Template v$PREVIOUS_VERSION"
fi