-
Notifications
You must be signed in to change notification settings - Fork 0
53 lines (43 loc) · 2.08 KB
/
check-urls.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
name: URL Status Check
on:
schedule:
- cron: "*/15 * * * *" # Run every 15 minutes
jobs:
check_urls:
runs-on: ubuntu-latest
steps:
- name: URL Status Check
run: |
# Define an array of URLs to check
urls=("https://example.com/api/v1/resource1" "https://example.com/api/v1/resource2" "https://example.com/api/v1/internal-resource")
# Prepare an empty results array to hold URL and status code
results=()
# Loop through each URL and make a GET request using curl
for url in "${urls[@]}"; do
# Make the GET request and capture the HTTP status code and any potential errors
status_code=$(curl -o /dev/null -s -w "%{http_code}" --connect-timeout 5 --max-time 10 "$url")
# Check if the curl command was successful
if [ $? -ne 0 ]; then
# If curl fails (e.g., connection timeout), set status code to 503 (unreachable)
if [ "$status_code" -eq "000" ]; then
echo "URL: $url is unreachable (timeout or connection failure). Setting response code to 503."
status_code=503
else
# If another error occurs (e.g., DNS resolution failure), set status code to 0
echo "URL: $url encountered an error. Setting response code to 0."
status_code=0
fi
fi
# Append the result with the actual or adjusted status code
echo "URL: $url, Status Code: $status_code"
results+=("{\"url\":\"$url\",\"response_code\":$status_code}")
done
# Join the results array into a JSON array string
results_json=$(printf "%s\n" "${results[@]}" | jq -s '.')
# Print the JSON to see the results
echo "Results: $results_json"
# Send the results to the specified endpoint using curl
post_results_url="https://your-api-endpoint.com/submit"
curl -X POST "$post_results_url" \
-H "Content-Type: application/json" \
-d "$results_json"