Skip to content

e2e-infra-cleanup #15757

e2e-infra-cleanup

e2e-infra-cleanup #15757

name: e2e-infra-cleanup
on:
workflow_dispatch:
inputs:
cleanup_interval_hours:
description: Cleanup interval in hours
required: true
default: "24"
schedule:
- cron: "0 * * * *"
concurrency: e2e-infra-cleanup
env:
AWS_DEFAULT_REGION: us-east-1
AWS_ACCESS_KEY_ID: ${{ secrets.E2E_TESTIM_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.E2E_TESTIM_AWS_SECRET_ACCESS_KEY }}
CLEANUP_INTERVAL_HOURS: ${{ github.event.inputs.cleanup_interval_hours || '24' }}
jobs:
find-expired-workspaces:
runs-on: ubuntu-latest
defaults:
run:
working-directory: automation/jumpbox
outputs:
workspaces: ${{ steps.get-workspaces.outputs.workspaces }}
count: ${{ steps.get-workspaces.outputs.count }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: replicatedhq/kots-regression-automation
token: ${{ secrets.E2E_GH_PAT }}
path: automation
ref: main
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- name: Initialize terraform
run: terraform init
- name: Get automation workspaces
id: get-workspaces
run: |
# automation workspace name is in the format automation-<id/sha>-<timestamp>
mapfile -t WORKSPACES < <(terraform workspace list | grep -o 'automation-.*')
declare -a EXPIRED_WORKSPACES
for workspace in "${WORKSPACES[@]}"; do
COMPLETION=$(echo "$workspace" | grep -o '[^-]*$' | grep -o '[0-9]*$')
if [ -z "$COMPLETION" ]; then
echo "Failed to parse completion date from workspace $workspace, adding to list of expired workspaces"
EXPIRED_WORKSPACES+=("${workspace}")
continue
fi
[ $(($(date +%s) - COMPLETION)) -gt $((60 * 60 * CLEANUP_INTERVAL_HOURS)) ] && EXPIRED_WORKSPACES+=("${workspace}")
done
echo "workspaces=$(printf '%s\n' "${EXPIRED_WORKSPACES[@]}" | head -c -1 | jq -R . | jq -sc .)" >> "$GITHUB_OUTPUT"
echo "count=$(printf '%s\n' "${EXPIRED_WORKSPACES[@]}" | head -c -1 | jq -R . | jq -sc '. | length')" >> "$GITHUB_OUTPUT"
cleanup-expired-workspaces:
needs: find-expired-workspaces
if: ${{ needs.find-expired-workspaces.outputs.count > 0 }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
workspace: ${{ fromJSON(needs.find-expired-workspaces.outputs.workspaces) }}
steps:
- name: Trigger workspace cleanup
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.E2E_GH_PAT }}
repository: replicatedhq/kots
event-type: e2e-workspace-cleanup
client-payload: '{"workspace": "${{ matrix.workspace }}"}'