Delete unused PR review environments #746
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |