Skip to content

Commit

Permalink
GHA: port Saltbox status script
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Mar 13, 2024
1 parent d91593c commit 11b24e3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ jobs:
needs: build
if: always()
steps:
- uses: technote-space/workflow-conclusion-action@v3
- name: Send Discord Notification
uses: sarisia/actions-status-discord@v1
- uses: actions/checkout@v4
- name: Determine Workflow Conclusion
run: ./scripts/workflow-status.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} ${{ github.run_id }}

- uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
description: "URL: ${{ needs.build.outputs.url }}"
status: ${{ env.WORKFLOW_CONCLUSION }}
9 changes: 5 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ jobs:
needs: build
if: always() && github.event_name != 'pull_request' && github.event.repository.fork == false
steps:
- uses: technote-space/workflow-conclusion-action@v3
- name: Send Discord Notification
uses: sarisia/actions-status-discord@v1
- uses: actions/checkout@v4
- name: Determine Workflow Conclusion
run: ./scripts/workflow-status.sh ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} ${{ github.run_id }}

- uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
description: "URL: ${{ needs.build.outputs.url }}"
status: ${{ env.WORKFLOW_CONCLUSION }}
76 changes: 76 additions & 0 deletions scripts/workflow-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/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 unknown; 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."

job_conclusions=$(echo "$response" | jq -r '.jobs[].conclusion')
for line in $job_conclusions; do
# Handle null or unexpected values
if [[ -z "$line" || "$line" == "null" ]]; then
((conclusion_counts[unknown]++))
else
((conclusion_counts[$line]++))
fi
done

success=true
break
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
fi

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

# Determine overall workflow conclusion
if [ ${conclusion_counts[cancelled]} -gt 0 ]; then
WORKFLOW_CONCLUSION="cancelled"
elif [ ${conclusion_counts[failure]} -gt 0 ]; then
WORKFLOW_CONCLUSION="failure"
elif [ ${conclusion_counts[success]} -gt 0 ]; then
WORKFLOW_CONCLUSION="success"
else
WORKFLOW_CONCLUSION="failure"
fi

# Export WORKFLOW_CONCLUSION to GitHub Actions environment
echo "WORKFLOW_CONCLUSION=$WORKFLOW_CONCLUSION" >> $GITHUB_ENV

0 comments on commit 11b24e3

Please sign in to comment.