generated from namidapoo/next15-shadcn-use-bun
-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (119 loc) · 5.88 KB
/
deploy-info-comment-on-pr.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
name: Deployment Info Comment on PR
on:
pull_request:
types: [opened]
jobs:
comment_on_pr:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ github.token }}
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Wait for 3 seconds
run: sleep 3
- name: Check if Deploy Workflow is Running
id: check_deploy
run: |
runs=$(gh run list --branch "${{ github.head_ref }}" \
--workflow "Deploy to Cloudflare Pages" \
--json status \
--jq '.[] | select(.status == "in_progress")' 2>/dev/null)
if [[ -n "$runs" ]]; then
echo "Deploy workflow is currently running. Exiting."
echo "DEPLOY_STATUS=running" >> $GITHUB_ENV
exit 0
else
echo "DEPLOY_STATUS=completed" >> $GITHUB_ENV
fi
- name: Add to Job Summaries if Deployment Running
if: env.DEPLOY_STATUS == 'running'
run: echo "✅ This workflow ended without executing anything because the deployment workflow was running." >> "$GITHUB_STEP_SUMMARY"
- name: Get Latest Commit Hash
if: env.DEPLOY_STATUS == 'completed'
id: get_commit
run: |
COMMIT_HASH=$(gh pr view ${{ github.event.pull_request.number }} --json commits -q ".commits[-1].oid")
echo "COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
SHORT_COMMIT_HASH=${COMMIT_HASH:0:7}
echo "SHORT_COMMIT_HASH=${SHORT_COMMIT_HASH}" >> $GITHUB_ENV
- name: Output Latest Commit Hash
if: env.DEPLOY_STATUS == 'completed'
run: echo "Latest Commit Hash ... ${{ env.COMMIT_HASH }}"
- name: Fetch latest deployment info and match with commit hash
if: env.DEPLOY_STATUS == 'completed'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PROJECT_NAME: ${{ secrets.CLOUDFLARE_PROJECT_NAME }}
run: |
response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$CLOUDFLARE_PROJECT_NAME/deployments" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
matching_deployment=$(echo "$response" | jq -r --arg commit "${{ env.COMMIT_HASH }}" '.result[] | select(.deployment_trigger.metadata.commit_hash == $commit)')
if [ -z "$matching_deployment" ]; then
echo "No deployment found for the latest commit hash."
exit 1
fi
deployment_url=$(echo "$matching_deployment" | jq -r '.url')
deployment_alias_url=$(echo "$matching_deployment" | jq -r '.aliases[0]')
pages_deployment_id=$(echo "$matching_deployment" | jq -r '.id')
echo "Matched Deploy URL: $deployment_url"
echo "Deploy Alias URL: $deployment_alias_url"
echo "Deploy ID: $pages_deployment_id"
echo "LATEST_DEPLOY_URL=$deployment_url" >> $GITHUB_ENV
echo "DEPLOY_ALIAS_URL=$deployment_alias_url" >> $GITHUB_ENV
echo "DEPLOY_ID=$pages_deployment_id" >> $GITHUB_ENV
- name: Create Comment Content
if: env.DEPLOY_STATUS == 'completed'
run: |
{
echo "GH_SAMPLE_COMMENT<<EOF"
echo "<!-- DEPLOYMENT_COMMENT -->"
echo "## Deploying ${{ secrets.CLOUDFLARE_PROJECT_NAME }} with <a href='https://pages.dev'><img alt='Cloudflare Pages' src='https://user-images.githubusercontent.com/23264/106598434-9e719e00-654f-11eb-9e59-6167043cfa01.png' width='16'></a> Cloudflare Pages"
echo ""
echo "<table><tr><td><strong>Latest commit:</strong> </td><td>"
echo "<code>${{ env.SHORT_COMMIT_HASH }}</code>"
echo "</td></tr>"
echo "<tr><td><strong>Status:</strong></td><td> ✅ Deploy successful!</td></tr>"
echo "<tr><td><strong>Preview URL:</strong></td><td>"
echo "<a href='${{ env.LATEST_DEPLOY_URL }}'>${{ env.LATEST_DEPLOY_URL }}</a>"
echo "</td></tr>"
echo "<tr><td><strong>Branch Preview URL:</strong></td><td>"
echo "<a href='${{ env.DEPLOY_ALIAS_URL }}'>${{ env.DEPLOY_ALIAS_URL }}</a>"
echo "</td></tr>"
echo "</table>"
echo ""
echo "[View logs](https://dash.cloudflare.com/?to=/:account/pages/view/${{ secrets.CLOUDFLARE_PROJECT_NAME }}/${{ env.DEPLOY_ID }})"
echo "EOF"
} >> "$GITHUB_ENV"
- name: Add or Update Comment on Pull Request
if: env.DEPLOY_STATUS == 'completed'
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
# Fetch existing comments
COMMENTS=$(gh api repos/${{ github.repository }}/issues/${PR_NUMBER}/comments --jq '.[] | @base64')
COMMENT_ID=""
for COMMENT in $COMMENTS; do
COMMENT_JSON=$(echo $COMMENT | base64 --decode)
BODY=$(echo $COMMENT_JSON | jq -r '.body')
ID=$(echo $COMMENT_JSON | jq -r '.id')
if echo "$BODY" | grep -q '<!-- DEPLOYMENT_COMMENT -->'; then
COMMENT_ID=$ID
break
fi
done
if [ -n "$COMMENT_ID" ]; then
echo "Updating existing comment ID: $COMMENT_ID"
gh api \
--method PATCH \
-H "Accept: application/vnd.github.v3+json" \
/repos/${{ github.repository }}/issues/comments/$COMMENT_ID \
-f body="${{ env.GH_SAMPLE_COMMENT }}"
else
echo "Creating new comment"
gh pr comment $PR_NUMBER --body "${{ env.GH_SAMPLE_COMMENT }}"
fi
- name: Add to Job Summaries
if: env.DEPLOY_STATUS == 'completed'
run: echo "${{ env.GH_SAMPLE_COMMENT }}" >> "$GITHUB_STEP_SUMMARY"