Skip to content

Check API and Notify on Failure #1361

Check API and Notify on Failure

Check API and Notify on Failure #1361

name: Check API and Notify on Failure
on:
push:
branches: ["main"]
schedule:
- cron: '0 * * * *'
jobs:
check-api:
runs-on: ubuntu-latest
steps:
- name: Check APIs status
id: check_status
run: |
apis=()
while IFS= read -r line || [ -n "$line" ]; do
apis+=("$line")
done <<< "${{ secrets.NEED_CHECK_URLS }}"
for api in "${apis[@]}"; do
echo "Accessing API with element $api"
response=$(curl -o /dev/null -s -w "%{http_code}\n" "$api")
if [ "$response" != "200" ] && [ "$response" != "301" && [ "$response" != "302" ]; then
echo "HTTP状态码不等于200且不等于301/302,发送通知..."
curl -X POST \
-H 'Content-Type: application/json' \
-d '{
"msg_type": "text",
"content": {
"text": "'"$api"': API check failed. Status code: '"$response"'"
}
}' "${{ secrets.FEISHU_WEBHOOK }}"
fi
done