Giter VIP home page Giter VIP logo

Comments (8)

GregBrimble avatar GregBrimble commented on September 28, 2024 1

Hi, thanks for the report! We're slowly but surely building out APIs in wrangler, so once we have a import { pagesPublish } from 'wrangler' (or similar) surface, we'll be able to properly address this problem.

from pages-action.

fubhy avatar fubhy commented on September 28, 2024 1

Great. That's a good solution for this github action indeed! I'd still argue that a --json option could be helpful for the wrangler CLI in general :-)

from pages-action.

mcoetzee avatar mcoetzee commented on September 28, 2024 1

@jasikpark This might interest you. I managed to get the alias with the below setup.

After deploying I make a request to the Cloudflare API to get the alias. You just need to replace YOUR_POJECT_NAME in the endpoint and it should work. You can then use ${{ steps.get-alias.outputs.alias }} as you see fit:

      - name: Publish
        id: cloudflare
        uses: cloudflare/pages-action@1
        with:
          ... All your settings

      - name: Get Deployment Alias
        if: github.event_name == 'pull_request'
        id: get-alias
        run: |
          sleep 20s
          deployment_response=$(curl --request GET --url https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/YOUR_POJECT_NAME/deployments/${{ steps.cloudflare.outputs.id }} --header 'Content-Type: application/json' -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}")
          alias=$(jq -r '.result.aliases[0]' <<< "$deployment_response")
          echo "alias=$alias" >> "$GITHUB_OUTPUT"

      - name: Echo outputs
        run: |
          echo ${{ steps.cloudflare.outputs.id }}
          echo ${{ steps.cloudflare.outputs.url }}
          echo ${{ steps.cloudflare.outputs.environment }}
          echo ${{ steps.get-alias.outputs.alias }}

Edit 1: Getting some mixed results when deploying the PR for the first time. Sometimes the alias is not yet available.

Edit 2: Adding the sleep 20s before making the request seems to work well enough.

from pages-action.

fubhy avatar fubhy commented on September 28, 2024

Sorry, meant to add a link to the code in question: https://github.com/cloudflare/pages-action/blob/main/src/index.ts#L68-L74

from pages-action.

jasikpark avatar jasikpark commented on September 28, 2024

Is there any progress on this? sorry to poke :p I suppose this is still blocked on wrangler having a JS api?

https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/src/pages/publish.tsx

my main interest is getting consistent alias output so that I can generate useful links to the deploy previews

from pages-action.

jasikpark avatar jasikpark commented on September 28, 2024

Sorry to poke again, but is there any work I can do to help move this along? I have a script that creates a facimile address by just appending

`${branchName}${cfpages-preview-url}`

but this doesn't work due to DNS encoding values and truncation of the url. I suppose I could just reverse engineer what the rest of the algo is 🤷

from pages-action.

jasikpark avatar jasikpark commented on September 28, 2024

My current solution is to just manually generate the deploy url from the branch name:

/** Discovered from generated urls in the CloudFlare Dashboard */
const MAX_LENGTH_SUBDOMAIN = 28;

/**
 * {@link genDeployURL} tries to take the branch for a deploy preview and generate the same subdomain that CloudFlare does.
 * @param props {{ branchName: string }}
 */
export default function genDeployURL({ branchName }) {
  let formattedSubdomain = branchName
    .toLowerCase()
    .replace(/[_|\/]/g, '-')
    .slice(0, MAX_LENGTH_SUBDOMAIN);

  // Do not end the subdomain with an `-`.
  if (formattedSubdomain.endsWith('-')) {
    formattedSubdomain = formattedSubdomain.slice(0, formattedSubdomain.length - 1);
  }

  return formattedSubdomain;
}
- name: Generate CF Deploy Preview URL
        id: deploy-preview-url-gen
        uses: actions/github-script@v6
        with:
          result-encoding: string
          script: |
            const { default: script } = await import('${{ github.workspace }}/.github/scripts/gen-deploy-url.mjs')
            return script({ branchName: '${{ steps.branch-name.outputs.current_branch }}' })

This may yet be incomplete, but it's been good enough for me for now. I use a GA to grab the true branch name and another GA to comment on the PR with the deploy details.

from pages-action.

nprogers avatar nprogers commented on September 28, 2024

I agree that the alias output from the publish step seems to always be the deployment URL. We are not making any improvements to this repo moving forward. The workarounds above will also work with wrangler-action.

from pages-action.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.