forked from Ndifreke000/stellar-insights
-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (145 loc) · 6.1 KB
/
deploy.yml
File metadata and controls
169 lines (145 loc) · 6.1 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
name: Deploy Backend (Blue-Green)
on:
push:
branches: [main]
paths:
- 'backend/**'
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
default: 'production'
type: choice
options:
- staging
- production
permissions:
id-token: write
contents: read
env:
AWS_REGION: us-east-1
ECR_REPOSITORY: stellar-insights-backend
jobs:
deploy:
name: Build & Deploy
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment || 'production' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set environment
id: env
run: |
ENV="${{ github.event.inputs.environment || 'production' }}"
echo "environment=${ENV}" >> "$GITHUB_OUTPUT"
echo "cluster=stellar-insights-${ENV}" >> "$GITHUB_OUTPUT"
echo "service=stellar-insights-service" >> "$GITHUB_OUTPUT"
echo "task_family=stellar-insights-${ENV}" >> "$GITHUB_OUTPUT"
echo "container_name=stellar-insights" >> "$GITHUB_OUTPUT"
echo "codedeploy_app=stellar-insights-${ENV}" >> "$GITHUB_OUTPUT"
echo "codedeploy_group=stellar-insights-${ENV}-dg" >> "$GITHUB_OUTPUT"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/terraform-executor
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: ecr-login
uses: aws-actions/amazon-ecr-login@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push image
uses: docker/build-push-action@v6
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: |
${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Get current task definition
id: current-task-def
run: |
aws ecs describe-task-definition \
--task-definition "${{ steps.env.outputs.task_family }}" \
--query 'taskDefinition' \
--output json > current-task-def.json
- name: Update task definition with new image
id: new-task-def
run: |
IMAGE="${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}"
# Update the image in the container definition and strip fields that can't be re-registered
jq --arg IMAGE "$IMAGE" --arg CONTAINER "${{ steps.env.outputs.container_name }}" \
'.containerDefinitions |= map(if .name == $CONTAINER then .image = $IMAGE else . end) |
del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy)' \
current-task-def.json > new-task-def.json
# Register new task definition
NEW_ARN=$(aws ecs register-task-definition \
--cli-input-json file://new-task-def.json \
--query 'taskDefinition.taskDefinitionArn' \
--output text)
echo "task_definition_arn=${NEW_ARN}" >> "$GITHUB_OUTPUT"
echo "Registered new task definition: ${NEW_ARN}"
- name: Create CodeDeploy deployment
id: deploy
run: |
APPSPEC=$(cat <<'APPSPEC_EOF'
{
"version": 0.0,
"Resources": [{
"TargetService": {
"Type": "AWS::ECS::Service",
"Properties": {
"TaskDefinition": "TASK_DEF_PLACEHOLDER",
"LoadBalancerInfo": {
"ContainerName": "CONTAINER_PLACEHOLDER",
"ContainerPort": 8080
}
}
}
}]
}
APPSPEC_EOF
)
APPSPEC=$(echo "$APPSPEC" | jq -c \
--arg TD "${{ steps.new-task-def.outputs.task_definition_arn }}" \
--arg CN "${{ steps.env.outputs.container_name }}" \
'.Resources[0].TargetService.Properties.TaskDefinition = $TD |
.Resources[0].TargetService.Properties.LoadBalancerInfo.ContainerName = $CN')
DEPLOYMENT_ID=$(aws deploy create-deployment \
--application-name "${{ steps.env.outputs.codedeploy_app }}" \
--deployment-group-name "${{ steps.env.outputs.codedeploy_group }}" \
--revision "revisionType=AppSpecContent,appSpecContent={content='${APPSPEC}'}" \
--description "SHA: ${{ github.sha }} | Actor: ${{ github.actor }}" \
--query 'deploymentId' \
--output text)
echo "deployment_id=${DEPLOYMENT_ID}" >> "$GITHUB_OUTPUT"
echo "Created deployment: ${DEPLOYMENT_ID}"
- name: Wait for deployment
run: |
echo "Waiting for deployment ${{ steps.deploy.outputs.deployment_id }}..."
aws deploy wait deployment-successful \
--deployment-id "${{ steps.deploy.outputs.deployment_id }}"
echo "Deployment completed successfully!"
- name: Deployment summary
if: always()
run: |
DEPLOY_INFO=$(aws deploy get-deployment \
--deployment-id "${{ steps.deploy.outputs.deployment_id }}" \
--query 'deploymentInfo' \
--output json 2>/dev/null || echo '{}')
STATUS=$(echo "$DEPLOY_INFO" | jq -r '.status // "UNKNOWN"')
cat >> "$GITHUB_STEP_SUMMARY" <<EOF
## Deployment Summary
| Field | Value |
|-------|-------|
| **Environment** | \`${{ steps.env.outputs.environment }}\` |
| **Deployment ID** | \`${{ steps.deploy.outputs.deployment_id }}\` |
| **Status** | \`${STATUS}\` |
| **Image Tag** | \`${{ github.sha }}\` |
| **Task Definition** | \`${{ steps.new-task-def.outputs.task_definition_arn }}\` |
| **Actor** | @${{ github.actor }} |
EOF