Skip to content

Commit ea9fff8

Browse files
authored
Initial commit
0 parents  commit ea9fff8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+19097
-0
lines changed

.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

.github/workflows/deploy.yml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Deploy to Cloudflare
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
workflow_dispatch:
7+
8+
jobs:
9+
build-and-deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Setup Terraform
15+
uses: hashicorp/[email protected]
16+
with:
17+
terraform_version: 1.6.4
18+
19+
- name: Use Node.js 18.x
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: 18.x
23+
cache: 'npm'
24+
25+
# Automatically get an account id via the API Token
26+
# if secrets.CLOUDFLARE_ACCOUNT_ID is not set.
27+
- name: Fetch Account ID
28+
id: fetch_account_id
29+
run: |
30+
if [[ -n "${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" ]]; then
31+
ACCOUNT_ID="${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"
32+
echo "Using provided CLOUDFLARE_ACCOUNT_ID from secrets."
33+
else
34+
ACCOUNT_ID=$(curl -X GET "https://api.cloudflare.com/client/v4/accounts" -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" -H "Content-Type:application/json" | jq ".result[0].id" -r)
35+
if [[ "$ACCOUNT_ID" == "null" ]]; then
36+
echo "Failed to get an account id, please make sure you have set up CLOUDFLARE_API_TOKEN correctly!"
37+
exit 1
38+
fi
39+
fi
40+
echo 'account_id='$ACCOUNT_ID >> $GITHUB_OUTPUT
41+
42+
env:
43+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
44+
45+
# This is a temporary workaround to fix issue #13
46+
# On a new Cloudflare account, the terraform apply will fail with `workers.api.error.subdomain_required`
47+
# This may be due to the account not having a worker subdomain yet, so we create a dummy worker and then delete it.
48+
# Cloudflare should allocate a worker subdomain after this.
49+
# https://github.com/cloudflare/terraform-provider-cloudflare/issues/3304
50+
- name: Create worker subdomain
51+
id: create_dummy_worker
52+
run: |
53+
curl --request PUT --fail-with-body \
54+
--url https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/dummy-ib4db6ntj5csdef3 \
55+
--header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN \
56+
--header 'Content-Type: application/javascript' \
57+
--data 'addEventListener('\''fetch'\'', (event) => event.respondWith(new Response('\''OK'\'')))'\
58+
59+
curl --request DELETE --fail-with-body \
60+
--url https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/dummy-ib4db6ntj5csdef3 \
61+
--header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN \
62+
--header 'Content-Type: application/json'
63+
env:
64+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
65+
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
66+
67+
- name: Install packages
68+
run: |
69+
npm install
70+
cd worker
71+
npm install
72+
73+
- name: Build worker
74+
run: |
75+
cd worker
76+
npx wrangler deploy src/index.ts --outdir dist --dry-run
77+
78+
- name: Build page
79+
run: |
80+
npx @cloudflare/next-on-pages
81+
82+
- name: Deploy using Terraform
83+
# We're using terraform for first-time setup here,
84+
# since we didn't setup a remote backend to store state,
85+
# following runs will fail with name conflict, which is normal.
86+
continue-on-error: true
87+
88+
run: |
89+
terraform init
90+
terraform apply -auto-approve -input=false
91+
env:
92+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
93+
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
94+
95+
# Still need to upload worker to keep it up-to-date (Terraform will fail after first-time setup)
96+
- name: Upload worker
97+
run: |
98+
curl --fail-with-body -X PUT https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/uptimeflare_worker/content --header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN -F 'index.js=@worker/dist/index.js;type=application/javascript+module' -F 'metadata={"main_module": "index.js"}'
99+
env:
100+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
101+
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
102+
103+
# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
104+
- name: Upload pages
105+
run: |
106+
npx wrangler pages deploy .vercel/output/static --project-name uptimeflare
107+
env:
108+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
109+
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}

.github/workflows/sync.yaml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Upstream Sync
2+
3+
permissions:
4+
contents: write
5+
actions: write
6+
7+
on:
8+
workflow_dispatch:
9+
inputs:
10+
manual-trigger:
11+
description: 'This will pull the upstream code while preserving your configuration file. Configuration file compatibility is not guaranteed, and you may need to manually update the configuration file after pulling.'
12+
type: boolean
13+
required: true
14+
default: false
15+
override-token:
16+
description: '[Optional] Replace the default token with the supplied PAT. Used to resolve `refusing to allow a GitHub App to create or update workflow... `'
17+
required: false
18+
19+
jobs:
20+
sync_latest_from_upstream:
21+
name: Sync latest commits from upstream repo
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- name: Checkout target repo
26+
uses: actions/checkout@v3
27+
with:
28+
token: ${{ inputs.override-token || secrets.GITHUB_TOKEN }}
29+
30+
- name: Current config
31+
run: |
32+
cat uptime.config.ts
33+
cp uptime.config.ts /tmp/origin.config.ts
34+
35+
- name: Sync upstream changes
36+
id: sync
37+
run: |
38+
git config user.name "github-actions[bot]"
39+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
40+
41+
# Fetch latest code
42+
git clone https://github.com/lyc8503/UptimeFlare /tmp/latest
43+
rm -rf /tmp/latest/.git
44+
45+
# Clean current repo and update
46+
git rm -rf '*'
47+
cp -r /tmp/latest/. .
48+
cp /tmp/origin.config.ts uptime.config.ts
49+
git add .
50+
git commit -m "Sync latest code from upstream"
51+
git push
52+
53+
- name: Trigger deployment
54+
uses: benc-uk/workflow-dispatch@v1
55+
with:
56+
workflow: deploy.yml

.gitignore

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts
36+
37+
.terraform/
38+
terraform.tfstate*
39+
40+
/.wrangler

.prettierrc.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trailingComma: 'es5'
2+
tabWidth: 2
3+
semi: false
4+
singleQuote: true
5+
printWidth: 100

.terraform.lock.hcl

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)