-
Notifications
You must be signed in to change notification settings - Fork 2
242 lines (203 loc) · 9.08 KB
/
deploy-reusable.yml
File metadata and controls
242 lines (203 loc) · 9.08 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
name: Reusable Deploy
on:
workflow_call:
inputs:
environment:
required: true
type: string
description: 'Environment name (preprod or prod)'
aws-region:
required: false
type: string
default: 'eu-west-2'
secrets:
AWS_ROLE_ARN:
required: true
AWS_SECRETS_ARN:
required: true
workflow_dispatch:
inputs:
environment:
required: true
type: choice
description: 'Environment to deploy to'
options:
- preprod
- prod
aws-region:
required: false
type: string
default: 'eu-west-2'
description: 'AWS region'
permissions:
id-token: write
contents: read
jobs:
load-services:
runs-on: ubuntu-latest
outputs:
services: ${{ steps.load.outputs.services }}
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Load service mappings
id: load
run: |
services=$(jq -c '.services' infra/services.json)
echo "services=${services}" >> $GITHUB_OUTPUT
echo "Loaded services:"
echo "${services}" | jq '.'
deploy:
needs: load-services
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
strategy:
matrix:
service: ${{ fromJson(needs.load-services.outputs.services) }}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 # v6.0.0
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ inputs.aws-region }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@183a1442edf41672e66566b7fc560e297a290896 # v2.1.1
- name: Setup .NET
uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 # v5.2.0
with:
dotnet-version: 10.0.x
- name: Run tests
run: dotnet test --configuration Release
- name: Build and Publish Image
id: build-image
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: dms-${{ inputs.environment }}-apps
IMAGE_TAG: ${{ matrix.service.name }}-${{ github.sha }}
run: |
PROJECT_PATH="src/${{ matrix.service.project }}"
if [ ! -f "$PROJECT_PATH" ]; then
echo "Project not found: $PROJECT_PATH - Skipping"
echo "skip=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "Building and publishing: $PROJECT_PATH"
dotnet publish "$PROJECT_PATH" \
--configuration Release \
--target:PublishContainer \
--property:ContainerRegistry=$REGISTRY \
--property:ContainerRepository=$REPOSITORY \
--property:ContainerImageTag=$IMAGE_TAG
IMAGE="${REGISTRY}/${REPOSITORY}:${IMAGE_TAG}"
echo "IMAGE=${IMAGE}" >> $GITHUB_OUTPUT
echo "skip=false" >> $GITHUB_OUTPUT
echo "Published image: ${IMAGE}"
- name: Testing workflow steps
run: echo "The testing / deployment of the image have been skipped for this demo, but the workflow is correctly iterating over the services and would deploy the built image to ECS if those steps were uncommented."
- name: Fetch required secrets
id: fetch-secrets
run: |
SECRET_JSON=$(aws secretsmanager get-secret-value \
--secret-id ${{ secrets.AWS_SECRETS_ARN }} \
--query 'SecretString' \
--output text)
TASK_ROLE=$(echo "$SECRET_JSON" | jq -r '.Task_Role_ARN')
EXEC_ROLE=$(echo "$SECRET_JSON" | jq -r '.Execution_Role_ARN')
EFS_FS_ID=$(echo "$SECRET_JSON" | jq -r '.EFS_File_System_ID')
EFS_AP_ID=$(echo "$SECRET_JSON" | jq -r '.EFS_Access_Point_ID')
echo "task_role=$TASK_ROLE" >> $GITHUB_OUTPUT
echo "exec_role=$EXEC_ROLE" >> $GITHUB_OUTPUT
echo "efs-fs-id=$EFS_FS_ID" >> $GITHUB_OUTPUT
echo "efs-ap-id=$EFS_AP_ID" >> $GITHUB_OUTPUT
- name: Build secrets list for service
id: build-secrets
run: |
SECRETS_JSON='${{ toJson(matrix.service.secrets) }}'
SECRETS_STRING=""
declare -A SECRET_MAP=(
["API_Base_URL"]="API__BaseUrl"
["Authentication_ApiKey"]="Authentication__ApiKey"
["S3_BucketName"]="AWS__S3__BucketName"
["Client_ID"]="AzureAd__ClientId"
["API_Client_ID"]="AzureAd__ClientId"
["Client_Secret"]="AzureAd__ClientSecret"
["AuditDb"]="ConnectionStrings__AuditDb"
["ClusterDb"]="ConnectionStrings__ClusterDb"
["DeliusRunningPictureDb"]="ConnectionStrings__DeliusRunningPictureDb"
["DeliusStagingDb"]="ConnectionStrings__DeliusStagingDb"
["MatchingDb"]="ConnectionStrings__MatchingDb"
["OfflocRunningPictureDb"]="ConnectionStrings__OfflocRunningPictureDb"
["OfflocStagingDb"]="ConnectionStrings__OfflocStagingDb"
["CatsRabbitMQ"]="ConnectionStrings__CatsRabbitMQ"
["RabbitMQ"]="ConnectionStrings__RabbitMQ"
["DMSFilesBasePath"]="DMSFilesBasePath"
["DMSFilesBasePath_DbInteractions"]="DMSFilesBasePath"
["Sentry_Dsn"]="Sentry_Dsn"
)
# Get the full secret ARN
SECRET_ARN="${{ secrets.AWS_SECRETS_ARN }}"
for secret in $(echo "$SECRETS_JSON" | jq -r '.[]'); do
env_var_name="${SECRET_MAP[$secret]}"
if [ -n "$env_var_name" ]; then
SECRETS_STRING+="${env_var_name}=${SECRET_ARN}:${secret}::"$'\n'
fi
done
echo "Built secrets string:"
echo "$SECRETS_STRING"
SECRETS_STRING=$(echo "$SECRETS_STRING" | sed '/^$/d')
echo "secrets<<EOF" >> $GITHUB_OUTPUT
echo "$SECRETS_STRING" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Replace EFS placeholders and inject roles in task definition
id: inject-roles
run: |
TASK_DEF_PATH="${{ matrix.service.taskDef }}"
if [ ! -f "$TASK_DEF_PATH" ]; then
echo "Task definition not found: $TASK_DEF_PATH - Skipping"
echo "skip=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "Processing task definition: $TASK_DEF_PATH"
# Step 1: Replace placeholders (EFS, CloudWatch)
LOG_GROUP_NAME="dms-${{ inputs.environment == 'prod' && 'prod' || 'preprod' }}/ecs"
sed -i.bak \
-e 's/EFS_FILE_SYSTEM_ID/${{ steps.fetch-secrets.outputs.efs-fs-id }}/g' \
-e 's/EFS_ACCESS_POINT_ID/${{ steps.fetch-secrets.outputs.efs-ap-id }}/g' \
-e "s|PLACEHOLDER|$LOG_GROUP_NAME|g" \
"$TASK_DEF_PATH"
# Step 2: Inject IAM roles and set family name to match Terraform convention
FAMILY_NAME="dms-${{ inputs.environment == 'prod' && 'prod' || 'preprod' }}-${{ matrix.service.name }}-task"
UPDATED_TASK_DEF=$(jq \
--arg task_role "${{ steps.fetch-secrets.outputs.task_role }}" \
--arg exec_role "${{ steps.fetch-secrets.outputs.exec_role }}" \
--arg family "$FAMILY_NAME" \
'.taskRoleArn = $task_role | .executionRoleArn = $exec_role | .family = $family' \
"$TASK_DEF_PATH")
UPDATED_TASK_DEF_FILE="rendered-task-def.json"
echo "$UPDATED_TASK_DEF" > "$UPDATED_TASK_DEF_FILE"
echo "Updated task definition:"
cat "$UPDATED_TASK_DEF_FILE" | jq '.'
echo "updated_task_def_file=$UPDATED_TASK_DEF_FILE" >> $GITHUB_OUTPUT
echo "skip=false" >> $GITHUB_OUTPUT
- name: Render ECS task definition with secrets
id: render-task-def
uses: aws-actions/amazon-ecs-render-task-definition@77954e213ba1f9f9cb016b86a1d4f6fcdea0d57e # v1.8.4
with:
task-definition: ${{ steps.inject-roles.outputs.updated_task_def_file }}
container-name: ${{ matrix.service.container }}
image: ${{ steps.build-image.outputs.IMAGE }}
environment-variables: |
DOTNET_ENVIRONMENT=${{ inputs.environment == 'prod' && 'Production' || 'PreProduction' }}
secrets: ${{ steps.build-secrets.outputs.secrets }}
- name: Deploy ECS service
if: steps.build-image.outputs.skip != 'true'
uses: aws-actions/amazon-ecs-deploy-task-definition@fc8fc60f3a60ffd500fcb13b209c59d221ac8c8c # v2.6.1
with:
task-definition: ${{ steps.render-task-def.outputs.task-definition }}
service: dms-${{ inputs.environment == 'prod' && 'prod' || 'preprod' }}-${{ matrix.service.name }}-service
cluster: dms-${{ inputs.environment == 'prod' && 'prod' || 'preprod' }}-cluster
wait-for-service-stability: true