-
Notifications
You must be signed in to change notification settings - Fork 2
132 lines (118 loc) · 5.73 KB
/
deploy.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
name: Cloudflare Pages
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PROJECT_NAME: ${{ vars.CLOUDFLARE_PROJECT_NAME }}
CLOUDFLARE_DIRECTORY: _site
on:
push:
branches: ["**"]
pull_request_target:
permissions: {}
jobs:
pre-deployment:
if: ${{ vars.CLOUDFLARE_PROJECT_NAME != '' }}
outputs:
branch: ${{ steps.get.outputs.branch }}
environment: ${{ steps.get.outputs.environment }}
runs-on: ubuntu-latest
steps:
- id: get
run: |
if ${{ github.event_name == 'push' }}; then
echo "branch=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
elif ${{ github.event_name == 'pull_request_target' }}; then
echo "branch=pull/${{ github.event.number }}/merge" >> "$GITHUB_OUTPUT"
if ${{ github.event.pull_request.head.repo.html_url != github.event.repository.html_url }}; then
echo "environment=External PR" >> "$GITHUB_OUTPUT"
fi
else
exit 1
fi
deployment:
needs: ["pre-deployment"]
env:
BRANCH: ${{ needs.pre-deployment.outputs.branch }}
runs-on: ubuntu-latest
environment:
name: ${{ needs.pre-deployment.outputs.environment }}
url: ${{ steps.publish.outputs.url }}
permissions:
contents: read
deployments: write
pull-requests: write
steps:
- id: pr-checkout
if: startsWith( needs.pre-deployment.outputs.branch, 'pull/' ) && endsWith( needs.pre-deployment.outputs.branch, '/merge' )
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
with:
ref: refs/${{ env.BRANCH }}
- if: steps.pr-checkout.conclusion == 'skipped'
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
with:
ref: ${{ env.BRANCH }}
- run: mkdir _site && cp test _site/index.html
- id: publish
# This workflow does NOT work with v1.5.0
uses: cloudflare/pages-action@61eafe73baad0195ab582cb447b2c6e15a0df9ce # v1.4.1
with:
apiToken: ${{ env.CLOUDFLARE_API_TOKEN }}
accountId: ${{ env.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_NAME }}
directory: ${{ env.CLOUDFLARE_DIRECTORY }}
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ env.BRANCH }}
- if: ${{ ! cancelled() && github.event_name == 'pull_request_target' }}
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: DEPLOYMENT_COMMENT
- id: comment-data
if: steps.fc.conclusion == 'success'
run: |
head_sha=$(git rev-parse HEAD)
get_short_sha_with_link() {
echo "<a href='${{ github.event.repository.html_url }}/commit/$1'><code>$(echo "$1" | head -c 7)</code></a>"
}
echo "commit_text=$(get_short_sha_with_link $(git rev-parse HEAD)) ( base: $(get_short_sha_with_link ${{ github.event.pull_request.base.sha }}) + head: $(get_short_sha_with_link ${{ github.event.pull_request.head.sha }}) )" >> "$GITHUB_OUTPUT"
echo "pr_preview_url=$(echo '${{ steps.publish.outputs.url }}' | perl -pe "s|(//).+?(\.)|\$1$(echo '${{ env.BRANCH }}' | perl -pe 's|/|-|g')\$2|")" >> "$GITHUB_OUTPUT"
- if: steps.fc.conclusion == 'success'
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
<!-- DEPLOYMENT_COMMENT -->
<table><tr><td><strong>Latest commit:</strong> </td><td>
${{ steps.comment-data.outputs.commit_text }}
</td></tr>
<tr><td><strong>Status:</strong></td><td> ✅ Deploy successful!</td></tr>
<tr><td><strong>Preview URL:</strong></td><td>
<a href='${{ steps.publish.outputs.url }}'>${{ steps.publish.outputs.url }}</a>
</td></tr>
<tr><td><strong>PR Preview URL:</strong></td><td>
<a href='${{ steps.comment-data.outputs.pr_preview_url }}'>${{ steps.comment-data.outputs.pr_preview_url }}</a>
</td></tr>
</table>
[View workflow logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})
[View Cloudflare logs](https://dash.cloudflare.com/?to=/${{ env.CLOUDFLARE_ACCOUNT_ID }}/pages/view/${{ env.CLOUDFLARE_PROJECT_NAME }}/${{ steps.publish.outputs.id }})
edit-mode: replace
- uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
if: failure() && steps.fc.conclusion == 'success'
with:
token: ${{ secrets.GITHUB_TOKEN}}
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
<!-- DEPLOYMENT_COMMENT -->
<table><tr><td><strong>Latest commit:</strong> </td><td>
${{ steps.comment-data.outputs.commit_text }}
</td></tr>
<tr><td><strong>Status:</strong></td><td> ❌ Deploy failed!</td></tr>
</table>
[View workflow logs](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }})
[View Cloudflare logs](https://dash.cloudflare.com/?to=/${{ env.CLOUDFLARE_ACCOUNT_ID }}/pages/view/${{ env.CLOUDFLARE_PROJECT_NAME }}/${{ steps.publish.outputs.id }})
edit-mode: replace