DEV Community

Ewerton Jordão
Ewerton Jordão

Posted on • Originally published at Medium on

GitHub Actions |How to remove all Failed Actions | PWSH ⚡ + GitHub API

Hello everyone, thank you very much for accessing this content, if you are here, you also use GitHub Actions and, of course, when you write an action sometimes that action fails, the action history grows and you need to remove it one by one of that history, it’s a boring task and you need to do it. What could facilitate this task? Okay, come on, we can make this task easier with PWSH7.

I wrote some Actions to automate tasks on my raspberry , my raspberry now it’s my Self Hosted Runner, In GitHub Actions you be able to use runners on plataform MacOS,Windows,Linux or Self Hosted in this particular mode, you need to management everything about admin tasks.

Before you need to have some things:

  1. Create a token on GitHub for connect on RestAPI (if you won’t provide this token you can’t interact with your GitHub Actions)
  2. PowerShell 7
  3. Secrests Managment(not mandatory but it’s cool to know)

Steps for create a token:

Click on your Avatar and select Settings
Click on your Avatar and select Settings

When open you profile ,select Developer Settings.
When open you profile ,select Developer Settings.

Personal access tokens  after click on Generate new token
Click on Personal access tokens after click on Generate new token

Flag those options
Flag those options

And apply these configurations
And apply these configurations

Now I show you the code 😃:It’s very simple after you had your personal access token, you need to know wich repository you want to make cleanup and that’s it 😃:

All workflows
All workflows

After run script remove jobs with failure status
After run script remove jobs with failure status

cleanup is done
cleanup is done

PowerShell Script:

#required https://www.powershellgallery.com/packages/Microsoft.PowerShell.SecretManagement/0.5.3-preview4
$githubUser = 'youruser'
$githubRepository = 'yourRepo'
$uriBase = "https://api.github.com"
$baseHeader = @{"Authorization" = "token $(Get-Secret -Name KeyGitHub -AsPlainText)" ; "Content-Type" = "application/json" }
$runsActiveParams = @{
Uri = ("{0}/repos/{1}/{2}/actions/runs" -f $uriBase ,$githubUser, $githubRepository)
Method = "Get"
Headers = $baseHeader
}
$runsActive = Invoke-RestMethod @runsActiveParams
$actionsFailure = $runsActive.workflow_runs | Where-Object { ($_.conclusion -eq "failure")}
[array]$baseURIJobs = @()
foreach ($actionFail in $actionsFailure.id) {
$baseURIJobs += ("/repos/{0}/{1}/actions/runs/{2}" -f $githubUser, $githubRepository, $actionFail)
}
foreach ($baseURIJob in $baseURIJobs) {
$runsDeleteParam = @{
Uri = ( "{0}{1}" -f $uriBase,$baseURIJob )
Method = "Delete"
Headers = $baseHeader
}
Write-Host "Delete job $(($runsDeleteParam.Uri -split "/")[8])"
Invoke-RestMethod @runsDeleteParam
}
view raw CleanupRepo.ps1 hosted with ❤ by GitHub

That’s it. I really wish you appreciate this content, if you liked that content share with all. See you at next opportunity.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay