Skip to content

Commit

Permalink
GHA: use bash
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Feb 6, 2024
1 parent 457c702 commit 0008d73
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 84 deletions.
86 changes: 2 additions & 84 deletions .github/workflows/sandbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,91 +123,9 @@ jobs:
needs: install
if: always() && github.event_name != 'pull_request' && github.event.repository.fork == false
steps:
- uses: actions/checkout@v4
- name: Determine Workflow Conclusion
run: |
max_attempts=5
page=1
success_count=0
failure_count=0
cancelled_count=0
skipped_count=0
null_count=0
while :; do
success=false
for attempt in $(seq 1 $max_attempts); do
echo "Attempt $attempt of $max_attempts for page $page"
echo "Fetching job conclusions for page: $page"
response=$(curl -sS -H "Authorization: token ${{ secrets.GH_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs?page=$page&per_page=100")
if [ $? -eq 0 ]; then
echo "API Request successful."
echo "Processing job conclusions..."
job_conclusions=$(echo "$response" | jq -r '.jobs[].conclusion')
echo "Job conclusions: $job_conclusions"
IFS=$'\n'
for conclusion in $job_conclusions; do
echo "Processing conclusion: $conclusion"
case $conclusion in
success) ((success_count++)) ;;
failure) ((failure_count++)) ;;
cancelled) ((cancelled_count++)) ;;
skipped) ((skipped_count++)) ;;
null) ((null_count++)) ;;
esac
done
unset IFS
echo "Conclusion counts processed successfully."
success=true
break
else
echo "API Request failed, retrying in $((attempt * 2)) seconds..."
sleep $((attempt * 2))
fi
done
if [ "$success" = false ]; then
echo "Failed to fetch job conclusions after $max_attempts attempts."
echo "WORKFLOW_CONCLUSION=failure" >> $GITHUB_ENV
exit 0
fi
response_with_headers=$(curl -sS -I -H "Authorization: token ${{ secrets.GH_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs?page=$((page+1))&per_page=100")
link_header=$(echo "$response_with_headers" | grep -i '^Link:' | tr -d '\r')
if echo "$link_header" | grep -q 'rel="next"'; then
echo "Found next page, proceeding..."
page=$((page + 1))
else
echo "No more pages to fetch, finalizing..."
break
fi
done
echo "Determining overall workflow conclusion..."
echo "Success: $success_count, Failure: $failure_count, Cancelled: $cancelled_count, Skipped: $skipped_count, Null: $null_count"
if [ $cancelled_count -gt 0 ]; then
echo "Some jobs were cancelled."
WORKFLOW_CONCLUSION="cancelled"
elif [ $failure_count -gt 0 ]; then
echo "Some jobs failed."
WORKFLOW_CONCLUSION="failure"
elif [ $success_count -gt 0 ] && [ $null_count -eq 0 ]; then
echo "All jobs succeeded."
WORKFLOW_CONCLUSION="success"
else
echo "Defaulting to failure due to uncertain job conclusions."
WORKFLOW_CONCLUSION="failure"
fi
echo "WORKFLOW_CONCLUSION=$WORKFLOW_CONCLUSION" >> $GITHUB_ENV
echo "Workflow conclusion determined: $WORKFLOW_CONCLUSION"
exit 0
run: ./scripts/workflow-status.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} ${{ github.run_id }}

- uses: sarisia/actions-status-discord@v1
with:
Expand Down
73 changes: 73 additions & 0 deletions scripts/workflow-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash

# Pass GitHub token, repository, and run ID as arguments
GITHUB_TOKEN=$1
GITHUB_REPOSITORY=$2
GITHUB_RUN_ID=$3

max_attempts=5
page=1
declare -A conclusion_counts

# Initialize conclusions to avoid integer expression expected error
for status in success failure cancelled skipped; do
conclusion_counts[$status]=0
done

while :; do
success=false
for attempt in $(seq 1 $max_attempts); do
echo "Attempt $attempt of $max_attempts for page $page"

response_with_headers=$(curl -sS -I -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs?page=$page&per_page=100")

link_header=$(echo "$response_with_headers" | grep -i '^Link:' | tr -d '\r')

response=$(curl -sS -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs?page=$page&per_page=100")

if [ $? -eq 0 ]; then
echo "API Request successful."

while read -r line; do
((conclusion_counts[$line]++))
done < <(echo "$response" | jq -r '.jobs[].conclusion')

success=true
break # Break out of the retry loop
else
echo "API Request failed, retrying in $((attempt * 2)) seconds..."
sleep $((attempt * 2))
fi
done

if [ "$success" = false ]; then
echo "API requests failed after $max_attempts attempts, defaulting to failure."
exit 1 # Use 'exit 1' to indicate script failure
fi

# Check for next page using the Link header
if echo "$link_header" | grep -q 'rel="next"'; then
page=$((page + 1))
else
break # Exit loop if no next page is found
fi
done

# Determine overall workflow conclusion
if [ ${conclusion_counts[cancelled]} -gt 0 ]; then
echo "Some jobs were cancelled."
WORKFLOW_CONCLUSION="cancelled"
elif [ ${conclusion_counts[failure]} -gt 0 ]; then
echo "Some jobs failed."
WORKFLOW_CONCLUSION="failure"
elif [ ${conclusion_counts[success]} -gt 0 ]; then
echo "All jobs succeeded."
WORKFLOW_CONCLUSION="success"
else
echo "Defaulting to failure due to uncertain job conclusions."
WORKFLOW_CONCLUSION="failure"
fi

echo "WORKFLOW_CONCLUSION=$WORKFLOW_CONCLUSION"

0 comments on commit 0008d73

Please sign in to comment.