简体   繁体   English

从 GitHub Actions 中删除工作流

[英]Delete a workflow from GitHub Actions

I create a couple workflows in the .github/workflows folder of my repository to experiment with GitHub Actions.我在存储库的.github/workflows文件夹中创建了几个工作流来试验 GitHub Actions。 I have since learned quite a bit and deleted said "experimental" workflows from my repo.从那以后,我学到了很多东西,并从我的仓库中删除了所说的“实验性”工作流程。 After deleting the "experimental" workflow yaml files and committing the deletions, when I go to the Actions tab of my repository I STILL see the workflows that I have since deleted.在删除“实验性”工作流 yaml 文件并提交删除后,当我 go 到我存储库的“操作”选项卡时,我仍然看到我已经删除的工作流。

I see no option to delete and start from scratch??我看不到删除并从头开始的选项? Is this not possible?这不可能吗? Is it maybe possible through GitHub API. Hmm.是否可以通过 GitHub API。嗯。

As of July 7, 2020, you can now delete the results of individual workflow runs .自 2020 年 7 月 7 日起, 您现在可以删除单个工作流运行的结果 To do this, navigate to your workflow, find the workflow run that you want to delete, and select the "..." menu.为此,导航到您的工作流程,找到您要删除的工作流程运行,然后选择“...”菜单。 In this menu, select "Delete workflow run".在此菜单中,选择“删除工作流运行”。

The workflow run and its logs will be removed.工作流运行及其日志将被删除。

删除工作流运行

Currently, you must do this for each workflow run individually.目前,您必须为每个单独运行的工作流执行此操作。

edit: As of 2021 Feb it seems that after all workflow runs are deleted the workflow it self disappears.编辑:截至 2021 年 2 月,似乎在删除所有工作流程运行后,工作流程自行消失。 One comment below also seems to confirm this.下面的一条评论似乎也证实了这一点。

It doesn't seem that there is currently a way to delete those workflows - this makes no sense - but it appears that once one makes the mistake of creating one they are stuck with it forever.目前似乎没有一种方法可以删除这些工作流程——这毫无意义——但似乎一旦有人犯了创建一个错误,他们就会永远坚持下去。 The only solution so far I found is to disable these workflows.到目前为止,我发现的唯一解决方案是禁用这些工作流程。

So if I go to the Actions tab (edit the url to match your repo), I can then click on a workflow and disable it via [...] in the right top corner of that tab as in the snapshot below:因此,如果我转到Actions 选项卡(编辑 url 以匹配您的 repo),然后我可以单击一个工作流并通过该选项卡右上角的 [...] 禁用它,如下面的快照所示:

在此处输入图像描述

To delete all workflow results at once一次删除所有工作流程结果

To delete the records here is the solution I foundhere with slight modifications from the original:在此处删除记录是我在此处找到的解决方案,对原始版本稍作修改:

user=GH_USERNAME repo=REPO_NAME; gh api repos/$user/$repo/actions/runs \
--paginate -q '.workflow_runs[] | select(.head_branch != "master") | "\(.id)"' | \
xargs -n1 -I % gh api repos/$user/$repo/actions/runs/% -X DELETE

Replace GH_USERNAME and REPO_NAME with the desired github username and repo name correspondingly.GH_USERNAMEREPO_NAME替换为相应的 github 用户名和 repo 名称。

This will delete all the old workflows that aren't on the master branch.这将删除所有不在master分支上的旧工作流。 You can further tweak this to do what you need.你可以进一步调整它来做你需要的。

Prerequisites:先决条件:

  • You will find the latest gh version here .您将在此处找到最新的gh版本。

Notes:笔记:

  • You may have to gh auth login if this is your first time using it如果这是您第一次使用它,您可能需要gh auth login
  • You may further change the command to gh api --silent if you prefer not to see the verbose output.如果您不想看到详细的输出,您可以进一步将命令更改为gh api --silent
  • For the final xargs part of the command chain - the original used -J instead of -I , which is not supported by GNU xargs .对于命令链的最后xargs部分 - 原始使用-J而不是-I ,GNU xargs不支持。 -J results in a single command, and -I will execute the command for each records, so it's a bit slower. -J产生一个命令,而-I将为每条记录执行命令,所以它有点慢。

