|
| 1 | +name: ☁️ Deploy to Cloudflare Pages (Runtime) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['main'] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: cloudflare-pages-runtime-${{ github.ref_name }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + deploy-runtime: |
| 17 | + name: 🏗 Build and Deploy Runtime |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: 🔍 Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: 🔒 Verify Google services secret exists |
| 25 | + run: | |
| 26 | + if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then |
| 27 | + echo "❌ GOOGLE_SERVICES_JSON_BASE64 secret is missing" |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | +
|
| 31 | + - name: 📁 Create google-services.json |
| 32 | + run: | |
| 33 | + echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json |
| 34 | + if ! jq empty google-services.json; then |
| 35 | + echo "❌ google-services.json is invalid" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + env: |
| 39 | + GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }} |
| 40 | + |
| 41 | + - name: 📦 Install pnpm |
| 42 | + run: npm install -g pnpm@10 |
| 43 | + |
| 44 | + - name: ⚙️ Setup Node.js |
| 45 | + uses: actions/setup-node@v4 |
| 46 | + with: |
| 47 | + node-version: '24' |
| 48 | + cache: 'pnpm' |
| 49 | + |
| 50 | + - name: 📥 Install dependencies |
| 51 | + run: pnpm install --frozen-lockfile |
| 52 | + |
| 53 | + - name: 🏗 Build Next.js for runtime |
| 54 | + env: |
| 55 | + NEXT_OUTPUT_MODE: standalone |
| 56 | + run: pnpm build |
| 57 | + |
| 58 | + - name: 🏗 Build Cloudflare Pages output (functions + static) |
| 59 | + run: pnpm dlx @cloudflare/next-on-pages@1 |
| 60 | + |
| 61 | + - name: 🔎 Verify Cloudflare Pages project exists |
| 62 | + env: |
| 63 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 64 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 65 | + PROJECT_NAME: ${{ secrets.CLOUDFLARE_PAGES_PROJECT_NAME_RUNTIME }} |
| 66 | + run: | |
| 67 | + RESPONSE=$(curl -fsSL \ |
| 68 | + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 69 | + -H "Content-Type: application/json" \ |
| 70 | + "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects") |
| 71 | +
|
| 72 | + COUNT=$(printf '%s' "$RESPONSE" | jq '.result | length') |
| 73 | + MATCH=$(printf '%s' "$RESPONSE" | jq --arg name "$PROJECT_NAME" 'any(.result[]?; .name == $name)') |
| 74 | +
|
| 75 | + echo "Pages projects visible in this account: $COUNT" |
| 76 | +
|
| 77 | + if [ "$MATCH" != "true" ]; then |
| 78 | + echo "Configured Pages project was not found in the provided Cloudflare account." |
| 79 | + echo "Verify CLOUDFLARE_ACCOUNT_ID points to the same account that owns the Pages project." |
| 80 | + echo "Visible project names:" |
| 81 | + printf '%s' "$RESPONSE" | jq -r '.result[]?.name' |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "Configured Pages project exists in the target account." |
| 86 | +
|
| 87 | + - name: 🚀 Deploy runtime app to Cloudflare Pages |
| 88 | + uses: cloudflare/wrangler-action@v3 |
| 89 | + with: |
| 90 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 91 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 92 | + command: >- |
| 93 | + pages deploy .vercel/output/static |
| 94 | + --project-name=${{ secrets.CLOUDFLARE_PAGES_PROJECT_NAME_RUNTIME }} |
| 95 | + --branch=${{ github.ref_name }} |
| 96 | + --commit-hash=${{ github.sha }} |
| 97 | + --functions=.vercel/output/functions |
0 commit comments