-
Notifications
You must be signed in to change notification settings - Fork 12
45 lines (39 loc) · 1.81 KB
/
test-admin-delete-unused.yaml
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
name: Delete unused PR review environments
on:
workflow_dispatch:
schedule:
- cron: "0 4 * * *" # Nightly at 4am
env:
AWS_DEFAULT_REGION: ca-central-1
DELETE_AFTER_DAYS: 21
FUNCTION_PREFIX: "notify-admin-pr"
IMAGE: notify/admin
jobs:
delete-unused-test-admin:
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
id: aws-creds
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ca-central-1
- name: Delete old PR review environments
run: |
IFS=$'\n\t'
DELETE_DATE_EPOCH=$(date -d "-${{ env.DELETE_AFTER_DAYS }} days" +%s)
PR_REVIEW_ENVS="$(aws lambda list-functions --query 'Functions[?starts_with(FunctionName, `${{ env.FUNCTION_PREFIX }}`) == `true`]' | jq -c -r '.[]')"
for ENV in $PR_REVIEW_ENVS; do
FUNCTION_NAME="$(jq -r '.FunctionName' <<< $ENV)"
LAST_MODIFIED="$(jq -r '.LastModified' <<< $ENV)"
LAST_MODIFIED_EPOCH="$(date -d $LAST_MODIFIED +%s)"
if [ $LAST_MODIFIED_EPOCH -lt $DELETE_DATE_EPOCH ]; then
echo "Deleting $FUNCTION_NAME"
PR_NUMBER="${FUNCTION_NAME##*-}"
aws lambda delete-function-url-config --function-name ${{ env.FUNCTION_PREFIX }}-$PR_NUMBER
aws lambda delete-function --function-name ${{ env.FUNCTION_PREFIX }}-$PR_NUMBER
aws logs delete-log-group --log-group-name /aws/lambda/${{ env.FUNCTION_PREFIX }}-$PR_NUMBER
aws ecr batch-delete-image --repository-name $IMAGE --image-ids imageTag=$PR_NUMBER
fi
done