common-workflows/actions/api-test/action.yml
2025-12-19 10:38:41 +00:00

75 lines
2.1 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: 'Reusable API Health Test'
description: '通用的 API /health 检查逻辑,可在各仓库中复用,支持传入完整 URL不含 /health'
inputs:
urls:
description: '要检查的服务 URL 列表,每行一个完整 URL不包含 /health例如 http://127.0.0.1:5800 或 https://service-name'
required: true
runs:
using: 'composite'
steps:
- name: Check API services
shell: bash
env:
URLS: ${{ inputs.urls }}
run: |
echo "====== 开始检查各 API 服务 /health ======"
failed_services=""
api_failed=0
# 按行读取 URL每行是一个完整的基础 URL不包含 /health
while IFS= read -r url; do
# 跳过空行
if [ -z "$url" ]; then
continue
fi
# 去掉末尾的 /,避免出现 //health
url_no_slash="${url%/}"
health_url="${url_no_slash}/health"
echo ""
echo "------ 检查服务 ${health_url} ------"
success=0
for i in {1..10}; do
if curl -fsS "${health_url}" > /dev/null; then
success=1
break
fi
echo "第 ${i} 次重试,等待 5 秒..."
sleep 5
done
if [ "${success}" -ne 1 ]; then
echo "✗ 服务 ${health_url} /health 检查失败"
failed_services="${failed_services} ${health_url}"
else
echo "✓ 服务 ${health_url} /health 正常"
fi
done <<< "$URLS"
if [ -n "${failed_services}" ]; then
echo ""
echo "✗ 以下服务健康检查失败:"
for s in ${failed_services}; do
echo " - ${s}"
done
api_failed=1
else
echo ""
echo "====== 所有 API 服务健康检查通过 ======"
fi
if [ "${api_failed}" -ne 0 ]; then
echo ""
echo "✗ API /health 检查存在失败,详见上方日志"
exit 1
fi
echo ""
echo "✓ 所有 API /health 检查通过"