-
Notifications
You must be signed in to change notification settings - Fork 0
178 lines (161 loc) · 7.88 KB
/
cd-beta.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
name: CD(beta)
run-name: CD (beta) - ${{ github.event_name }}
on:
# push:
pull_request:
branches:
- develop
workflow_dispatch:
env:
PROJECT_NAME: ${{ vars.CLOUDFLARE_BETA_PROJECT_NAME }}
PRODUCTION_BRANCH: develop
jobs:
build_and_deploy:
if: |
(github.event_name == 'push' && ((github.head_ref || github.ref_name) != 'develop' )) ||
(github.event_name == 'pull_request' && github.event.pull_request.merged == true) ||
github.event_name == 'workflow_dispatch'
runs-on: [self-hosted, portainer]
permissions:
contents: read
deployments: write
name: Publish to Cloudflare
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
- name: Symlink
run: |
mkdir -p /home/runner/work/${{ github.event.repository.name }}/
ln -s ${{ github.workspace }} /home/runner/work/${{ github.event.repository.name }}/${{ github.event.repository.name }}
- uses: actions/setup-node@v4
with:
node-version: "latest"
- name: cache
uses: actions/cache@v4
with:
# See here for caching with `yarn` https://github.com/actions/cache/blob/main/examples.md#node---yarn or you can leverage caching with actions/setup-node https://github.com/actions/setup-node
path: |
~/.npm
/home/runner/work/${{ github.event.repository.name }}/${{ github.event.repository.name }}/.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}
- name: build
run: npx @cloudflare/next-on-pages@1
env:
NEXT_PUBLIC_API_ENDPOINT: ${{vars.BETA_API_ENDPOINT}}
NEXT_PUBLIC_FIREBASE_API_KEY: ${{vars.BETA_FIREBASE_API_KEY}}
NEXT_PUBLIC_FIREBASE_APP_ID: ${{vars.BETA_FIREBASE_APP_ID}}
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${{vars.BETA_FIREBASE_AUTH_DOMAIN}}
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${{vars.BETA_FIREBASE_MESSAGING_SENDER_ID}}
NEXT_PUBLIC_FIREBASE_PROJECT_ID: ${{vars.BETA_FIREBASE_PROJECT_ID}}
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: ${{vars.BETA_FIREBASE_STORAGE_BUCKET}}
- name: set production branch
run: |
result=$(curl -sS -X PATCH \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}' \
-d '{ "production_branch": "${{ env.PRODUCTION_BRANCH }}" }' \
https://api.cloudflare.com/client/v4/accounts/${{ vars.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ env.PROJECT_NAME }})
result=$(echo "$result" | jq -r '
if .success == true then
"✅ Cloudflare Pages Production Branchの変更に成功しました。\n✨ \(.result.latest_deployment.production_branch)から\(.result.production_branch)に変更されました"
else
"❌ Cloudflare Pages Production Branchの変更に失敗しました。\n📝エラーコード: \(.errors[].code) \nエラーメッセージ: \(.errors[].message)"
end
')
echo "$result"
{ echo 'summary<<EOF'
echo "$result"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
id: set-production-branch
- name: Publish to Cloudflare Pages
if: steps.set-production-branch.outcome == 'success'
uses: cloudflare/wrangler-action@v3
with:
# Explicit branch name needed because of: https://github.com/cloudflare/pages-action/issues/97
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy .vercel/output/static --project-name=${{ env.PROJECT_NAME }} --branch=${{ github.head_ref || github.ref_name }}
id: publish
- name: generate alias url
id: generate-alias-url
if: ${{ (github.head_ref || github.ref_name) != env.PRODUCTION_BRANCH }}
env:
BRANCH: ${{ github.ref_name }}
PUBLISH_CFPAGES_URL: ${{ steps.publish.outputs.deployment-url }}
run: |
[[ $PUBLISH_CFPAGES_URL =~ (https?://)?([a-zA-Z0-9-]+)\.pages\.dev ]]
invalid_chars_regex='[^a-z0-9-]'
max_alias_length=28
alphanum="abcdefghijklmnopqrstuvwxyz0123456789"
function generate_branch_alias() {
local branch="$1"
local normalised=$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed -e "s/$invalid_chars_regex/-/g" -e "s/^-//" -e "s/-\$//")
if [ -z "$normalised" ]; then
echo "branch-$(rand_alphanum 10)"
return
fi
if [ ${#normalised} -gt $max_alias_length ]; then
echo "${normalised:0:$max_alias_length}"
return
fi
echo "$normalised"
}
function rand_alphanum() {
local length="$1"
local alias=""
for ((i=0; i<$length; i++)); do
alias+="${alphanum:RANDOM%${#alphanum}:1}"
done
}
new_url="https://$(generate_branch_alias "${BRANCH}").${BASH_REMATCH[2]}.pages.dev"
echo $new_url
echo alias-url=$new_url >> $GITHUB_OUTPUT
- name: Create Job Summary
uses: actions/github-script@v7
env:
CHANGE_BRANCH: ${{ steps.set-production-branch.outputs.summary }}
PUBLISH_CFPAGES: ${{ steps.publish.outputs.command-output }}
PUBLISH_CFPAGES_URL: ${{ steps.publish.outputs.deployment-url }}
PUBLISH_CFPAGES_ALIAS: ${{ steps.generate-alias-url.outputs.alias-url }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `\n#### Cloudflare Pages set production branch ⚙️\`${{ steps.set-production-branch.outcome }}\`\n
#### Cloudflare Pages Deploy 📖\`${{ steps.publish.outcome }}\`\n
🔗 Preview URL: [${process.env.PUBLISH_CFPAGES_URL}](${process.env.PUBLISH_CFPAGES_URL})\n
${process.env.PUBLISH_CFPAGES_ALIAS ?
`📌 Branch Preview URL: [${process.env.PUBLISH_CFPAGES_ALIAS}](${process.env.PUBLISH_CFPAGES_ALIAS})\n`:""
}
commit hash: \`${{ github.event.head_commit.id }}\`\n
commit message: [${{ github.event.head_commit.message}}](${{ github.server_url }}/${{ github.repository }}/tree/${{ github.event.head_commit.id}})\n
commit author: \`${{ github.event.head_commit.author.name }}\`\n
commit date: \`${{ github.event.head_commit.timestamp }}\`\n
<details><summary>Deploying with Cloudflare Pages</summary>
\`\`\`deploy\n
${process.env.CHANGE_BRANCH}
${process.env.PUBLISH_CFPAGES}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
await core.summary
.addHeading('Cloudflare Pages Deploy report')
.addRaw(output)
.write()
- name: Notify
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
content: "status: ${{ job.status }} \n🔗 Preview URL: ${{ steps.publish.outputs.deployment-url }}"
title: "${{ github.workflow }}"
description: "Build and deploy status of sos24-client-production."
color: 0x6600CC
url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
username: GitHub Actions
avatar_url: https://r2.sohosai.com/logo.png