Skip to content

Instantly share code, notes, and snippets.

@evaera
Last active June 24, 2024 13:43
Show Gist options
  • Save evaera/0b94fdb72db01829c90e20f49f57ca3b to your computer and use it in GitHub Desktop.
Save evaera/0b94fdb72db01829c90e20f49f57ca3b to your computer and use it in GitHub Desktop.
Cloudflare Pages deploy into subdirectories

This Github Actions workflow file lets you deploy multiple websites to Cloudflare Pages in subfolders instead of subdomains by using an intermediate repository to hold the built files.

  • Create a new repository where the files will get deployed to. That is your build repo, and you should set up your Pages from that repo.
  • Create a secret in your repo or organization called DEPLOY_PAT with the value of a GitHub personal access token of an account that has access to push to your build repo
  • Edit the values under env:
    • replace AuthorNameGoesHere with the author of the build repo
    • replace BuildRepoNameGoesHere with the name of the build repo
    • replace UsernameOfPATGoesHere with the username of the account you created the personal access token for
name: Deploy
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
# Build step goes here
- uses: actions/setup-node@v2
with:
node-version: "14"
name: Setup node
- run: npm run build # Example build command
name: Build website
- name: Deploy
run: |
set -ex
git config --global user.email "[email protected]"
git config --global user.name "github-actions-bot"
REPO="$HOME/$REPO_NAME"
PROJECT_NAME="${GITHUB_REPOSITORY/${AUTHOR_NAME}\//}"
git clone "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${AUTHOR_NAME}/${REPO_NAME}.git" "$REPO"
rm -rf "$REPO/$PROJECT_NAME"
cp -r build "$REPO/$PROJECT_NAME"
cd "$REPO"
git add .
git diff-index --quiet HEAD || git commit --message "Deploy $PROJECT_NAME"
git push "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${AUTHOR_NAME}/${REPO_NAME}.git"
env:
GITHUB_TOKEN: ${{ secrets.DEPLOY_PAT }}
AUTHOR_NAME: AuthorNameGoesHere
REPO_NAME: BuildRepoNameGoesHere
GITHUB_USERNAME: UsernameOfPATGoesHere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment