Naposledy aktivní 1 month ago

GitHub Action

jetsung revidoval tento gist 7 months ago. Přejít na revizi

4 files changed, 4 insertions, 4 deletions

remove_github_deployments_run.sh

@@ -3,7 +3,7 @@
3 3 #============================================================
4 4 # File: remove_github_workflow_runs.sh
5 5 # Description: 批量删除 GitHub 部署记录
6 - # URL: https://s.fx4.cn/
6 + # URL: https://fx4.cn/
7 7 # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_deployments_run.sh
8 8 # Author: Jetsung Chan <[email protected]>
9 9 # Version: 0.1.0

remove_github_packages_untagged.sh

@@ -3,7 +3,7 @@
3 3 #============================================================
4 4 # File: remove_github_packages_untagged.sh
5 5 # Description: 删除 GitHub Packages 悬空的镜像标签
6 - # URL: https://s.fx4.cn/
6 + # URL: https://fx4.cn/
7 7 # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_packages_untagged.sh
8 8 # Author: Jetsung Chan <[email protected]>
9 9 # Version: 0.1.0

remove_github_workflow_runs.ps1

@@ -1,7 +1,7 @@
1 1 #============================================================
2 2 # File: Remove-GitHubWorkflowRuns.ps1
3 3 # Description: 批量删除 GitHub Action Workflows 流水线
4 - # URL: https://s.fx4.cn/
4 + # URL: https://fx4.cn/
5 5 # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_workflow_runs.ps1
6 6 # Author: Jetsung Chan <[email protected]>
7 7 # Ported to PowerShell by: Jetsung Chan

remove_github_workflow_runs.sh

@@ -3,7 +3,7 @@
3 3 #============================================================
4 4 # File: remove_github_workflow_runs.sh
5 5 # Description: 批量删除 GitHub Action Workflows 流水线
6 - # URL: https://s.fx4.cn/
6 + # URL: https://fx4.cn/
7 7 # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_workflow_runs.sh
8 8 # Author: Jetsung Chan <[email protected]>
9 9 # Version: 0.1.0

jetsung revidoval tento gist 9 months ago. Přejít na revizi

1 file changed, 114 insertions

remove_github_workflow_runs.ps1(vytvořil soubor)

@@ -0,0 +1,114 @@
1 + #============================================================
2 + # File: Remove-GitHubWorkflowRuns.ps1
3 + # Description: 批量删除 GitHub Action Workflows 流水线
4 + # URL: https://s.fx4.cn/
5 + # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_workflow_runs.ps1
6 + # Author: Jetsung Chan <[email protected]>
7 + # Ported to PowerShell by: Jetsung Chan
8 + # Version: 0.1.0
9 + # CreatedAt: 2025-08-27
10 + # UpdatedAt: 2025-08-27
11 + # Requires: PowerShell 7+, GitHub CLI (gh), jq (optional, but used here)
12 + #============================================================
13 +
14 + # 等效于 set -e: 遇到错误(非0退出码或异常)立即终止脚本
15 + # $ErrorActionPreference = "Stop"
16 +
17 + # 等效于 set -u: 严格模式,禁止使用未初始化的变量
18 + # Set-StrictMode -Version Latest
19 +
20 + # 等效于 set -x: 跟踪命令执行(显示执行的命令)
21 + # Set-PSDebug -Trace 1
22 +
23 + # 或者(退出程序)
24 + # Set-PSDebug -Off
25 +
26 + param(
27 + [Parameter(Mandatory = $true)]
28 + [string]$OrgName,
29 +
30 + [Parameter(Mandatory = $true)]
31 + [string]$RepoName
32 + )
33 +
34 + # 启用严格模式
35 + $ErrorActionPreference = 'Stop'
36 + if ($env:DEBUG) { $PSNativeCommandUseErrorActionPreference = $true }
37 +
38 + $Repo = "$OrgName/$RepoName"
39 + $Url = "repos/$Repo/actions/runs"
40 +
41 + $TotalDeleted = 0
42 +
43 + function Delete-RunId {
44 + param([string]$Id)
45 +
46 + Write-Host "Deleting URL: $Url/$Id"
47 + $Result = $null
48 +
49 + try {
50 + # 使用 gh api 删除指定的 workflow run
51 + gh api -X DELETE "$Url/$Id" --silent | Out-Null
52 + $Result = "✅ Deleted '$Id'"
53 + $script:TotalDeleted++
54 + }
55 + catch {
56 + $Result = "❌ Failed '$Id'"
57 + Write-Host $Result
58 + Write-Host "An error occurred while deleting ID '$Id'. Press Enter to exit."
59 + Write-Host "Total IDs deleted: $script:TotalDeleted"
60 + $null = Read-Host
61 + exit 1
62 + }
63 +
64 + Write-Host $Result
65 + }
66 +
67 + # 设置 UTF-8 输出编码(关键!)
68 + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
69 +
70 + # 可选环境变量
71 + $env:GH_NO_COLOR = "1"
72 + $env:NO_COLOR = "1"
73 +
74 + while ($true) {
75 + try {
76 + # 使用 --jq '.' 获取纯净 JSON,并确保 UTF-8 输出
77 + $JsonOutput = gh api "$Url" --jq '.' 2>$null
78 +
79 + # 清理 ANSI 颜色码
80 + $JsonOutput = $JsonOutput -replace "\x1B\[[0-9;]*[a-zA-Z]", ""
81 + $JsonOutput = $JsonOutput.Trim()
82 +
83 + if ([string]::IsNullOrWhiteSpace($JsonOutput)) {
84 + Write-Host "空响应。按回车退出。"
85 + $null = Read-Host
86 + exit 0
87 + }
88 +
89 + # 现在可以正确解析含中文的 JSON
90 + $Response = $JsonOutput | ConvertFrom-Json
91 +
92 + } catch {
93 + Write-Error "JSON 解析失败。原始内容:`n$JsonOutput"
94 + exit 1
95 + }
96 +
97 + $Runs = $Response.workflow_runs
98 + $TotalIds = $Runs.Count
99 +
100 + if ($TotalIds -eq 0) {
101 + Write-Host "No more IDs to delete. Press Enter to exit."
102 + Write-Host "Total IDs deleted: $TotalDeleted"
103 + $null = Read-Host
104 + break
105 + }
106 +
107 + foreach ($Run in $Runs) {
108 + $Id = $Run.id.ToString() # 确保是字符串,并去除可能的换行符等
109 + Delete-RunId -Id $Id
110 + }
111 +
112 + # 可选:避免 API 限流,暂停 2 秒
113 + # Start-Sleep -Seconds 2
114 + }

