一键脚本
bash <(curl -sL gitee.com/meimolihan/cmdbox/raw/master/sh/git_actions_status.sh)
bash <(curl -sL ../sh/git_actions_status.sh)
补充说明
git_actions_status 用于查询 GitHub Actions 中 release.yaml 流水线的最新运行记录,展示运行状态、提交标题、触发分支、触发事件、Run ID 和运行耗时,并提供实时跟踪、查看日志、取消构建、重新运行等快捷操作命令,方便发布后快速确认流水线结果。
需在已配置 GitHub 仓库的目录(含
.git)中运行,且本机已安装并登录gh(GitHub CLI)与jq。
功能特点
- 最新记录查询:自动拉取
release.yamlworkflow 最近一次运行记录(gh run list --limit 1) - 状态中文翻译:
in_progress→ 运行中,并自动拉取最终结果conclusion(成功/失败/已取消/已跳过) - 耗时计算:将 ISO8601 启动时间转换为已运行时长(秒/分/时,自动进位显示)
- 彩色输出:运行状态、提交标题、分支、事件、Run ID、耗时分类着色
- 快捷操作:输出
gh run watch/view/--log/--log-failed/cancel/rerun一键复制命令 - 优雅退出:按任意键后清屏退出
注意事项
- 需在 GitHub 仓库目录内运行,且该仓库配置了
release.yamlworkflow - 依赖
gh(GitHub CLI)且已完成gh auth login授权(repo 权限) - 依赖
jq解析 JSON,未安装时需先apt install jq - 依赖
date -d解析 ISO8601 时间(GNU date),macOS 需改用date -j -f - 若仓库没有 release.yaml 仅推送过 main(无
v*tag),gh run list返回空数组,脚本提示未找到记录 - 运行耗时按"当前时间 - 启动时间"计算,运行中的流水线耗时持续增长;已完成则显示总耗时
脚本源码
#!/bin/bash
set -uo pipefail
list_color_init() {
export gl_hui=$'\033[38;5;59m'
export gl_hong=$'\033[38;5;9m'
export gl_lv=$'\033[38;5;10m'
export gl_huang=$'\033[38;5;11m'
export gl_lan=$'\033[38;5;32m'
export gl_bai=$'\033[38;5;15m'
export gl_zi=$'\033[38;5;13m'
export gl_bufan=$'\033[38;5;14m'
export reset=$'\033[0m'
}
list_color_init
break_end() {
echo -e "\n${gl_lv}操作完成${gl_bai}"
echo -e "${gl_bai}按任意键退出 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
read -r -n 1 -s -r -p ""
echo ""
clear
exit 0
}
# 获取最新 release.yaml workflow 运行记录
get_gh_run_info() {
gh run list --workflow=release.yml --limit 1 --json status,displayTitle,headBranch,event,databaseId,startedAt
}
# ISO8601时间转秒数,计算耗时,增加容错
calc_elapsed() {
local start_iso="$1"
local start_ts
start_ts=$(date -d "${start_iso}" +%s 2>/dev/null)
if [[ -z "$start_ts" ]]; then
echo "时间解析失败"
return
fi
local now_ts=$(date +%s)
local diff=$(( now_ts - start_ts ))
if (( diff < 60 )); then
echo "${diff} 秒"
elif (( diff < 3600 )); then
echo "$((diff / 60)) 分 $((diff % 60)) 秒"
else
echo "$((diff / 3600)) 时 $(((diff % 3600)/60)) 分"
fi
}
beautify_gh_run() {
clear
echo -e "${gl_zi}>>> GitHub Actions Release 流水线信息${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
json_data=$(get_gh_run_info)
if [[ -z "$json_data" || "$json_data" == "[]" ]]; then
echo -e "${gl_hong}[错误] 未找到 release.yaml 流水线运行记录${reset}"
echo -e "${gl_bai}检查:gh auth status 确认gh已登录,仓库目录正确${reset}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
fi
# jq 解析合法字段
status=$(echo "$json_data" | jq -r '.[0].status')
title=$(echo "$json_data" | jq -r '.[0].displayTitle')
branch=$(echo "$json_data" | jq -r '.[0].headBranch')
event=$(echo "$json_data" | jq -r '.[0].event')
run_id=$(echo "$json_data" | jq -r '.[0].databaseId')
started_at=$(echo "$json_data" | jq -r '.[0].startedAt')
# 计算运行耗时
elapsed=$(calc_elapsed "$started_at")
# 状态翻译 + 颜色
case "$status" in
in_progress)
status_text="${gl_huang}运行中${reset}"
;;
completed)
# 拉取本次run的最终结果 conclusion
conclusion=$(gh run view "$run_id" --json conclusion | jq -r '.conclusion')
case "$conclusion" in
success) status_text="${gl_lv}成功${reset}";;
failure) status_text="${gl_hong}失败${reset}";;
cancelled) status_text="${gl_hui}已取消${reset}";;
skipped) status_text="${gl_huang}已跳过${reset}";;
*) status_text="${gl_huang}已完成(${conclusion})${reset}";;
esac
;;
*)
status_text="${gl_hui}${status}${reset}"
;;
esac
printf "%-14s%s\n" "${gl_hui}[运行状态]:${reset}" "$status_text"
printf "%-14s%s\n" "${gl_hui}[提交标题]:${reset}" "${gl_huang}$title${reset}"
printf "%-14s%s\n" "${gl_hui}[触发分支]:${reset}" "${gl_lan}$branch${reset}"
printf "%-14s%s\n" "${gl_hui}[触发事件]:${reset}" "${gl_bai}$event${reset}"
printf "%-14s%s\n" "${gl_hui}[Run ID]:${reset}" "${gl_bufan}$run_id${reset}"
printf "%-14s%s\n" "${gl_hui}[已耗时]:${reset}" "${gl_bai}$elapsed${reset}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
echo -e ""
echo -e "${gl_huang}>>> 快捷操作命令${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
echo -e "${gl_lv}实时跟踪流水线:${reset}gh run watch $run_id"
echo -e "${gl_lv}查看详细信息:${reset}gh run view $run_id"
echo -e "${gl_lv}查看完整日志:${reset}gh run view $run_id --log"
echo -e "${gl_lv}查看失败日志:${reset}gh run view $run_id --log-failed"
echo -e "${gl_lv}取消本次构建:${reset}gh run cancel $run_id"
echo -e "${gl_lv}重新运行流水线:${reset}gh run rerun $run_id"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
}
beautify_gh_run
快捷操作命令
脚本输出后可直接使用以下命令(<run_id> 为脚本显示的 Run ID):
| 操作 | 命令 |
|---|---|
| 实时跟踪流水线 | gh run watch <run_id> |
| 查看详细信息 | gh run view <run_id> |
| 查看完整日志 | gh run view <run_id> --log |
| 查看失败日志 | gh run view <run_id> --log-failed |
| 取消本次构建 | gh run cancel <run_id> |
| 重新运行流水线 | gh run rerun <run_id> |
创建本地脚本
cat > /usr/local/bin/git_actions_status.sh << 'EOF'
#!/bin/bash
# 将上方脚本源码完整粘贴至此
EOF
chmod +x /usr/local/bin/git_actions_status.sh
/usr/local/bin/git_actions_status.sh