90 lines
3.6 KiB
YAML
90 lines
3.6 KiB
YAML
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 22.x
|
|
uses: actions/setup-node@v3
|
|
with:
|
|
node-version: 22.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 }}
|
|
|
|
- nname: TODO migrate and init D1
|
|
|
|
- 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
|
|
# As we don't save terraform state somewhere, we need to import the existing resources
|
|
run: |
|
|
terraform init
|
|
|
|
KV_ID=$(curl https://api.cloudflare.com/client/v4/accounts/$TF_VAR_CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces\?per_page\=100 --header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN | jq -r '.result[] | select(.title == "uptimeflare_kv") | .id')
|
|
if [ -n "$KV_ID" ]; then
|
|
echo "Importing existing resources..."
|
|
terraform import cloudflare_workers_kv_namespace.uptimeflare_kv "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/$KV_ID"
|
|
terraform import cloudflare_workers_script.uptimeflare_worker "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare_worker" || echo "WARNING: Worker script import failed, continuing..."
|
|
terraform import cloudflare_workers_cron_trigger.uptimeflare_worker_cron "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare_worker" || echo "WARNING: Cron trigger import failed, continuing..."
|
|
terraform import cloudflare_pages_project.uptimeflare "$TF_VAR_CLOUDFLARE_ACCOUNT_ID/uptimeflare" || echo "WARNING: Pages project import failed, continuing..."
|
|
else
|
|
echo "KV namespace not found, first-time setup."
|
|
fi
|
|
|
|
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 }}
|
|
|
|
# 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 }}
|