-
Notifications
You must be signed in to change notification settings - Fork 0
227 lines (201 loc) · 7.46 KB
/
Copy pathlambda-deploy.yml
File metadata and controls
227 lines (201 loc) · 7.46 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
name: Lambda Deploy
on:
push:
branches: [main]
paths:
- 'apps/backend/lambdas/**'
- 'shared/types/**'
workflow_dispatch:
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
lambdas: ${{ steps.detect.outputs.lambdas }}
has-changes: ${{ steps.detect.outputs.has-changes }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect changed lambdas
id: detect
run: |
BASE_COMMIT="${{ github.event.before }}"
all_lambdas=$(ls -d apps/backend/lambdas/*/ | grep -Ev "(tools)/" | sed 's#/$##' | jq -R -s -c 'split("\n") | map(select(. != ""))')
changed_files=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || git diff --name-only HEAD)
changed_lambdas=()
for lambda_path in apps/backend/lambdas/*/; do
lambda_name=$(basename "$lambda_path")
if [[ "$lambda_name" != "tools" ]]; then
if echo "$changed_files" | grep -q "^apps/backend/lambdas/$lambda_name/"; then
changed_lambdas+=("$lambda_path")
fi
fi
done
if [[ ${#changed_lambdas[@]} -eq 0 ]]; then
echo "No specific lambda changes detected, deploying all lambdas"
lambdas=$all_lambdas
has_changes="true"
else
lambdas=$(printf '%s\n' "${changed_lambdas[@]}" | sed 's#/$##' | jq -R -s -c 'split("\n") | map(select(. != ""))')
has_changes="true"
fi
echo "lambdas=$lambdas" >> $GITHUB_OUTPUT
echo "has-changes=$has_changes" >> $GITHUB_OUTPUT
# Post the initial "Deploying backend" Slack message before builds run.
prep:
needs: detect-changes
if: needs.detect-changes.outputs.has-changes == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
ts: ${{ steps.slack.outputs.ts }}
total: ${{ steps.count.outputs.total }}
steps:
- uses: actions/checkout@v4
- name: Count lambdas
id: count
env:
LAMBDAS: ${{ needs.detect-changes.outputs.lambdas }}
run: echo "total=$(echo "$LAMBDAS" | jq 'length')" >> "$GITHUB_OUTPUT"
- name: Start deploy notification
id: slack
uses: ./.github/actions/slack-deploy
with:
mode: start
component: backend
total-steps: ${{ steps.count.outputs.total }}
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
build:
needs: detect-changes
if: needs.detect-changes.outputs.has-changes == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
lambda: ${{ fromJson(needs.detect-changes.outputs.lambdas) }}
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Build shared lambda-auth package
run: npm ci --prefix shared/lambda-auth && npm run build --prefix shared/lambda-auth
- name: Install dependencies
working-directory: ${{ matrix.lambda }}
run: npm ci --legacy-peer-deps
- name: Package lambda
working-directory: ${{ matrix.lambda }}
run: npm run package
- name: Compute artifact name
id: artifact
run: echo "name=lambda-$(basename ${{ matrix.lambda }})" >> $GITHUB_OUTPUT
- name: Upload lambda artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.artifact.outputs.name }}
path: ${{ matrix.lambda }}/lambda.zip
if-no-files-found: error
deploy:
needs: [detect-changes, prep, build]
if: needs.detect-changes.outputs.has-changes == 'true'
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write # assume the OIDC apply role
contents: read
actions: read # count completed legs for the live status bar
pull-requests: read
strategy:
fail-fast: false
matrix:
lambda: ${{ fromJson(needs.detect-changes.outputs.lambdas) }}
steps:
- name: Compute artifact name
id: artifact
run: echo "name=lambda-$(basename ${{ matrix.lambda }})" >> $GITHUB_OUTPUT
- name: Download lambda artifact
uses: actions/download-artifact@v4
with:
name: ${{ steps.artifact.outputs.name }}
path: ${{ matrix.lambda }}
- name: Configure AWS credentials (write apply role via OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::489881683177:role/branch-ci-apply
aws-region: us-east-2
- name: Deploy lambda
id: deploy
run: |
set -o pipefail
LAMBDA_NAME=$(basename ${{ matrix.lambda }})
FUNCTION_NAME="branch-${LAMBDA_NAME}"
{
echo "Deploying Lambda function: $FUNCTION_NAME"
aws lambda update-function-code \
--function-name "$FUNCTION_NAME" \
--zip-file fileb://${{ matrix.lambda }}/lambda.zip
echo "✓ Successfully deployed $FUNCTION_NAME"
} 2>&1 | tee deploy.log
- name: Compute result name
id: rname
if: always()
run: echo "name=$(basename ${{ matrix.lambda }})" >> "$GITHUB_OUTPUT"
- name: Save deploy result
if: always()
run: |
mkdir -p lambda-results
echo "${{ steps.rname.outputs.name }}|${{ steps.deploy.outcome }}" > "lambda-results/${{ steps.rname.outputs.name }}.meta"
cp deploy.log "lambda-results/${{ steps.rname.outputs.name }}.log" 2>/dev/null \
|| echo "(no deploy output captured)" > "lambda-results/${{ steps.rname.outputs.name }}.log"
- name: Upload deploy result
if: always()
uses: actions/upload-artifact@v4
with:
name: lambda-result-${{ steps.rname.outputs.name }}
path: lambda-results/
if-no-files-found: warn
- name: Thread step (live)
if: always()
uses: ./.github/actions/slack-deploy
with:
mode: step
component: backend
total-steps: ${{ needs.prep.outputs.total }}
job-name: deploy
step-name: Deployed branch-${{ steps.rname.outputs.name }}
step-status: ${{ steps.deploy.outcome }}
details-file: deploy.log
ts: ${{ needs.prep.outputs.ts }}
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
notify:
name: Deployment Notify
needs: [detect-changes, prep, deploy]
if: always() && needs.detect-changes.outputs.has-changes == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download deploy results
uses: actions/download-artifact@v4
with:
pattern: lambda-result-*
path: lambda-results
merge-multiple: true
- name: Finalize deploy notification
uses: ./.github/actions/slack-deploy
with:
mode: finalize
component: backend
total-steps: ${{ needs.prep.outputs.total }}
results-dir: lambda-results
ts: ${{ needs.prep.outputs.ts }}
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}