rossjrw / pr-preview-action

GitHub Action that deploys a pull request preview to GitHub Pages, similar to Vercel and Netlify, and cleans up after itself.
https://github.com/marketplace/actions/deploy-pr-preview
MIT License
267 stars 43 forks source link

Use links of the deploy preview, not actual deployment #14

Open dharmit opened 2 years ago

dharmit commented 2 years ago

I'm running into an issue where clicking on any link on the landing page on deploy preview take me to the page on actual deployment being served from the root of gh-pages branch instead of the deploy preview generated using pr-review-action.

For example, my index page has a link to "Introduction" which takes me to <user>.github.io/<repo>/docs/introduction. For a deploy preview built using pr-review-action, I expect it to take me to <user>.github.io/<repo>/pr-review/pr-<pr-number>/docs/introduction.

For reference, you can take a look at https://github.com/dharmit/odo-gh-actions/pull/6. The deploy preview is available at https://dharmit.github.io/odo-gh-actions/pr-preview/pr-6/. But when I click on "Read the docs" button, it takes me to https://dharmit.github.io/odo-gh-actions/docs/introduction while I expect it to take me to https://dharmit.github.io/odo-gh-actions/pr-preview/pr-6//docs/introduction.

rossjrw commented 2 years ago

I see that your site is built using Docusaurus. To reflect the changed base URL in your build, you should use the baseUrl config setting: https://docusaurus.io/docs/api/docusaurus-config#baseUrl

You may wish to determine this value programmatically in your config.js file. Your preview workflow might for example set an environment variable containing the path of the preview deployment, which you could then pick up in your config, e.g.:

# .github/workflows/test-deploy.yml

...
jobs:
  test-deploy:
    ...
    steps:
      ...
      - run: echo "PREVIEW_PATH=pr-preview/pr-${{ github.event.number }}" >> "$GITHUB_ENV"
      - name: Build website
        run: yarn build
      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./docs/website/build/
// docs/website/docusaurus.config.js

let baseUrl = "/odo-gh-actions/"
if (process.env.PREVIEW_PATH) baseUrl += process.env.PREVIEW_PATH

module.exports = {
  ...
  baseUrl: baseUrl,
  ...
}

I'm going to record this as a documentation task, because while I feel that it's beyond the scope of the action to do this automatically, it should at least be mentioned in the documentation along with a pointer on how to get started setting it up.

james-s-w-clark commented 1 year ago

https://github.com/IdiosApps/docusaurus-pr-preview-gh/pull/3

Here's an example of this working in a public repo. The above suggestion works great.

0o-de-lally commented 1 month ago

Very helpful example. Perhaps this can be promoted somewhere in the docs? Thanks for a great tool!