From 86f984f8515fce0184e837c72d6f7d57d8fcf5e0 Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 15:06:58 +0200 Subject: [PATCH 01/10] ci: use gh actions instead of cf app --- .github/workflows/deploy.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..1b6c4d2 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,31 @@ +name: Deploy + +on: [push] + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + contents: read + deployments: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + + - name: Install dependencies + run: bun install + + - name: Build + run: bun run build + + - name: Deploy to Cloudflare Pages + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} From 478d76659d05ba8320fb2eae03c8af7461032075 Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 15:40:15 +0200 Subject: [PATCH 02/10] cleanup and msg --- .github/workflows/deploy.yml | 40 ++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1b6c4d2..c5ba2f5 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,6 +1,9 @@ name: Deploy - -on: [push] +on: + push: + branches: [main] + pull_request: + types: [opened, synchronize, reopened] jobs: deploy: @@ -8,6 +11,7 @@ jobs: permissions: contents: read deployments: write + pull-requests: write steps: - name: Checkout uses: actions/checkout@v4 @@ -24,8 +28,40 @@ jobs: run: bun run build - name: Deploy to Cloudflare Pages + id: cloudflare uses: cloudflare/wrangler-action@v3 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} + + - name: Comment on PR + if: github.event_name == 'pull_request' && github.event.action == 'opened' + uses: actions/github-script@v7 + with: + script: | + const output = `**Branch Preview**: ${{ steps.cloudflare.outputs.url }}`; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }); + + cleanup: + if: github.event_name == 'pull_request' && github.event.action == 'closed' + runs-on: ubuntu-latest + permissions: + contents: read + deployments: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Delete Preview Deployment + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deployments delete --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} From fda1093b56c7309b7592e15872e13a7331c39064 Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 15:43:47 +0200 Subject: [PATCH 03/10] test upd --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c5ba2f5..80a7bc0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -36,7 +36,7 @@ jobs: command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} - name: Comment on PR - if: github.event_name == 'pull_request' && github.event.action == 'opened' + if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') uses: actions/github-script@v7 with: script: | From b49c78af9f267772497d5f924eee464a5512f454 Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 15:57:33 +0200 Subject: [PATCH 04/10] use separated deploys --- .../{deploy.yml => deploy-preview.yml} | 35 +++++++++++++------ .github/workflows/deploy-prod.yml | 35 +++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) rename .github/workflows/{deploy.yml => deploy-preview.yml} (61%) create mode 100644 .github/workflows/deploy-prod.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy-preview.yml similarity index 61% rename from .github/workflows/deploy.yml rename to .github/workflows/deploy-preview.yml index 80a7bc0..f30e086 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy-preview.yml @@ -1,7 +1,5 @@ -name: Deploy +name: Deploy [Preview] on: - push: - branches: [main] pull_request: types: [opened, synchronize, reopened] @@ -16,6 +14,21 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Create GitHub Deployment + id: deployment + uses: actions/github-script@v7 + with: + script: | + const deployment = await github.rest.repos.createDeployment({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: context.payload.pull_request.head.ref, + environment: 'Preview', + required_contexts: [], + auto_merge: false + }); + return deployment.data.id; + - name: Setup Bun uses: oven-sh/setup-bun@v2 with: @@ -33,20 +46,20 @@ jobs: with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} + command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} - - name: Comment on PR - if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') + - name: Update deployment status uses: actions/github-script@v7 with: script: | - const output = `**Branch Preview**: ${{ steps.cloudflare.outputs.url }}`; - - github.rest.issues.createComment({ - issue_number: context.issue.number, + await github.rest.repos.createDeploymentStatus({ owner: context.repo.owner, repo: context.repo.repo, - body: output + deployment_id: ${{ steps.deployment.outputs.result }}, + state: 'success', + environment_url: '${{ steps.cloudflare.outputs.url }}', + description: 'Deployed to Cloudflare Pages', + environment: 'Preview' }); cleanup: diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml new file mode 100644 index 0000000..52c31af --- /dev/null +++ b/.github/workflows/deploy-prod.yml @@ -0,0 +1,35 @@ +name: Deploy +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + contents: read + deployments: write + pull-requests: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + + - name: Install dependencies + run: bun install + + - name: Build + run: bun run build + + - name: Deploy to Cloudflare Pages + id: cloudflare + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} From 2eb83d63f0fcb91cef54a8018a239c037e3c63cc Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:08:22 +0200 Subject: [PATCH 05/10] debug deploy --- .github/workflows/deploy-preview.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index f30e086..a401726 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -48,6 +48,13 @@ jobs: accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} + - name: Debug Cloudflare outputs + run: | + echo "${{ steps.cloudflare.outputs }}" + echo "Deployment URL: ${{ steps.cloudflare.outputs.url }}" + echo "Deployment ID: ${{ steps.cloudflare.outputs.deployment-id }}" + echo "Environment: ${{ steps.cloudflare.outputs.environment }}" + - name: Update deployment status uses: actions/github-script@v7 with: @@ -63,7 +70,7 @@ jobs: }); cleanup: - if: github.event_name == 'pull_request' && github.event.action == 'closed' + if: github.event.action == 'closed' runs-on: ubuntu-latest permissions: contents: read From f87f412c6d38fc5154e9eb788eb634c32e6eab2a Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:11:02 +0200 Subject: [PATCH 06/10] toJSON --- .github/workflows/deploy-preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index a401726..a240fa1 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -50,7 +50,7 @@ jobs: - name: Debug Cloudflare outputs run: | - echo "${{ steps.cloudflare.outputs }}" + echo "${{ toJSON(steps.cloudflare.outputs) }}" echo "Deployment URL: ${{ steps.cloudflare.outputs.url }}" echo "Deployment ID: ${{ steps.cloudflare.outputs.deployment-id }}" echo "Environment: ${{ steps.cloudflare.outputs.environment }}" From b7cb5abfc09defe0e65c433277b2983bd747889e Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:14:52 +0200 Subject: [PATCH 07/10] right preview url --- .github/workflows/deploy-preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index a240fa1..aa289bd 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -64,7 +64,7 @@ jobs: repo: context.repo.repo, deployment_id: ${{ steps.deployment.outputs.result }}, state: 'success', - environment_url: '${{ steps.cloudflare.outputs.url }}', + environment_url: '${{ steps.cloudflare.outputs.deployment-alias-url }}', description: 'Deployed to Cloudflare Pages', environment: 'Preview' }); From aee41eb80d2f3a2834348589f8446896ba2411ad Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:16:36 +0200 Subject: [PATCH 08/10] remove extra debug --- .github/workflows/deploy-preview.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index aa289bd..9bd045e 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -50,8 +50,7 @@ jobs: - name: Debug Cloudflare outputs run: | - echo "${{ toJSON(steps.cloudflare.outputs) }}" - echo "Deployment URL: ${{ steps.cloudflare.outputs.url }}" + echo "Deployment URL: ${{ steps.cloudflare.outputs.deployment-alias-url }}" echo "Deployment ID: ${{ steps.cloudflare.outputs.deployment-id }}" echo "Environment: ${{ steps.cloudflare.outputs.environment }}" From f92da31a368dd52621aa49e5047a6be7351f134d Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:26:44 +0200 Subject: [PATCH 09/10] enhance --- .github/workflows/deploy-preview.yml | 85 ++++++++++++++++++---------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index 9bd045e..46e8301 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -1,10 +1,11 @@ name: Deploy [Preview] on: pull_request: - types: [opened, synchronize, reopened] + types: [opened, synchronize, reopened, closed] jobs: deploy: + if: github.event.action != 'closed' runs-on: ubuntu-latest permissions: contents: read @@ -22,10 +23,11 @@ jobs: const deployment = await github.rest.repos.createDeployment({ owner: context.repo.owner, repo: context.repo.repo, - ref: context.payload.pull_request.head.ref, + ref: context.payload.pull_request.head.sha, # Use SHA instead of ref environment: 'Preview', required_contexts: [], - auto_merge: false + auto_merge: false, + description: 'Deploying to Cloudflare Pages' }); return deployment.data.id; @@ -48,39 +50,64 @@ jobs: accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy dist --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} - - name: Debug Cloudflare outputs - run: | - echo "Deployment URL: ${{ steps.cloudflare.outputs.deployment-alias-url }}" - echo "Deployment ID: ${{ steps.cloudflare.outputs.deployment-id }}" - echo "Environment: ${{ steps.cloudflare.outputs.environment }}" - - name: Update deployment status + if: always() uses: actions/github-script@v7 with: script: | - await github.rest.repos.createDeploymentStatus({ + const isSuccess = '${{ steps.cloudflare.outcome }}' === 'success'; + const state = isSuccess ? 'success' : 'failure'; + const description = isSuccess + ? 'Deployed successfully' + : 'Deployment failed'; + + const data = { owner: context.repo.owner, repo: context.repo.repo, deployment_id: ${{ steps.deployment.outputs.result }}, - state: 'success', - environment_url: '${{ steps.cloudflare.outputs.deployment-alias-url }}', - description: 'Deployed to Cloudflare Pages', - environment: 'Preview' - }); + state: state, + description, + environment: 'Preview', + environment_url: isSuccess ? '${{ steps.cloudflare.outputs.deployment-alias-url }}' : null + }; + + await github.rest.repos.createDeploymentStatus(data); cleanup: - if: github.event.action == 'closed' - runs-on: ubuntu-latest - permissions: - contents: read - deployments: write - steps: - - name: Checkout - uses: actions/checkout@v4 + if: github.event.action == 'closed' + runs-on: ubuntu-latest + permissions: + contents: read + deployments: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Delete Cloudflare Pages deployment + continue-on-error: true + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deployment delete --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} + + - name: Mark GitHub deployments as inactive + uses: actions/github-script@v7 + with: + script: | + const deployments = await github.rest.repos.listDeployments({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: context.payload.pull_request.head.sha, + environment: 'Preview' + }); - - name: Delete Preview Deployment - uses: cloudflare/wrangler-action@v3 - with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: pages deployments delete --project-name=${{ secrets.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.event.pull_request.head.ref }} + for (const deployment of deployments.data) { + await github.rest.repos.createDeploymentStatus({ + owner: context.repo.owner, + repo: context.repo.repo, + deployment_id: deployment.id, + state: 'inactive', + description: 'PR closed, deployment cleaned up' + }); + } From 3be0f599b0c6039ce12369d10bb3581aa65ffe95 Mon Sep 17 00:00:00 2001 From: Vlad Esafev Date: Fri, 25 Jul 2025 16:29:49 +0200 Subject: [PATCH 10/10] update --- .github/workflows/deploy-preview.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index 46e8301..587125d 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -23,7 +23,7 @@ jobs: const deployment = await github.rest.repos.createDeployment({ owner: context.repo.owner, repo: context.repo.repo, - ref: context.payload.pull_request.head.sha, # Use SHA instead of ref + ref: context.payload.pull_request.head.ref, environment: 'Preview', required_contexts: [], auto_merge: false, @@ -98,7 +98,7 @@ jobs: const deployments = await github.rest.repos.listDeployments({ owner: context.repo.owner, repo: context.repo.repo, - ref: context.payload.pull_request.head.sha, + ref: context.payload.pull_request.head.ref, environment: 'Preview' });