cloudflare / pages-action

MIT License
468 stars 95 forks source link

No way to define if production or preview env #63

Closed giladv closed 1 month ago

giladv commented 1 year ago

Hey,

So want to use CF pages on a monorepo which the new workingDirectory feature was designed for. Problem is, to use with monorepos, project has to be defined as a "direct upload" project which doesn't has a production branch setting in it. The only way to do it is to allow this action to have a productionBranch setting which is missing.

Please advise.

raiyansarker commented 1 year ago

I am having the same issue. Every push or pr in any branch results in production deployment.

ari-becker commented 1 year ago

I had the same issue, the workaround is to invoke the API directly to patch the production_branch field to be the real production branch: https://developers.cloudflare.com/api/operations/pages-project-update-project

aaronadamsCA commented 1 year ago

workingDirectory support was only released yesterday, so you should probably try again and see if you're still having this problem.

That said, the production branch name comes from the Cloudflare API, but I'm not sure the value can be changed from main. I believe that would be a dashboard gap, since that value should still be configurable for direct uploads.

lostpebble commented 1 year ago

We have multiple branches for multiple deployments and previews (our own version of a preview, that needs to be available directly at our subdomain)- but now anything we run that is not "master" branch (like PR actions etc.) are all being deployed as "preview" which is useless because now those deployments are not visible and live at our subdomain.

I've run into this kind of issue before. I think Cloudflare needs to have good defaults- but always give the option to make a deployment a "production" deployment... Why can't we just provide a flag "--production" or whatever.

WalshyDev commented 1 year ago

I've run into this kind of issue before. I think Cloudflare needs to have good defaults- but always give the option to make a deployment a "production" deployment... Why can't we just provide a flag "--production" or whatever.

You... can. Just specify the branch as your production branch (--branch=<production-branch> in wrangler) With this action, it's just

branch: <production-branch>
lostpebble commented 1 year ago

Would be amazing if there was more documentation about this!

EDIT: Nevermind, I see it in the Readme. My mistake, but would be nice to just have an "options" area that shows all the options available.

lostpebble commented 1 year ago

image

Still is set as preview... This DX is really not so great. Now I have to guess what the actual "production" branch is- probably "main". I can't set this anywhere because the page project is a "direct upload" one, because we are using Github Actions.

The idea of a single "production" branch doesn't really work in a lot of cases either.

WalshyDev commented 1 year ago

It'll either be "main" or "production" probably

The idea of a single "production" branch doesn't really work in a lot of cases either.

You have to remember we use to have only Git, not Direct Uploads. Specifying a --production is the same as a --branch=<prod-branch>

We could technically add an option to just do that but not sure I like that personally (feel free to put up an issue on the Wrangler docs though)

lostpebble commented 1 year ago

We've managed to win with "main". Its just a lil weird because we don't even have a "main" branch (still using "master" on this repo). So overall, just opening the door for confusion.

I understand that Git was how things originally worked- but surely when it comes to direct uploads, sometimes these won't even be coming from a git repo- an option just to say --production seems like a no-brainer to me. Should have been there from the get-go.

aaronadamsCA commented 1 year ago

@WalshyDev I think the CF dash should be updated to let folks change the production branch when the project is set to manual upload. Right now it basically hard-codes to main, which is not always going to be correct.

IMHO the rest of the functionality is fine here and the only real problem is in the CF dash.

WalshyDev commented 1 year ago

I think the CF dash should be updated to let folks change the production branch when the project is set to manual upload

Yes, it needs to have this indeed. Unfortunately, it's been one of those things that just slipped into the backlog and have not been prioritized. It's on my todo list but things keep taking higher priority.

nickdandakis commented 1 year ago

For anyone else that's bumping into this and wants a quick overview on how to fix up their project's production branch as of right now...

There's two steps required here:

  1. Ensure branch: YOUR_PRODUCTION_BRANCH_HERE is set in your Action's yaml file
  2. Update your Cloudflare Pages project's production_branch. Here's a helpful cURL (h/t @ari-becker)
    curl --request PATCH \
    --url https://api.cloudflare.com/client/v4/accounts/YOUR_ACCOUNT_IDENTIFIER_HERE/pages/projects/YOUR_PROJECT_IDENTIFIER_HERE \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer YOUR_API_TOKEN_HERE' \
    --data '{ "production_branch": "YOUR_PRODUCTION_BRANCH_HERE" }'

Replace YOUR_ACCOUNT_IDENTIFIER_HERE, YOUR_PROJECT_IDENTIFIER_HERE, YOUR_API_TOKEN_HERE with your own. All of which can be found or created in your Cloudflare dashboard. Note that YOUR_API_TOKEN_HERE needs the Cloudflare Pages:Edit permission.

Replace PRODUCTION_BRANCH_HERE with your repo's branch (e.g. for me it's trunk). This should match the branch: PRODUCTION_BRANCH_HERE set in your Action yaml.

🚀

Keavon commented 1 year ago

Hints for the above:

YOUR_ACCOUNT_IDENTIFIER_HERE

  1. Go to your dashboard, copy the hash from the URL immediately after the hostname

YOUR_PROJECT_IDENTIFIER_HERE

  1. Use the name of your CF Pages site as it appears in the dashboard

YOUR_API_TOKEN_HERE

  1. Go to https://dash.cloudflare.com/profile/api-tokens
  2. Click "Create Token"
  3. At the bottom under "Custom token" beside "Create Custom Token" click "Get Started"
  4. Give the token a name, it doesn't matter what
  5. Under "Permissions", select "Account" from the first dropdown, "Cloudflare Pages" from the second, and "Edit" from the third
  6. Leave the rest blank and click "Continue to summary" then "Create token"
  7. Click "Copy"

YOUR_PRODUCTION_BRANCH_HERE

  1. Probably master or whatever your actual Git production branch is named instead of the default of main which has no UI to be changed

After making those 4 changes, copy and paste it into a Linux terminal (not command prompt, for some reason the API returns an error using the built-in Windows bash). It might be easier/more reliable to remove the \ after each line and make it all one line. As a one-liner for convenience:

curl --request PATCH --url https://api.cloudflare.com/client/v4/accounts/YOUR_ACCOUNT_IDENTIFIER_HERE/pages/projects/YOUR_PROJECT_IDENTIFIER_HERE --header 'Content-Type: application/json' --header 'Authorization: Bearer YOUR_API_TOKEN_HERE' --data '{ "production_branch": "YOUR_PRODUCTION_BRANCH_HERE" }'

Important: I found that using the bash built into Windows didn't work in command prompt. I kept getting errors from the API. Using the exact same command from WSL bash did work, though. Not sure why, but hopefully that helps.