jetsung revidoval tento gist 9 months ago. Přejít na revizi

3 files changed, 192 insertions

remove_github_deployments_run.sh(vytvořil soubor)

@@ -0,0 +1,48 @@
1 + #!/usr/bin/env bash
2 +
3 + #============================================================
4 + # File: remove_github_workflow_runs.sh
5 + # Description: 批量删除 GitHub 部署记录
6 + # URL: https://s.fx4.cn/
7 + # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_deployments_run.sh
8 + # Author: Jetsung Chan <[email protected]>
9 + # Version: 0.1.0
10 + # CreatedAt: 2025-08-18
11 + # UpdatedAt: 2025-08-18
12 + #============================================================
13 +
14 +
15 + if [[ -n "${DEBUG:-}" ]]; then
16 + set -eux
17 + else
18 + set -euo pipefail
19 + fi
20 +
21 + owner="$1" # 替换为仓库所有者的用户名
22 + repo="$2" # 替换为仓库名称
23 +
24 + # 获取所有部署记录的 ID,按创建时间降序排列
25 + deployment_ids=$(gh api "repos/$owner/$repo/deployments" --paginate --jq 'sort_by(.created_at) | reverse | .[].id')
26 +
27 + # 将部署 ID 保存到数组中
28 + readarray -t ids <<<"$deployment_ids"
29 +
30 + # 获取部署记录的数量
31 + num_deployments=${#ids[@]}
32 +
33 + # 如果部署记录少于2个,则无需删除
34 + if [[ "$num_deployments" -lt 2 ]]; then
35 + echo "没有足够的部署记录可供删除。"
36 + exit 0
37 + fi
38 +
39 + # 遍历部署 ID,跳过最新的一个
40 + for ((i=1; i<num_deployments; i++)); do
41 + deployment_id=${ids[i]}
42 + echo "删除部署 ID: $deployment_id"
43 + if ! gh api -X DELETE "repos/$owner/$repo/deployments/$deployment_id" --silent; then
44 + echo "失败"
45 + fi
46 + done
47 +
48 + echo "旧的部署记录已删除,仅保留最新的一个。"

remove_github_packages_untagged.sh(vytvořil soubor)

@@ -0,0 +1,77 @@
1 + #!/usr/bin/env bash
2 +
3 + #============================================================
4 + # File: remove_github_packages_untagged.sh
5 + # Description: 删除 GitHub Packages 悬空的镜像标签
6 + # URL: https://s.fx4.cn/
7 + # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_packages_untagged.sh
8 + # Author: Jetsung Chan <[email protected]>
9 + # Version: 0.1.0
10 + # CreatedAt: 2025-08-18
11 + # UpdatedAt: 2025-08-18
12 + #============================================================
13 +
14 +
15 + if [[ -n "${DEBUG:-}" ]]; then
16 + set -eux
17 + else
18 + set -euo pipefail
19 + fi
20 +
21 + # 配置
22 + GITHUB_TOKEN="${GITHUB_TOKEN:?GITHUB_TOKEN is required}"
23 + ORG_NAME="${1:?ORG_NAME is required}" # 组织名称;若为个人账号可传 '-',此时使用 "users/"
24 + REPO_NAME="${2:?REPO_NAME is required}" # 包所属的仓库名称
25 +
26 + if [ "$ORG_NAME" = "-" ]; then
27 + ORG_INFO="user/"
28 + else
29 + ORG_INFO="orgs/$ORG_NAME/"
30 + fi
31 +
32 + page=1
33 + while true; do
34 + echo "Fetching page $page of package versions..."
35 + versions_json=$(curl -s \
36 + -H "Authorization: token $GITHUB_TOKEN" \
37 + -H "Accept: application/vnd.github+json" \
38 + "https://api.github.com/${ORG_INFO}packages/container/$REPO_NAME/versions?per_page=100&page=$page")
39 +
40 + count=$(echo "$versions_json" | jq 'length')
41 + if [ "$count" -eq 0 ]; then
42 + echo "No more package versions to process."
43 + break
44 + fi
45 +
46 + echo "Processing $count versions on page $page..."
47 + # 遍历当前页面所有版本(使用 base64 避免 jq 中处理特殊字符问题)
48 + for version_enc in $(echo "$versions_json" | jq -r '.[] | @base64'); do
49 + # 定义一个辅助函数,便于解码 JSON 字段
50 + _jq() {
51 + echo "$version_enc" | base64 --decode | jq -r "${1}"
52 + }
53 +
54 + version_id=$(_jq '.id')
55 + tag_count=$(_jq '.metadata.container.tags | length')
56 +
57 + if [ "$tag_count" -eq 0 ]; then
58 + echo "Deleting untagged version: $version_id"
59 + http_code=$(curl -s -o /dev/null -w "%{http_code}" \
60 + -X DELETE \
61 + -H "Authorization: token $GITHUB_TOKEN" \
62 + -H "Accept: application/vnd.github+json" \
63 + "https://api.github.com/${ORG_INFO}packages/container/$REPO_NAME/versions/$version_id")
64 + if [ "$http_code" -eq 204 ]; then
65 + echo "Deleted version $version_id successfully."
66 + else
67 + echo "Failed to delete version $version_id. HTTP status: $http_code"
68 + fi
69 + else
70 + echo "Skipping version $version_id (tag count: $tag_count)"
71 + fi
72 + done
73 +
74 + page=$((page + 1))
75 + done
76 +
77 + echo "Done."

remove_github_workflow_runs.sh(vytvořil soubor)

@@ -0,0 +1,67 @@
1 + #!/usr/bin/env bash
2 +
3 + #============================================================
4 + # File: remove_github_workflow_runs.sh
5 + # Description: 批量删除 GitHub Action Workflows 流水线
6 + # URL: https://s.fx4.cn/
7 + # ORIGIN: https://gist.asfd.cn/jetsung/githubci/raw/HEAD/remove_github_workflow_runs.sh
8 + # Author: Jetsung Chan <[email protected]>
9 + # Version: 0.1.0
10 + # CreatedAt: 2025-08-18
11 + # UpdatedAt: 2025-08-18
12 + #============================================================
13 +
14 +
15 + if [[ -n "${DEBUG:-}" ]]; then
16 + set -eux
17 + else
18 + set -euo pipefail
19 + fi
20 +
21 + ORG_NAME=${1:?ORG_NAME is required}
22 + REPO_NAME=${2:?REPO_NAME is required}
23 +
24 + repo="$ORG_NAME/$REPO_NAME"
25 + url="repos/$repo/actions/runs"
26 +
27 + total_deleted=0
28 +
29 + delete_id() {
30 + local id=$1
31 + local result=""
32 +
33 + echo "Deleting URL: $url/$id"
34 + if gh api -X DELETE "$url/$id" --silent; then
35 + result="✅ Deleted '$id'"
36 + total_deleted=$((total_deleted + 1))
37 + else
38 + result="❌ Failed '$id'"
39 + echo "$result"
40 + echo "An error occurred while deleting ID '$id'. Press Enter to exit."
41 + echo "Total IDs deleted: $total_deleted"
42 + read -n 1 -s -r -p ""
43 + exit 1
44 + fi
45 +
46 + printf "%s\n" "$result"
47 + }
48 +
49 + while true; do
50 + total_ids=$(gh api "$url" | jq '.workflow_runs | length')
51 +
52 + if [[ $total_ids -eq 0 ]]; then
53 + echo "No more IDs to delete. Press Enter to exit."
54 + echo "Total IDs deleted: $total_deleted"
55 + read -n 1 -s -r -p ""
56 + break
57 + fi
58 +
59 + # 使用 process substitution 避免子 shell
60 + while read -r id; do
61 + id="${id//$'\r'/}" # 去掉回车符
62 + delete_id "$id"
63 + done < <(gh api "$url" | jq -r '.workflow_runs[].id')
64 +
65 + # 可选:等待几秒再继续循环,避免频繁调用 API
66 + # sleep 2
67 + done
Novější Starší