Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Niu-820 authored Nov 17, 2024
0 parents commit da8fa8f
Show file tree
Hide file tree
Showing 45 changed files with 19,097 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
109 changes: 109 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: Deploy to Cloudflare

on:
push:
branches: ['main']
workflow_dispatch:

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2.0.3
with:
terraform_version: 1.6.4

- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: 'npm'

# Automatically get an account id via the API Token
# if secrets.CLOUDFLARE_ACCOUNT_ID is not set.
- name: Fetch Account ID
id: fetch_account_id
run: |
if [[ -n "${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" ]]; then
ACCOUNT_ID="${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"
echo "Using provided CLOUDFLARE_ACCOUNT_ID from secrets."
else
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)
if [[ "$ACCOUNT_ID" == "null" ]]; then
echo "Failed to get an account id, please make sure you have set up CLOUDFLARE_API_TOKEN correctly!"
exit 1
fi
fi
echo 'account_id='$ACCOUNT_ID >> $GITHUB_OUTPUT
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

# This is a temporary workaround to fix issue #13
# On a new Cloudflare account, the terraform apply will fail with `workers.api.error.subdomain_required`
# This may be due to the account not having a worker subdomain yet, so we create a dummy worker and then delete it.
# Cloudflare should allocate a worker subdomain after this.
# https://github.com/cloudflare/terraform-provider-cloudflare/issues/3304
- name: Create worker subdomain
id: create_dummy_worker
run: |
curl --request PUT --fail-with-body \
--url https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/dummy-ib4db6ntj5csdef3 \
--header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN \
--header 'Content-Type: application/javascript' \
--data 'addEventListener('\''fetch'\'', (event) => event.respondWith(new Response('\''OK'\'')))'\
curl --request DELETE --fail-with-body \
--url https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/dummy-ib4db6ntj5csdef3 \
--header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN \
--header 'Content-Type: application/json'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}

- name: Install packages
run: |
npm install
cd worker
npm install
- name: Build worker
run: |
cd worker
npx wrangler deploy src/index.ts --outdir dist --dry-run
- name: Build page
run: |
npx @cloudflare/next-on-pages
- name: Deploy using Terraform
# We're using terraform for first-time setup here,
# since we didn't setup a remote backend to store state,
# following runs will fail with name conflict, which is normal.
continue-on-error: true

run: |
terraform init
terraform apply -auto-approve -input=false
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}

# Still need to upload worker to keep it up-to-date (Terraform will fail after first-time setup)
- name: Upload worker
run: |
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"}'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}

# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
- name: Upload pages
run: |
npx wrangler pages deploy .vercel/output/static --project-name uptimeflare
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
56 changes: 56 additions & 0 deletions .github/workflows/sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Upstream Sync

permissions:
contents: write
actions: write

on:
workflow_dispatch:
inputs:
manual-trigger:
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.'
type: boolean
required: true
default: false
override-token:
description: '[Optional] Replace the default token with the supplied PAT. Used to resolve `refusing to allow a GitHub App to create or update workflow... `'
required: false

jobs:
sync_latest_from_upstream:
name: Sync latest commits from upstream repo
runs-on: ubuntu-latest

steps:
- name: Checkout target repo
uses: actions/checkout@v3
with:
token: ${{ inputs.override-token || secrets.GITHUB_TOKEN }}

- name: Current config
run: |
cat uptime.config.ts
cp uptime.config.ts /tmp/origin.config.ts
- name: Sync upstream changes
id: sync
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Fetch latest code
git clone https://github.com/lyc8503/UptimeFlare /tmp/latest
rm -rf /tmp/latest/.git
# Clean current repo and update
git rm -rf '*'
cp -r /tmp/latest/. .
cp /tmp/origin.config.ts uptime.config.ts
git add .
git commit -m "Sync latest code from upstream"
git push
- name: Trigger deployment
uses: benc-uk/workflow-dispatch@v1
with:
workflow: deploy.yml
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

.terraform/
terraform.tfstate*

/.wrangler
5 changes: 5 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trailingComma: 'es5'
tabWidth: 2
semi: false
singleQuote: true
printWidth: 100
25 changes: 25 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit da8fa8f

Please # to comment.