-
Notifications
You must be signed in to change notification settings - Fork 0
124 lines (114 loc) · 5.25 KB
/
dev-build.yml
File metadata and controls
124 lines (114 loc) · 5.25 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
### 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
on:
workflow_dispatch:
pull_request:
branches:
- main
paths-ignore:
- '.github/**'
permissions:
id-token: write
contents: read
jobs:
prep:
name: Prep for Build
runs-on: ubuntu-latest
outputs:
cpuarch: ${{ steps.setarch.outputs.cpuarch }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Set CPU Architecture
id: setarch
run: |
echo "### :abacus: Architecture Selection" >> $GITHUB_STEP_SUMMARY
if [[ -f .aws-architecture ]]; then
ARCH=$(cat .aws-architecture)
echo "\`$ARCH\` was read from \`.aws-architecture\` and passed to the deploy job." >> $GITHUB_STEP_SUMMARY
else
ARCH="linux/amd64"
echo "No \`.aws-architecture\` file, so default \`$ARCH\` was passed to the deploy job." >> $GITHUB_STEP_SUMMARY
fi
if [[ "$ARCH" != "linux/arm64" && "$ARCH" != "linux/amd64" ]]; then
echo "$ARCH is INVALID architecture!"
echo "$ARCH is INVALID architecture!" >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "cpuarch=$ARCH" >> $GITHUB_OUTPUT
deploy:
needs: prep
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"
CPU_ARCH: ${{ needs.prep.outputs.cpuarch }}
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
id: version
run: |
echo "Waiting for updated Lambda function to be ready"
aws lambda wait function-updated-v2 --region ${{ env.AWS_REGION }} --function-name ${{ env.FUNCTION }}
echo "New updated Lambda is ready."
echo "### Publish New Version of the Lambda" >> $GITHUB_STEP_SUMMARY
VERSION=$(aws lambda publish-version --region ${{ env.AWS_REGION }} --function-name ${{ env.FUNCTION }} --query 'Version' --output text)
echo "lambda_version=$VERSION" >> $GITHUB_OUTPUT
aws lambda wait published-version-active --region ${{ env.AWS_REGION }} --function-name ${{ env.FUNCTION }} --qualifier $VERSION
echo "New published Lambda is ready."
echo "New published Lambda version = $VERSION" >> $GITHUB_STEP_SUMMARY
- name: Update Lambda Alias
id: alias
env:
VERSION: ${{ steps.version.outputs.lambda_version }}
run: |
echo "### Update Lambda Alias" >> $GITHUB_STEP_SUMMARY
ALIAS_VERSION=$(aws lambda update-alias --region ${{ env.AWS_REGION }} --function-name ${{ env.FUNCTION }} --name live --function-version ${{ env.VERSION }} --routing-config '{}' --query 'FunctionVersion' --output text)
echo "Lambda alias linked to function version $ALIAS_VERSION" >> $GITHUB_STEP_SUMMARY
- name: Cleanup Lambda Versions
id: cleanup
run: |
echo "### Cleanup Lambda Versions" >> $GITHUB_STEP_SUMMARY
VERSIONS=$(aws lambda list-versions-by-function --function-name ${{ env.FUNCTION }} --query 'Versions[?Version!=`$LATEST`].Version')
echo "Current versions:"
echo "$VERSIONS"
VERSIONS_TO_DELETE=$(echo "$VERSIONS" | jq -r 'sort_by(tonumber) | .[:-2] | .[]')
echo "Versions to delete:"
echo "$VERSIONS_TO_DELETE"
while read VERSION; do
if [ -n "$VERSION" ]; then
echo "Deleting version: $VERSION"
aws lambda delete-function \
--function-name ${{ env.FUNCTION }} \
--qualifier "$VERSION"
fi
done <<< "$VERSIONS_TO_DELETE"
CURRENT_VERSIONS=$(aws lambda list-versions-by-function --function-name ${{ env.FUNCTION }} --query 'Versions[?Version!=`$LATEST`].Version' --output text)
echo "Current available versions of the Lambda: $CURRENT_VERSIONS" >> $GITHUB_STEP_SUMMARY