调整 CI #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Cloudflare | |
on: | |
push: | |
branches: [ 'main' ] | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Setup Terraform | |
uses: hashicorp/[email protected] | |
with: | |
terraform_version: 1.6.4 | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
- uses: pnpm/action-setup@v2 | |
name: Install pnpm | |
id: pnpm-install | |
with: | |
version: 8 | |
run_install: false | |
- 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 }} | |
# 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 Workers packages | |
run: | | |
cd workers | |
pnpm install | |
- name: Install Pages packages | |
run: | | |
cd pages | |
pnpm install | |
- name: Build worker | |
run: | | |
cd workers | |
pnpm npx wrangler deploy src/index.ts --outdir dist --dry-run | |
- name: Build page | |
run: | | |
cd pages | |
pnpm npx @cloudflare/next-on-pages | |
- name: Deploy using Terraform | |
continue-on-error: true | |
run: | | |
terraform init | |
terraform apply -auto-approve -input=false | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
TF_VAR_CLOUDFLARE_EMAIL_ADDRESS: ${{ secrets.CLOUDFLARE_EMAIL_ADDRESS }} | |
TF_VAR_CLOUDFLARE_DOMAIN_ZONE_NAME: ${{ secrets.CLOUDFLARE_DOMAIN_ZONE_NAME }} | |
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }} | |
- name: init database | |
run: | | |
cd pages | |
pnpm npx wrangler d1 execute yourselfemail_db --remote --file=./migrations/0001_create_table.sql | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }} | |
- name: Upload worker | |
run: | | |
cd workers | |
curl --fail-with-body -X PUT https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/yourselfemail_worker/content --header 'Authorization: Bearer '$CLOUDFLARE_API_TOKEN -F 'index.js=@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 }} | |
- name: Upload pages | |
run: | | |
cd pages | |
pnpm npx wrangler pages deploy .vercel/output/static --project-name yourselfemail | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }} |