Thank you to the OP on the community forum for sharing this in first place.感谢社区论坛上的 OP 首先分享这个。

Here's a few commands to quickly clean up your workflows.这里有一些命令可以快速清理您的工作流程。

You'll need the xargs , gh and jq CLI tools.您将需要xargsghjq CLI 工具。

Depending on how many runs you have you'll have to execute the delete step multiple times because the GH API endpoints are paginated.根据您运行的次数,您必须多次执行删除步骤,因为 GH API 端点是分页的。

OWNER=<your user/org name>
REPO=<repo name>

# list workflows
gh api -X GET /repos/$OWNER/$REPO/actions/workflows | jq '.workflows[] | .name,.id'

# copy the ID of the workflow you want to clear and set it
WORKFLOW_ID=<workflow id>

# list runs
gh api -X GET /repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/runs | jq '.workflow_runs[] | .id'

# delete runs (you'll have to run this multiple times if there's many because of pagination)
gh api -X GET /repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/runs | jq '.workflow_runs[] | .id' | xargs -I{} gh api -X DELETE /repos/$OWNER/$REPO/actions/runs/{}

Based on the @Giampaolo Rodolà answer (which worked for me), I created this simple shell script that does the job.根据@Giampaolo Rodolà 的回答(对我有用),我创建了这个简单的 shell 脚本来完成这项工作。

Disable the workflow you want to delete (via Github console) before executing this script.在执行此脚本之前禁用要删除的工作流(通过 Github 控制台)。

org=<your org>
repo=<your repo>

# Get workflow IDs with status "disabled_manually"
workflow_ids=($(gh api repos/$org/$repo/actions/workflows | jq '.workflows[] | select(.["state"] | contains("disabled_manually")) | .id'))

