cloudflare / pages-action

MIT License
468 stars 95 forks source link

Ability to delete preview site when pr is closed #47

Closed andrewgbell closed 1 month ago

andrewgbell commented 1 year ago

Hi,

The action is working great, however we're ending up with a lot of preview sites that are no longer needed as they don't appear to know that the pr is closed/merged.

Is there any ability, or plan to handle the deletion of preview sites when pr's are merged?

Thanks.

jaydrogers commented 1 year ago

I am wondering if there will be support for this as well.

I wonder if there is a workaround that we can run via the API to delete it? Seems "hacky" to do, but it would be nice to get official word if this is planned to be supported or not before messing with the API. 🙌

Source

https://community.cloudflare.com/t/delete-cloudlfare-pages-deployments/345640

jaydrogers commented 1 year ago

Looking into this further, I noticed when I deleted the environment from GitHub it automatically deleted it from the CloudFlare dashboard too.

I wonder if managing this through the GitHub API might be a better approach?

https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deleting-an-environment

limakzi commented 1 year ago

@andrewgbell I really appreciate this request. ❤️ @jaydrogers Cloudflare Pages and Github Environment are two completely isolated things. Said that, I would see a proper workflow like this:

flowchart LR
    A[Pull-request has been opened] --> B[Environment has been provisioned]
    B -->|Provision| Environment
    subgraph Environment
    C[Github Environment] -. matches .- D[Cloudflare Deployment]
    end

    E[Pull-request has been merged] --> F[Environment has been decommissioned]
    F -->|Decommission| Environment
jaydrogers commented 1 year ago

Thanks a ton for the diagram! This makes sense. I'll play around with this and see if I can provide some examples that work for me.

I appreciate your attention to detail @limakzi!

rmoff commented 7 months ago

FWIW I found this action that does have the option to delete when a PR is closed: https://github.com/unlike-ltd/github-actions-cloudflare-pages

dargmuesli commented 7 months ago

The action @rmoff linked seems to use a basic api fetch: https://github.com/unlike-ltd/github-actions-cloudflare-pages/blob/main/src/cloudflare/deployment/delete.ts#L7

The pages-action also uses some fetches already, so it might be possible to adapt: https://github.com/cloudflare/pages-action/blob/aeb0d936a7e08b4861c3d0ac8811fe39138085f9/src/index.ts#L52

acrogenesis commented 2 months ago

This action works:

name: Delete Cloudflare Pages Preview Deployment

on:
  pull_request:
    types: [closed]

jobs:
  delete-preview-deployment:
    runs-on: x64
    steps:
      - name: Get branch name
        id: get-branch
        run: echo "branch=$(gh pr view $PR_NO --repo $REPO --json headRefName --jq '.headRefName')" >> $GITHUB_OUTPUT
        env:
          REPO: ${{ github.repository }}
          PR_NO: ${{ github.event.pull_request.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Delete Cloudflare Pages preview deployments
        run: |
          DEPLOY_BRANCH="${{ steps.get-branch.outputs.branch }}"
          deployment_ids=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ github.event.repository.name }}/deployments" \
            -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
            -H "Content-Type: application/json" \
            | jq -r --arg DEPLOY_BRANCH "$DEPLOY_BRANCH" '.result[] | select(.deployment_trigger.metadata.branch == $DEPLOY_BRANCH) | .id')

          for deployment_id in $deployment_ids; do
            echo "Deleting deployment $deployment_id"
            curl -s -X DELETE "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ github.event.repository.name }}/deployments/$deployment_id?force=true" \
              -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
              -H "Content-Type: application/json"
          done
nprogers commented 1 month ago

@andrewgbell Thanks for the suggestion! This seems like something that might be better managed within Pages instead of as part of an action. I'll pass this request to the Pages core team as a possible new feature. In the meantime, please use the workaround posted above. Thanks! cc @nevikashah

robinmetral commented 1 week ago

@nprogers is there a way we can track this feature request to Pages core? Would also love my preview deploys to be automatically deleted when a PR is merged (and this would probably free up a certain amount of storage on CF data centers).