Skip to content

Commit

Permalink
update action
Browse files Browse the repository at this point in the history
  • Loading branch information
JDeepD committed Oct 12, 2024
1 parent dff7b7b commit 575ca18
Showing 1 changed file with 114 additions and 101 deletions.
215 changes: 114 additions & 101 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,119 +1,132 @@
name: cloudflare-build-log
description: Get your Cloudflare Build Logs in your Repository Pull Requests
name: 'Cloudflare Build Log'
description: 'Get your Cloudflare Build Logs in your Repository Pull Requests'
author: Jaydeep Das
branding:
icon: 'paperclip'
color: 'white'

jobs:
- name: Poll Cloudflare and Fetch logs
id: fetch_logs
run: |
fetch_latest_deployment() {
RESPONSE=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
if echo "$RESPONSE" | jq -e '.success == true' > /dev/null; then
LATEST_DEPLOYMENT=$(echo "$RESPONSE" | jq -c --arg branch "$PR_BRANCH" '.result | map(select(.deployment_trigger.metadata.branch == $branch)) | max_by(.created_on)')
echo "$LATEST_DEPLOYMENT"
else
echo "Cloudflare API request failed. Response:"
echo "$RESPONSE" | jq '.'
exit 1
fi
}
inputs:
CLOUDFLARE_API_TOKEN:
description: 'Cloudflare API Token for authentication'
required: true
ACCOUNT_ID:
description: 'Cloudflare Account ID'
required: true
PROJECT_NAME:
description: 'Cloudflare Pages project name'
required: true

wait_for_build_completion() {
MAX_ATTEMPTS=30
ATTEMPT=0
SLEEP_TIME=20
runs:
using: "composite"
steps:
- name: Poll Cloudflare and Fetch logs
shell: bash
run: |
fetch_latest_deployment() {
RESPONSE=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
if echo "$RESPONSE" | jq -e '.success == true' > /dev/null; then
LATEST_DEPLOYMENT=$(echo "$RESPONSE" | jq -c --arg branch "$PR_BRANCH" '.result | map(select(.deployment_trigger.metadata.branch == $branch)) | max_by(.created_on)')
echo "$LATEST_DEPLOYMENT"
else
echo "Cloudflare API request failed. Response:"
echo "$RESPONSE" | jq '.'
exit 1
fi
}
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
DEPLOYMENT=$(fetch_latest_deployment)
STATUS=$(echo "$DEPLOYMENT" | jq -r '.latest_stage.status')
wait_for_build_completion() {
MAX_ATTEMPTS=30
ATTEMPT=0
SLEEP_TIME=20
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failed" ]; then
echo "Build completed with status: $STATUS"
return 0
elif [ "$STATUS" = "canceled" ]; then
echo "Build was canceled"
return 1
fi
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
DEPLOYMENT=$(fetch_latest_deployment)
STATUS=$(echo "$DEPLOYMENT" | jq -r '.latest_stage.status')
echo "Build still in progress. Waiting..."
sleep $SLEEP_TIME
ATTEMPT=$((ATTEMPT + 1))
done
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failed" ]; then
echo "Build completed with status: $STATUS"
return 0
elif [ "$STATUS" = "canceled" ]; then
echo "Build was canceled"
return 1
fi
echo "Timeout waiting for build to complete"
return 1
}
echo "Build still in progress. Waiting..."
sleep $SLEEP_TIME
ATTEMPT=$((ATTEMPT + 1))
done
fetchlogs() {
DEPLOYMENT=$1
if [ -z "$DEPLOYMENT" ]; then
echo "No deployments found for branch: $PR_BRANCH."
exit 0
fi
DEPLOYMENT_ID=$(echo "$DEPLOYMENT" | jq -r '.id')
echo "\`\`\`"
echo "Timeout waiting for build to complete"
return 1
}
echo "Fetching deployment logs for deployment: $DEPLOYMENT_ID"
fetchlogs() {
DEPLOYMENT=$1
if [ -z "$DEPLOYMENT" ]; then
echo "No deployments found for branch: $PR_BRANCH."
exit 0
fi
DEPLOYMENT_ID=$(echo "$DEPLOYMENT" | jq -r '.id')
echo "\`\`\`"
LOGS_RESPONSE=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments/$DEPLOYMENT_ID/history/logs" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
echo "Fetching deployment logs for deployment: $DEPLOYMENT_ID"
if echo "$LOGS_RESPONSE" | jq -e '.success == true' > /dev/null; then
echo "Deployment Log Summary:"
echo "========================"
LOGS_RESPONSE=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME/deployments/$DEPLOYMENT_ID/history/logs" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
TOTAL_LOGS=$(echo "$LOGS_RESPONSE" | jq -r '.result.total')
echo "Total log entries: $TOTAL_LOGS"
echo ""
if echo "$LOGS_RESPONSE" | jq -e '.success == true' > /dev/null; then
echo "Deployment Log Summary:"
echo "========================"
echo "$LOGS_RESPONSE" | jq -r '.result.data[] | .line' | while read -r message; do
case "$message" in
"Cloning repository..."*)
echo -e "\n[STEP] $message"
;;
"Success: Finished cloning repository files")
echo -e "[SUCCESS] $message\n"
;;
"Installing project dependencies:"*)
echo -e "\n[STEP] $message"
;;
"Error:"*|"Failed:"*)
echo -e "\n[ERROR] $message"
;;
*"ERR_PNPM"*)
echo -e "[ERROR] $message$"
;;
"Detected the following tools"*)
echo -e "\n[INFO] $message"
;;
*)
echo "$message"
;;
esac
done
TOTAL_LOGS=$(echo "$LOGS_RESPONSE" | jq -r '.result.total')
echo "Total log entries: $TOTAL_LOGS"
echo ""
echo "$LOGS_RESPONSE" | jq -r '.result.data[] | .line' | while read -r message; do
case "$message" in
"Cloning repository..."*)
echo -e "\n[STEP] $message"
;;
"Success: Finished cloning repository files")
echo -e "[SUCCESS] $message\n"
;;
"Installing project dependencies:"*)
echo -e "\n[STEP] $message"
;;
"Error:"*|"Failed:"*)
echo -e "\n[ERROR] $message"
;;
*"ERR_PNPM"*)
echo -e "[ERROR] $message$"
;;
"Detected the following tools"*)
echo -e "\n[INFO] $message"
;;
*)
echo "$message"
;;
esac
done
else
echo "Failed to fetch deployment logs. API Response:"
echo "$LOGS_RESPONSE" | jq '.'
fi
echo "\`\`\`"
}
if wait_for_build_completion; then
latest_deployment=$(fetch_latest_deployment)
BUILD_LOGS=$(fetchlogs "$latest_deployment")
echo "logs<<EOF" >> $GITHUB_ENV
echo "$BUILD_LOGS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "Failed to fetch deployment logs. API Response:"
echo "$LOGS_RESPONSE" | jq '.'
echo "Failed to fetch logs: Build did not complete successfully"
exit 1
fi
echo "\`\`\`"
}
if wait_for_build_completion; then
latest_deployment=$(fetch_latest_deployment)
BUILD_LOGS=$(fetchlogs "$latest_deployment")
echo "logs<<EOF" >> $GITHUB_ENV
echo "$BUILD_LOGS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "Failed to fetch logs: Build did not complete successfully"
exit 1
fi

0 comments on commit 575ca18

Please sign in to comment.