Delete old step functions #1210
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 old step functions | |
on: | |
schedule: | |
- cron: '20 4 * * *' | |
workflow_dispatch: | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
load-config: | |
uses: ./.github/workflows/load-config.yaml | |
delete-unused-step-functions: | |
name: Delete old step functions | |
runs-on: ubuntu-20.04 | |
needs: load-config | |
strategy: | |
matrix: | |
environment_name: ${{fromJson(needs.load-config.outputs.environment_names)}} | |
environment: ${{ matrix.environment_name }} | |
steps: | |
- id: setup-aws | |
name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/ci-iac-role | |
aws-region: ${{ secrets.AWS_REGION }} | |
# If the step functions list is too close to its limit, | |
# this will remove the 500 older step functions. | |
- id: remove-old-state-machines | |
name: Remove old step functions | |
run: |- | |
state_machines_list=$(aws stepfunctions list-state-machines --output text) | |
threshold=500 | |
maximum=$((10000 - $threshold)) | |
count=$(echo $state_machines_list | grep -o 'arn:aws:states:' | wc -l) | |
echo $count | |
if (($count > $maximum)) | |
then | |
echo "$count higher than the maximum, so deleting $threshold state machines" | |
echo $state_machines_list \ | |
| sed 's/STATEMACHINES/\n STATEMACHINES/g' \ | |
| sort -k2 \ | |
| head -$threshold \ | |
| awk '{print $4}' \ | |
| xargs -I {} aws stepfunctions delete-state-machine --state-machine-arn {} | |
echo "Finished deleting state machines" | |
else | |
echo "No state machines deleted" | |
fi | |
# If the activities list is too close to its limit, | |
# this will remove the 500 older activities. | |
- id: remove-old-activities | |
name: Remove old activities | |
run: |- | |
activities_list=$(aws stepfunctions list-activities --output text) | |
threshold=500 | |
maximum=$((10000 - $threshold)) | |
count=$(echo $activities_list | grep -o 'arn:aws:states:' | wc -l) | |
echo $count | |
if (($count > $maximum)) | |
then | |
echo "$count higher than the maximum, so deleting $threshold activities" | |
echo $activities_list \ | |
| sed 's/ACTIVITIES/\n ACTIVITIES/g' \ | |
| sort -k2 \ | |
| head -$threshold \ | |
| awk '{print $2}' \ | |
| xargs -I {} aws stepfunctions delete-activity --activity-arn {} | |
echo "Finished deleting activities" | |
else | |
echo "No activities deleted" | |
fi | |
- id: send-to-slack | |
name: Send failure notification to Slack | |
if: failure() && github.ref == 'refs/heads/master' | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.WORKFLOW_STATUS_BOT_TOKEN }} | |
uses: voxmedia/github-action-slack-notify-build@v1 | |
with: | |
channel: workflow-failures | |
status: FAILED | |
color: danger |