Последняя активность 1 month ago

GitHub Action

Версия 34e1a47403d62892c222d2b0cec3a814437e35ac

remove_github_deployments_run.sh Исходник
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
15if [[ -n "${DEBUG:-}" ]]; then
16 set -eux
17else
18 set -euo pipefail
19fi
20
21owner="$1" # 替换为仓库所有者的用户名
22repo="$2" # 替换为仓库名称
23
24# 获取所有部署记录的 ID,按创建时间降序排列
25deployment_ids=$(gh api "repos/$owner/$repo/deployments" --paginate --jq 'sort_by(.created_at) | reverse | .[].id')
26
27# 将部署 ID 保存到数组中
28readarray -t ids <<<"$deployment_ids"
29
30# 获取部署记录的数量
31num_deployments=${#ids[@]}
32
33# 如果部署记录少于2个,则无需删除
34if [[ "$num_deployments" -lt 2 ]]; then
35 echo "没有足够的部署记录可供删除。"
36 exit 0
37fi
38
39# 遍历部署 ID,跳过最新的一个
40for ((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
46done
47
48echo "旧的部署记录已删除,仅保留最新的一个。"
49
remove_github_packages_untagged.sh Исходник
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
15if [[ -n "${DEBUG:-}" ]]; then
16 set -eux
17else
18 set -euo pipefail
19fi
20
21# 配置
22GITHUB_TOKEN="${GITHUB_TOKEN:?GITHUB_TOKEN is required}"
23ORG_NAME="${1:?ORG_NAME is required}" # 组织名称;若为个人账号可传 '-',此时使用 "users/"
24REPO_NAME="${2:?REPO_NAME is required}" # 包所属的仓库名称
25
26if [ "$ORG_NAME" = "-" ]; then
27 ORG_INFO="user/"
28else
29 ORG_INFO="orgs/$ORG_NAME/"
30fi
31
32page=1
33while 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))
75done
76
77echo "Done."
78
remove_github_workflow_runs.sh Исходник
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
15if [[ -n "${DEBUG:-}" ]]; then
16 set -eux
17else
18 set -euo pipefail
19fi
20
21ORG_NAME=${1:?ORG_NAME is required}
22REPO_NAME=${2:?REPO_NAME is required}
23
24repo="$ORG_NAME/$REPO_NAME"
25url="repos/$repo/actions/runs"
26
27total_deleted=0
28
29delete_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
49while 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
67done
68