-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (152 loc) · 6.73 KB
/
Copy pathdev-build.yml
File metadata and controls
164 lines (152 loc) · 6.73 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
### This is the Terraform-generated dev-build.yml workflow for the ###
### timdex-semantic-builder-dev app repository. ###
### If this is a Lambda repo, uncomment the FUNCTION line at the end of ###
### the document. If the container requires any additional pre-build ###
### commands, uncomment and edit the PREBUILD line at the end of the ###
### document. ###
name: Dev Container Build and Deploy
# Allow for manual runs (workflow_dispatch) and PRs to the main branch except
# for changes in the .github directory and the README
on:
workflow_dispatch:
pull_request:
branches:
- main
paths-ignore:
- '.github/**'
- 'README.md'
# Force multiple invocations of this workflow to run in sequence not in
# parallel
concurrency:
group: dev-build-deploy
cancel-in-progress: false
queue: single
# Minimum required permissions for this job (the id-token: write is required
# for OIDC authentication to AWS)
permissions:
id-token: write
contents: read
jobs:
deploy:
name: Dev Deploy
uses: mitlibraries/.github/.github/workflows/ecr-multi-arch-deploy-dev.yml@main
secrets: inherit
with:
AWS_REGION: "us-east-1"
GHA_ROLE: "timdex-semantic-builder-gha-dev"
ECR: "timdex-semantic-builder-dev"
FUNCTION: "timdex-semantic-builder-dev"
# PREBUILD:
# The following section is specific to this function because this is our first
# Lambda function that requires provisioned capacity to stay warm. As a
# consequence, we need to ensure that the Lambda is "published" and that the
# Lambda alias points at the latest published version. We may eventually move
# this to the shared workflow...
publish:
needs: deploy
name: Publish and Update Alias
env:
AWS_REGION: "us-east-1"
GHA_ROLE: "timdex-semantic-builder-gha-dev"
FUNCTION: "timdex-semantic-builder-dev"
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-region: us-east-1
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCT_DEV }}:role/${{ env.GHA_ROLE }}
- name: Publish New Version
# After deploying a Lambda, there is a delay before it is ready, so we
# use the "aws lambda wait" command to pause until AWS confirms that the
# Lambda is ready before attempting to publish a version of the Lambda.
# There is another delay after publishing before the published Lambda is
# actually available, so we use the `aws lambda wait` again.
id: version
run: |
echo "### Publish New Version of Lambda" >> $GITHUB_STEP_SUMMARY
echo "Waiting for updated Lambda function to be ready"
aws lambda wait function-updated-v2 \
--region "$AWS_REGION" \
--function-name "$FUNCTION"
echo "New updated Lambda is ready."
echo "Publish New Version of the Lambda"
VERSION=$(aws lambda publish-version \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--query 'Version' \
--output text)
echo "lambda_version=$VERSION" >> $GITHUB_OUTPUT
aws lambda wait published-version-active \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--qualifier $VERSION
echo "New published Lambda is ready."
echo "New published Lambda version = $VERSION" >> $GITHUB_STEP_SUMMARY
- name: Update Lambda Alias
# Update the alias to point to the latest version. But, then we need to
# wait for the alias to stabilize (e.g., for the old version to be
# completely removed from the alias). That is, we do not consider the
# Lambda alias updated until after it has stabilized to just one version
id: alias
env:
VERSION: ${{ steps.version.outputs.lambda_version }}
run: |
echo "### Update Lambda Alias" >> $GITHUB_STEP_SUMMARY
NEW_ALIAS_VERSION=$(aws lambda update-alias \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--name live \
--function-version "$VERSION" \
--routing-config '{}' \
--query 'FunctionVersion' \
--output text)
echo "Lambda alias updated and now linked to function version $NEW_ALIAS_VERSION" >> $GITHUB_STEP_SUMMARY
echo "Start polling for alias stabilization"
MAX_ATTEMPTS=30
DELAY_IN_SECS=10
ATTEMPT=0
echo "Waiting for Lambda alias stabilization"
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
LAMBDA_ALIAS_INFO=$(aws lambda get-alias \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--name live)
CURRENT_VERSION=$(echo "$LAMBDA_ALIAS_INFO" | jq -r '.FunctionVersion')
ROUTING_CONFIG=$(echo "$LAMBDA_ALIAS_INFO" | jq -r '.RoutingConfig // empty')
if [ "$CURRENT_VERSION" = "$VERSION" ] && [ -z "$ROUTING_CONFIG" ]; then
echo "Lambda alias has stabilized"
echo "Lambda alias has stabilized on version $CURRENT_VERSION" >> $GITHUB_STEP_SUMMARY
exit 0
fi
ATTEMPT=$(($ATTEMPT + 1))
echo "Attempt $ATTEMPT of $MAX_ATTEMPTS; wait $DELAY_IN_SECS seconds"
sleep $DELAY_IN_SECS
done
echo "Lambda alias did not stabilize within timeout period" >> $GITHUB_STEP_SUMMARY
exit 1
- name: Cleanup Lambda Versions
id: cleanup
run: |
echo "### Cleanup Lambda Versions" >> $GITHUB_STEP_SUMMARY
VERSIONS=$(aws lambda list-versions-by-function \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--query 'Versions[?Version!=`$LATEST`].Version')
echo "Current versions: $VERSIONS"
VERSIONS_TO_DELETE=$(echo "$VERSIONS" | jq -r 'sort_by(tonumber) | .[:-2] | .[]')
echo "Versions to delete: $VERSIONS_TO_DELETE"
while read VERSION; do
if [ -n "$VERSION" ]; then
echo "Deleting version: $VERSION"
aws lambda delete-function \
--region "$AWS_REGION" \
--function-name "$FUNCTION" \
--qualifier "$VERSION"
fi
done <<< "$VERSIONS_TO_DELETE"
CURRENT_VERSIONS=$(aws lambda list-versions-by-function \
--function-name "$FUNCTION" \
--query 'Versions[?Version!=`$LATEST`].Version' \
--output text)
echo "Current available versions of the Lambda: $CURRENT_VERSIONS" >> $GITHUB_STEP_SUMMARY