From c2267e7cc1116a4627f5161c5a3af470a81c5f52 Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 5 Sep 2024 15:58:24 +0800 Subject: [PATCH] update workflow --- .github/workflows/delete_old_workflows.yml | 85 +++++++++++++++------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/.github/workflows/delete_old_workflows.yml b/.github/workflows/delete_old_workflows.yml index b1ff32fb..af16fda9 100644 --- a/.github/workflows/delete_old_workflows.yml +++ b/.github/workflows/delete_old_workflows.yml @@ -1,31 +1,66 @@ -name: Delete Old Workflows - +name: Delete old workflow runs on: workflow_dispatch: + inputs: + days: + description: 'Days-worth of runs to keep for each workflow' + required: true + default: '30' + minimum_runs: + description: 'Minimum runs to keep for each workflow' + required: true + default: '6' + delete_workflow_pattern: + description: 'Name or filename of the workflow (if not set, all workflows are targeted)' + required: false + delete_workflow_by_state_pattern: + description: 'Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually' + required: true + default: "ALL" + type: choice + options: + - "ALL" + - active + - deleted + - disabled_inactivity + - disabled_manually + delete_run_by_conclusion_pattern: + description: 'Remove runs based on conclusion: action_required, cancelled, failure, skipped, success' + required: true + default: "ALL" + type: choice + options: + - "ALL" + - "Unsuccessful: action_required,cancelled,failure,skipped" + - action_required + - cancelled + - failure + - skipped + - success + dry_run: + description: 'Logs simulated changes, no deletions are performed' + required: false jobs: - - delete-old-workflows: + del_runs: runs-on: ubuntu-latest - + permissions: + actions: write + contents: read steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Delete old workflows - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - min_created_at=$(date -d '7 days ago' +%Y-%m-%dT%H:%M:%SZ) - - workflows=$(gh workflow list --json name,path,createdAt --jq 'map({name: .name, path: .path, createdAt: .createdAt})') - - for workflow in $workflows; do - created_at=$(echo $workflow | jq -r '.createdAt') - if [ "$created_at" < "$min_created_at" ]; then - echo "Deleting workflow: $(echo $workflow | jq -r '.name') ($(echo $workflow | jq -r '.path'))" - gh workflow delete "$(echo $workflow | jq -r '.path')" --yes - else - echo "Keeping workflow: $(echo $workflow | jq -r '.name') ($(echo $workflow | jq -r '.path'))" - fi - done \ No newline at end of file + - name: Delete workflow runs + uses: Mattraks/delete-workflow-runs@v2 + with: + token: ${{ github.token }} + repository: ${{ github.repository }} + retain_days: ${{ github.event.inputs.days }} + keep_minimum_runs: ${{ github.event.inputs.minimum_runs }} + delete_workflow_pattern: ${{ github.event.inputs.delete_workflow_pattern }} + delete_workflow_by_state_pattern: ${{ github.event.inputs.delete_workflow_by_state_pattern }} + delete_run_by_conclusion_pattern: >- + ${{ + startsWith(github.event.inputs.delete_run_by_conclusion_pattern, 'Unsuccessful:') + && 'action_required,cancelled,failure,skipped' + || github.event.inputs.delete_run_by_conclusion_pattern + }} + dry_run: ${{ github.event.inputs.dry_run }} \ No newline at end of file