-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
202 lines (177 loc) · 7.28 KB
/
action.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
name: 'Cloudflare Build Log'
description: 'Get your Cloudflare Build Logs in your Repository Pull Requests'
author: Jaydeep Das
branding:
icon: 'paperclip'
color: 'white'
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
GITHUB_TOKEN:
description: 'GitHub token for creating/updating comments'
required: true
COMMIT_SHA:
description: 'The SHA of the commit that triggered the workflow'
required: true
runs:
using: "composite"
steps:
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: <!-- CLOUDFLARE_PAGES_BUILD_LOGS -->
- name: Update comment with "Updating" status
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ inputs.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.fc.outputs.comment-id }}
body: |
<!-- CLOUDFLARE_PAGES_BUILD_LOGS -->
## Cloudflare Pages Build Status
| Key | Value |
| --- | ----- |
| Branch | ${{ github.head_ref }} |
| Commit | ${{ github.sha }} |
| Status | Updating... |
Build in progress. This comment will be updated once the build is complete.
edit-mode: replace
create-if-not-exists: true
- name: Poll Cloudflare and Fetch logs
shell: bash
env:
CLOUDFLARE_API_TOKEN: ${{ inputs.CLOUDFLARE_API_TOKEN }}
ACCOUNT_ID: ${{ inputs.ACCOUNT_ID }}
PROJECT_NAME: ${{ inputs.PROJECT_NAME }}
PR_BRANCH: ${{ github.head_ref }}
COMMIT_SHA: ${{ inputs.COMMIT_SHA }}
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
}
wait_for_build_completion() {
MAX_ATTEMPTS=30
ATTEMPT=0
SLEEP_TIME=20
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
DEPLOYMENT=$(fetch_latest_deployment)
STATUS=$(echo "$DEPLOYMENT" | jq -r '.latest_stage.status')
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failure" ]; then
echo "Build completed with status: $STATUS"
echo "BUILD_STATUS=$STATUS" >> $GITHUB_ENV
return 0
elif [ "$STATUS" = "canceled" ]; then
echo "Build was canceled"
echo "BUILD_STATUS=canceled" >> $GITHUB_ENV
return 1
fi
echo "Build still in progress. Waiting..."
sleep $SLEEP_TIME
ATTEMPT=$((ATTEMPT + 1))
done
echo "Timeout waiting for build to complete"
echo "BUILD_STATUS=timeout" >> $GITHUB_ENV
return 1
}
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 "Fetching deployment logs for deployment: $DEPLOYMENT_ID"
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")
if echo "$LOGS_RESPONSE" | jq -e '.success == true' > /dev/null; then
echo "Deployment Log Summary:"
echo "========================"
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 logs: Build did not complete successfully"
echo "logs=Failed to fetch logs: Build did not complete successfully" >> $GITHUB_ENV
fi
echo "BUILD_TIME=$(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_ENV
- name: Update comment with build results
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ inputs.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.fc.outputs.comment-id }}
body: |
<!-- CLOUDFLARE_PAGES_BUILD_LOGS -->
## Cloudflare Pages Build Logs
| Key | Value |
| --- | ----- |
| Branch | ${{ github.head_ref }} |
| Commit | ${{ inputs.COMMIT_SHA }} |
| Build Time | ${{ env.BUILD_TIME }} |
| Status | ${{ env.BUILD_STATUS }} |
<details>
<summary>Click to expand build logs</summary>
${{ env.logs }}
</details>
edit-mode: replace