Delete GitHub Action Workflow Test Runs using GitHub CLI

Working with GitHub Actions has dramatically changed the way we manage CI/CD pipelines. However, as developers, we often push test runs, especially when fine-tuning our workflows. While these test runs serve an essential purpose in development, they can quickly clutter the Workflow history. And let’s face it, not all of them need to stick around, so we need something to delete GitHub Action workflow runs.

The Dilemma with Local Development Tools

It’s tempting to entirely rely on local development tools for testing workflows, such as act for GitHub Actions. While they can be somewhat beneficial, they often give incorrect feedback, particularly for intricate workflows, compared to running directly in GitHub Actions. This can lead to developers spending extra time troubleshooting issues that may not exist in the live environment or overlooking critical issues that only manifest in the CI/CD runners.

The Power of the GitHub CLI

Given the inconsistencies with local testing tools, developers tend to push test runs to GitHub, just like myself. These runs, in my case ones prefixed with “WIP” (Work In Progress), are usually meant for initial testing and debug purposes. To streamline the delete GitHub Action workflow runs process, I’ve crafted a simple script using the GitHub CLI combined with command-line JSON processor, jq.

aliases:
    wip-workflow-clean: |-
        !gh api /repos/:owner/:repo/actions/workflows | jq -c '.workflows[] | select(.name | contains("WIP")) | .id' | while read i; do
            gh api -X GET /repos/:owner/:repo/actions/workflows/$i/runs?per_page=100 | jq '.workflow_runs[] | .id' | while read i; do
                gh api --silent -X DELETE /repos/:owner/:repo/actions/runs/$i
            done
        done

When run from the directory of a repository, this script automatically identifies and removes any Workflow runs prefixed with “WIP” for the repository on GitHub. This approach ensures that your Workflow history remains neat, tidy, and representative of the runs that truly matter.

How to Implement the Workflow Delete Script:

To harness the power of this cleanup operation:

  1. Set Up GitHub CLI: Ensure you have the GitHub-cli installed and authenticated for your account.
  2. Ensure jq is Installed: Used your preferred download method.
  3. Add Script to GitHub CLI Config: Add the script to your ~/.config/gh/config.yml.
  4. Run the Script: Navigate to your repo and execute the script. Watch as your Workflow history gets decluttered, removing all those unnecessary test runs!
$ ~/github-repo: gh wip-workflow-clean

Maintaining a clean and efficient development process is pivotal for productive software development. With our extended GitHub CLI script, you can effortlessly keep your GitHub Actions Workflow history clean and meaningful. Say goodbye to unnecessary test runs and ensure a clean slate for your continuous integration and deployment journey.