|
| 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 | + |
| 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 }} |
0 commit comments