Giter Site home page Giter Site logo

schnerring / cloudflare-pages-branch-alias-action Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 1.54 MB

A GitHub Action that generates a valid branch alias for Cloudflare Pages deployments from a Git branch name

License: Other

TypeScript 76.27% Shell 23.73%

cloudflare-pages-branch-alias-action's Introduction

Cloudflare Pages Branch Alias

A GitHub Action that generates a valid branch alias for Cloudflare Pages deployments from a Git branch name.

When using Direct Uploads for Cloudflare Pages, the deployment URL is only available after the deployment has finished. This is insufficient if the deployment URL is required before uploading. For example, the static site generator Hugo requires a baseUrl parameter at build time. This Action serves as a workaround until natively supported by the cloudflare/workers-sdk.

From the Cloudflare docs about Pages Preview Aliases

When a preview deployment is published, it is given a unique, hash-based address — for example, <hash>.<project>.pages.dev. These are atomic and may always be visited in the future. However, Pages also creates an alias [<git-branch>.<project>.pages.dev] for git branch’s name and updates it so that the alias always maps to the latest commit of that branch. [...]

Branch name aliases are lowercased and non-alphanumeric characters are replaced with a hyphen — for example, the fix/api branch creates the fix-api.<project>.pages.dev alias.

Credit goes to Daniel Walsh who shared a JavaScript algorithm to calculate the <git-branch> prefix on the Cloudflare Community forums. This Action is merely a wrapper around that script. Thank you @AngusMorton for optimizing the script!

Usage

The following shows how the action can be used to pass the calculated branch alias to Hugo via --baseURL flag.

name: Build Hugo Site

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.111.3'

      - uses: schnerring/cloudflare-pages-branch-alias-action@v1
        id: pages-branch-alias
        with:
          # git-branch input
          git-branch: ${{ github.ref_name }}

      - name: Build Hugo
        # branch-alias output
        run: |
          hugo \
            --minify \
            --baseURL "https://${{ steps.pages-branch-alias.outputs.branch-alias }}.my-project.pages.dev"

cloudflare-pages-branch-alias-action's People

Contributors

angusmorton avatar dependabot[bot] avatar schnerring avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

angusmorton

cloudflare-pages-branch-alias-action's Issues

Trailing dashes should be removed after limiting the alias max length

Currently, the trailing dashes are removed before we limit the length of the array. This means that trailing dashes can still end up in the final alias.

For example: aaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbb, we normalise and trim trailing dashes it remains the same. Then we limit the length aaaaaaaaaaaaaaaaaaaaaaaaaaa-.

We should update the logic in generate-branch-alias.ts to trim after we substring.

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.