Branch alias URL for Pages Production env

Type

Product improvement

Description

Cloudflare Pages

Benefit

The problem is that build tooling is unable to deterministically find the previous deployment on the same Production branch.

For Preview builds, there is a branch Alias URL, like fix-api.<project>.pages.dev , but not for Production.

Build tooling that wants to access the current deployment can do so using the Alias URL, but only for Preview builds, not for Production builds (Assume preview on the same branch is disabled). If preview builds are enabled, and they happen to be enabled on the same branch that Production builds from, then the Alias URL is (accidentally) available, but not if Preview builds are turned off.

Then problem has been encountered before:

While the asker was satisfied with the answer given, the problem remains: branch Alias URL’s are only available (haphazardly) if Preview builds are also enabled, and the Production branch happens to be enabled for Previews also.

The “Alias URL” feature needs to work in the Production environment too.

My workaround

My workaround requires an environment variable CF_BUG_756652_WORKAROUND to be set to hint the build script that it is running in a production environment, and the correct branch URL is <proj>.pages.dev instead of <branch>.<proj>.pages.dev

		# https://community.cloudflare.com/t/branch-alias-url-for-pages-production-env/756652
		# On Cloudflare Pages, a branch URL Alias is not available for Production builds (if Preview Deployments are disabled)
		# See community discussion above.
		# This workaround should work for both preview and production.
		# Set the Environment variable CF_BUG_756652_WORKAROUND to contain both the production branch name and the
		# project URL, which is what the branch URL would point to. Example:
		# CF_BUG_756652_WORKAROUND="release https://my-project.pages.dev"
		if [[ ${CF_BUG_756652_WORKAROUND:+isset} ]] ; then
			local cf_production_branch_alias_params=($CF_BUG_756652_WORKAROUND)
			if [[ ${cf_production_branch_alias_params[0]} == ${CF_PAGES_BRANCH} ]] ; then
				BRANCH_URL=${cf_production_branch_alias_params[1]}
			else
				local escaped_branch=$(echo $CF_PAGES_BRANCH | sed 's@[^[:alnum:]]@-@g;')
				BRANCH_URL=$(echo $CF_PAGES_URL | sed -r "s@https://\\w+.@https://$escaped_branch.@")
			fi
		fi

I’m not sure I understand the issue. The production alias is your production URL <project>.pages.dev

The problem is that the build script cannot determine whether the previous deployment is <project>.pages.dev or <branch>.<project>.pages.dev

If it assumes <branch>.<project>.pages.dev, this alias does not exist for non-Preview builds (or with preview builds disabled).

If it assumes <project>.pages.dev - this is incorrect when building a preview deployment.

I need the build to reliably know the URL for the previous build on the same branch.

I have updated the question to add my implemented workaround; hopefully it clarifies why to reliably calculate $BRANCH_URL, a hint is needed via the environment.

I should have put it more succinctly: In order to know how to correctly calculate the URL Alias, the build script needs to detect if it is running in a Production or Preview environment.

Based on that detection, it needs to add the <branch> piece to the URL, or omit it.