for workflow_id in "${workflow_ids[@]}"
do
  echo "Listing runs for the workflow ID $workflow_id"
  run_ids=( $(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id') )
  for run_id in "${run_ids[@]}"
  do
    echo "Deleting Run ID $run_id"
    gh api repos/$org/$repo/actions/runs/$run_id -X DELETE >/dev/null
  done
done

Outcome:结果:

Listing runs for the workflow ID 5261185
Deleting Run ID 507553125
Deleting Run ID 507548002
Listing runs for the workflow ID 5261568
Deleting Run ID 525274011
Deleting Run ID 525264327
Deleting Run ID 525247443

Ensure to have Github client installed and required token permissions in Github.确保在 Github 中安装了 Github 客户端和所需的令牌权限。

I managed to fix this (currently not possible via UI) by using "gh" CLI tool and reading REST API docs .我设法通过使用“gh” CLI 工具和阅读REST API 文档来解决这个问题(目前无法通过 UI)。

First, get all your workflows (these are the ones shown in the web UI -> Actions -> left column):首先,获取所有工作流程(这些工作流程显示在 Web UI -> 操作 -> 左栏中):

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/workflows
{
  "total_count": 2,
  "workflows": [
    {
      "id": 3611607,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 4336318,
      "name": "MacOS",
      ...
    }
  ]
}

Use the ID of the workflow you want to delete (say 3611607) to get all of its individual runs:使用您要删除的工作流的 ID(例如 3611607)来获取其所有单独的运行:

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/workflows/3611607/runs
{
  "total_count": 17,
  "workflow_runs": [
    {
      "id": 363876785,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 363876786,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 363876787,
      "name": "FreeBSD",
      ...
    },
}

For each run id (let's say 363876785), delete it with:对于每个运行 ID(比如说 363876785),使用以下命令将其删除:

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/runs/363876785 -X DELETE

After this, the undeletable Action in the left column of the web UI should disappear.之后,Web UI 左栏中不可删除的 Action 应该会消失。

Delete all jobs belonged to your workflow and your workflow will be gone删除属于您的工作流程的所有工作,您的工作流程将消失

在此处输入图像描述

P/s: in the case you have thousand of jobs to delete, then using API is a good way to go with:https://docs.github.com/en/rest/reference/actions#workflow-runs P/s:如果您有数千个作业要删除,那么使用 API 是一个很好的方法:https ://docs.github.com/en/rest/reference/actions#workflow-runs

Until GitHub implements a "Delete all workflow runs", you have to rely on the API.在 GitHub 实现“删除所有工作流运行”之前,您必须依赖 API。 With the CLI tools gh and jq installed on your workstation, you can run the following commands to delete all runs of that workflow.在工作站上安装 CLI 工具ghjq后,您可以运行以下命令来删除该工作流的所有运行。 Once all runs are removed, it won't show up anymore in the UI .删除所有运行后,它将不再显示在 UI 中

cd /path/to/your/repo

gh workflow list # Pick-up the workflow ID for which you want to delete all runs
WORKFLOW_ID=<the workflow id> # Change this line!

# List last 10 runs of the workflow you picked to double check the id
gh run list -L 10 -w $WORKFLOW_ID

# Some set up
REPO_INFO=$(gh repo view --json name,owner)
REPO_FULL_NAME="$(echo $REPO_INFO | jq '.owner.login' -r)/$(echo $REPO_INFO | jq '.name' -r)"

# Ready? Let's delete some runs!
gh api -X GET "/repos/$REPO_FULL_NAME/actions/workflows/$WORKFLOW_ID/runs?per_page=100" | jq '.workflow_runs[] | .id' -r | xargs -t -I{} gh api --silent -X DELETE /repos/$REPO_FULL_NAME/actions/runs/{}

The last command will delete the last 100 runs (limit from GitHub API).最后一个命令将删除最后 100 次运行(来自 GitHub API 的限制)。 If you have more, run it multiple times to delete all.如果有更多,请多次运行以删除所有内容。

Delete all runs from a certain workflow从某个工作流中删除所有运行

An improved version of @Sheece Gardazi 's answer that supports selecting a certain workflow: @Sheece Gardazi答案的改进版本,支持选择特定工作流程:

export OWNER="my-user"
export REPOSITORY="my-repo"
export WORKFLOW="My Workflow"

gh api -X GET /repos/$OWNER/$REPOSITORY/actions/runs --paginate \
  | jq '.workflow_runs[] | select(.name == '\"$WORKFLOW\"') | .id' \
  | xargs -I{} gh api -X DELETE /repos/$OWNER/$REPOSITORY/actions/runs/{}

It requires GitHub CLI :它需要GitHub CLI

brew install gh
gh auth login

and jq :jq

brew install jq

Following to the Github Actions document: https://docs.github.com/en/actions/managing-workflow-runs/deleting-a-workflow-run遵循 Github Actions 文档: https ://docs.github.com/en/actions/managing-workflow-runs/deleting-a-workflow-run

It should be easy to delete a workflow which you don't need any more, like showing in this image删除不再需要的workflow应该很容易,就像在这张图片中显示的那样在此处输入图像描述

If you don't see that delete option but the disable workflow instead, then it's because that workflow still have some workflow runs .如果您没有看到delete option ,而是看到disable workflow ,那是因为该工作流仍然有一些workflow runs You need to delete those workflow runs and then the delete option will appears :)您需要删除这些工作流程运行,然后会出现删除选项:)

在此处输入图像描述

And a PowerShell implementation (thanks to the other respondents), which also requires the gh cli .还有一个 PowerShell 实现(感谢其他受访者),它也需要gh cli

$user = "your user"
$repo = "repo"

(gh api repos/$user/$repo/actions/runs | ConvertFrom-Json).workflow_runs |
 %{ $_.id } |
 %{ gh api repos/$user/$repo/actions/runs/$_ -X DELETE }

Re-run the "one-liner" until you have no more;重新运行“单线”,直到没有更多; it currently pages to 30 results.它目前分页到 30 个结果。

I had 600+ actions that I wanted deleted so there were multiple pages.我有 600 多个要删除的操作,因此有多个页面。 I had to run the command in for loop:我必须在 for 循环中运行命令:

# install following packages 
sudo snap install jq gh
# To authenticate github cli
gh auth login

# for reference path to your code repository: https://github.com/$OWNER/$REPOSITORY
export OWNER=<OWNER_NAME/ORGANIZATIONS_NAME>
export REPOSITORY=<REPOSITORY_NAME>

# repeat command 30 times, if there are 30 pages of workflow history 
for i in {1..30}; do gh api -X GET /repos/$OWNER/$REPOSITORY/actions/runs | jq '.workflow_runs[] | .id' | xargs -I{} gh api -X DELETE /repos/$OWNER/$REPOSITORY/actions/runs/{}; done

I wasn't able to delete the workflow inspite of all the answers in this post.. What worked for me was that I first authenticated myself using "gh auth login" and the used the below command to get the details of the workflow that you want delete.尽管这篇文章中有所有答案,但我无法删除工作流程。对我有用的是,我首先使用“gh auth login”对自己进行了身份验证,然后使用以下命令获取您的工作流程的详细信息想删除。 If you do not know the workflow id, then just run "gh api repos/$org/$repo/actions/workflows/" to see all the workflows.如果您不知道工作流 ID,则只需运行“gh api repos/$org/$repo/actions/workflows/”即可查看所有工作流。 Once you run this, you will know the branch where you need to delete the workflow from.运行此程序后,您将知道需要从中删除工作流的分支。 In our case, the work flow existed in the "develop" branch.在我们的例子中,工作流程存在于“开发”分支中。 You can see this in the "html_url".您可以在“html_url”中看到这一点。 Once I deleted the workflow file from the develop branch, the workflow vanished from everywhere.一旦我从开发分支中删除了工作流文件,工作流就从各处消失了。 Also, when you run the "gh api repos/$org/$repo/actions/workflows/$workflow_id", you will notice that the state will be changed to "deleted".此外,当您运行“gh api repos/$org/$repo/actions/workflows/$workflow_id”时,您会注意到状态将更改为“已删除”。

$> gh api repos/$org/$repo/actions/workflows/$workflow_id

{
  "id": 6,
  "node_id": "blah",
  "name": "Run Unit Tests",
  "path": ".github/workflows/unittests.yml",
  "state": "active",
  "created_at": "2021-05-15T00:25:19.000-07:00",
  "updated_at": "2022-03-10T13:02:43.000-08:00",
  "url": "blah",
  "html_url": "https://company-name/workspace/project/blob/develop/.github/workflows/unittests.yml",
  "badge_url": "blah"
}

For anyone wondering, deleting the workflow.yml files within .github/workflows works BUT you need to make sure it is deleted in all branches.对于任何想知道的人,删除.github/workflows中的workflow.yml文件是可行的,但您需要确保在所有分支中都将其删除。 If master/main still has the workflow files then GitHub will hold onto them.如果master/main仍然有工作流文件,那么 GitHub 将保留它们。

删除所有相关的工作流程运行后,它应该会自动删除。

Deleting the workflows runs via the CLI was only part of the solution in my case.就我而言,删除通过 CLI 运行的工作流只是解决方案的一部分。 GitHub still refused to show any workflows I tried to add again afterwards. GitHub 仍然拒绝显示我之后尝试再次添加的任何工作流程。

I solved it by using the " New workflow " button in GH and to create a workflow from template.我通过使用 GH 中的“新工作流程”按钮并从模板创建工作流程来解决它。 I pasted the content of my original YML file and renamed the file so that everything looked like before.我粘贴了原始 YML 文件的内容并重命名了该文件,以使一切看起来像以前一样。 Lastly, I committed via web - and GitHub showed again my workflow.最后,我通过网络提交——GitHub 再次展示了我的工作流程。

Here is another option to delete all logs from a Github actions workflow automatically, using Ritchie CLI .这是另一个使用Ritchie CLI自动从 Github 操作工作流中删除所有日志的选项。

All you need to do is:您需要做的就是:

  • run rit github delete workflow-logs运行rit github delete workflow-logs
  • inform your github username and token告知你的github 用户名令牌
  • inform the repo owner and name通知回购所有者名称
  • select the repo workflow to clean选择要清理的 repo 工作流程

An example can be seen here .可以在这里看到一个例子。 Note that you need to import this repository using the Ritchie CLI tool for the command to work.请注意,您需要使用 Ritchie CLI 工具导入此存储库才能使命令正常工作。

  1. Install the Ritchie CLI: https://docs.ritchiecli.io/安装 Ritchie CLI: https ://docs.ritchiecli.io/

  2. Run rit add repo运行rit add repo

? Select your provider: Github
? Repository name: formulas-github
? Repository URL: https://github.com/GuillaumeFalourd/formulas-github
? Is a private repository? no
? Select a tag version: {latest}
? Set the priority: 1
  1. Run rit set formula-runner运行rit set formula-runner
? Select a default formula run type: docker
The default formula runner has been successfully configured!
  1. Start Docker Desktop启动 Docker 桌面

  2. Go to https://github.com/settings/tokens , add generate a GitHub Personal Access Token (use the "workflow" scope)转到https://github.com/settings/tokens ,添加生成 GitHub 个人访问令牌(使用“工作流”范围)

  3. Run rit github delete workflow-logs运行rit github delete workflow-logs

  4. Enter your GitHub username, GitHub token, GitHub owner, GitHub repository name and select the workflow for which you want the runs to be deleted 🙌输入您的 GitHub 用户名、GitHub 令牌、GitHub 所有者、GitHub 存储库名称,然后选择您希望删除运行的工作流🙌

  5. Revoke your PAT撤销您的PAT

I created this command line tool to conveniently select multiple entries and delete them together from a navigable list:我创建了这个命令行工具来方便地选择多个条目并将它们从可导航列表中一起删除:

https://github.com/jv-k/delete-workflow-runs https://github.com/jv-k/delete-workflow-runs

I made some minor adjustments to @david-miguel 's answer which offloads the filtering to github's api instead of doing it locally.我对@david-miguel的回答做了一些小的调整,将过滤卸载到github的api,而不是在本地进行。

#!/bin/sh

OWNER="user-name|org-name"
REPOSITORY="repository-name"
WORKFLOW="workflow-human-friendly-name"

gh run list --repo "$OWNER/$REPOSITORY" -w "$WORKFLOW" --json databaseId \
    | jq '.[].databaseId' \
    | xargs -I{} gh api -X DELETE /repos/$OWNER/$REPOSITORY/actions/runs/{}

For the people who deleted the workflow and can't delete the workflow run (404)?对于删除工作流且无法删除工作流运行 (404) 的人?

After finding this thread, i still did spent quite some time to make my workflows run fade away...找到这个帖子后,我仍然花了相当长的时间让我的工作流程逐渐消失......

The answer provided by @ribeiro is correct, however for people that have DELETED their workflows, they won't be able to delete the runs , it will resolve into a 404! @ribeiro 提供的答案是正确的,但是对于已删除其工作流程的人,他们将无法删除运行,它将解析为 404!

How to delete workflows run that doesn't have workflows anymore I've tried many things, in the end, only this worked for me:如何删除不再有工作流的工作流运行我尝试了很多事情,最后,只有这对我有用:

  1. For each workflows you deleted, create the yml file with the same name that the previous one( filename.yml and the name property in the workflow must be the same as the previous one ).对于您删除的每个工作流,创建与前一个同名的 yml 文件( filename.yml工作流中的 name 属性必须与前一个相同)。 This way github will see them as 'existing'.这样 github 会将它们视为“现有”。
  2. Create a.sh file and input the following (i will use the answer provided by Ribeiro, however, i will remove the state filter because i wish to delete them all)创建 a.sh 文件并输入以下内容(我将使用 Ribeiro 提供的答案,但是,我将删除 state 过滤器,因为我希望将它们全部删除)
OWNER="aOwner"
REPO="aRepo"

workflow_ids=($(gh api repos/$OWNER/$REPO/actions/workflows | jq '.workflows[] | .id'))

for workflow_id in "${workflow_ids[@]}"
do
 echo "Listing runs for the workflow ID $workflow_id"
 run_ids=( $(gh api repos/$OWNER/$REPO/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id') )
 for run_id in "${run_ids[@]}"
 do
   echo "Deleting Run ID $run_id"
   gh api repos/$OWNER/$REPO/actions/runs/$run_id -X DELETE >/dev/null
 done
done
  1. Run the sh file.运行 .sh 文件。 You should see workflows run deleted (it does take some times tho)您应该会看到已删除运行的工作流程(确实需要一些时间)

As of Dec 31, 2022, assume you have a workflow .github/workflows/test.yml .截至 2022 年 12 月 31 日,假设您有一个工作流.github/workflows/test.yml

Once you delete this workflow file and merge into main branch, the workflow on the side menu plus all workflow run history will be gone.删除此工作流文件并合并到主分支后,侧面菜单上的工作流以及所有工作流运行历史记录都将消失。

You can verify by manually go to the old workflow link https://github.com/[username]/[repo]/actions/workflows/test.yml您可以通过手动 go 验证到旧的工作流程链接https://github.com/[username]/[repo]/actions/workflows/test.yml

在此处输入图像描述

This is the current result.这是目前的结果。

Here is an answer that just uses the GitHub CLI (you need to install and authenticate re: https://github.com/cli/cli#installation ) and awk:这是一个仅使用 GitHub CLI 的答案(您需要安装并验证 re: https://github.com/cli/cli#installation )和 awk:

OWNER="repoOwner"
REPO="repoName"

gh run list --repo <[HOST/]OWNER/REPO> -workflow <string> | \
  awk -F '\t' '{ if ($2 == "failure") { print $7 } }' | \
  while read id; do gh api repos/$OWNER/$REPO/runs/$id -X DELETE; done

Update your local branch from to sync with master , then delete the github/workflows.更新您的本地分支以与 master 同步,然后删除 github/workflows。 Commit and push your changes .提交并推送您的更改。 Wokflow should be deleted in master应在 master 中删除 Wokflow

I did find a way of doing this.我确实找到了这样做的方法。 You can go to .github/workflows or wherever your workflow is set and then commit deleting of the file(workflow file) which will finally delete it.您可以转到.github/workflows或设置工作流的任何位置,然后提交删除文件(工作流文件) ,最终将其删除。

If you want to delete multiple workflow runs, you should use the GitHub Action API to get the run ids you want to delete, then send DELETE request with a header containing personal access token to delete the workflow run.如果要删除多个工作流运行,则应使用 GitHub Action API 获取要删除的运行 ID,然后发送带有包含个人访问令牌的标头的 DELETE 请求以删除工作流运行。
Try this python script to delete all workflow runs.试试这个 python 脚本来删除所有工作流运行。
https://github.com/wh201906/garage/blob/master/Python/DeleteGithubAction/delete.py https://github.com/wh201906/garage/blob/master/Python/DeleteGithubAction/delete.py
It uses grequests to make multi requests at once, and this script doesn't require gh and jq.它使用 grequests 一次发出多个请求,并且此脚本不需要 gh 和 jq。

Your workflows are *.yml files saved in your repo in the folder /.github/workflows/您的工作流程是 *.yml 文件保存在文件夹 /.github/workflows/ 中的存储库中

Just delete them!只需删除它们!

我尝试从这个位置 .github/workflows/ 删除 yml 文件,它就像一个魅力。

you can delete the file from the Code tab in github as it was added as a normal commit您可以从 github 的 Code 选项卡中删除该文件,因为它是作为普通提交添加的

在此处输入图像描述

click on the file and then click delete icon:单击文件,然后单击删除图标:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM