Giter Club home page Giter Club logo

pr-preview-action's Introduction

Deploy PR Preview action

GitHub Action that deploys previews of pull requests to GitHub Pages. Works on any repository with a GitHub Pages site.

Features:

  • Creates and deploys previews of pull requests to your GitHub Pages site
  • Leaves a comment on the pull request with a link to the preview so that you and your team can collaborate on new features faster
  • Updates the deployment and the comment whenever new commits are pushed to the pull request
  • Cleans up after itself — removes deployed previews when the pull request is closed
  • Can be configured to override any of these behaviours

Preview URLs look like this: https://[owner].github.io/[repo]/pr-preview/pr-[number]/

Sample comment left by the action

Pictured: #1

This Action does not currently support deploying previews for PRs from forks, but will do so in the upcoming v2.

Usage

A GitHub Actions workflow is required to use this Action.

All the workflow needs to do first is checkout the repository and build the Pages site.

First, ensure that your repository is configured to have its GitHub Pages site deployed from a branch, by setting the source for the deployment under Settings > Pages of your repository to Deploy from branch:

GitHub Pages settings

Pictured: Repository Pages settings at /settings/page

The gh-pages branch is used for GitHub Pages deployments by convention, and will be used in examples here as well.

If your GitHub pages site is deployed from the gh-pages branch, built with e.g. an npm script to the ./build/ dir, and you're happy with the default settings, usage is very simple:

# .github/workflows/preview.yml
name: Deploy PR previews

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed

concurrency: preview-${{ github.ref }}

jobs:
  deploy-preview:
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install and Build
        if: github.event.action != 'closed' # You might want to skip the build if the PR has been closed
        run: |
          npm install
          npm run build

      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./build/

Important things to be aware of

Run only when files are changed

Consider limiting this workflow to run only when relevant files are edited to avoid deploying previews unnecessarily.

Run on all appropriate pull request events

Be sure to pick the right event types for the pull_request event. It only comes with opened, reopened, and synchronize by default — but this Action assumes by default that the preview should be removed during the closed event, which it only sees if you explicitly add it to the workflow.

Grant Actions permission to read and write to the repository

This must be changed in the repository settings by selecting "Read and write permissions" at Settings > Actions > General > Workflow permissions. Otherwise, the Action won't be able to make any changes to your deployment branch.

Set a concurrency group

I highly recommend setting a concurrency group scoped to each PR using github.ref as above, which should prevent the preview and comment from desynchronising if you are e.g. committing very frequently.

Ensure your main deployment is compatible

If you are using GitHub Actions to deploy your GitHub Pages sites (typically on push to the main branch), there are some actions you should take to avoid the PR preview overwriting the main deployment, or vice-versa.

  1. Prevent your main deployment from deleting previews

    If your root directory on the GitHub Pages deployment branch (or docs/ on the main branch) is generated automatically (e.g. on pushes to the main branch, with a tool such as Webpack), you will need to configure it not to remove the umbrella directory (pr-preview/ by default, see configuration below).

    For example, if you are using JamesIves/github-pages-deploy-action to deploy your build, you can implement this using its clean-exclude parameter:

    # .github/workflows/build-deploy-pages-site.yml
    steps:
      ...
      - uses: JamesIves/github-pages-deploy-action@v4
        ...
        with:
          clean-exclude: pr-preview/
          ...

    If you don't do this, your main deployment may delete all of your currently-existing PR previews.

  2. Don't force-push your main deployment

    Force-pushing your main deployment will cause it to overwrite any and all files in the deployment location. This will destroy any ongoing preview deployments. Instead, consider adjusting your deployment workflow to rebase or merge your main deployment onto the deployment branch such that it respects other ongoing deployments.

    For example, if you are using JamesIves/github-pages-deploy-action to deploy your build, be aware that at the time of writing (v4.3.0) it force-pushes new deployments by default. You can disable this by setting its force parameter to false, which will prompt it to rebase new deployments instead of force-pushing them:

    # .github/workflows/build-deploy-pages-site.yml
    steps:
      ...
      - uses: JamesIves/github-pages-deploy-action@v4
        ...
        with:
          force: false
          ...

    This feature was introduced in v4.3.0 of the above Action.

Configuration

The following configuration settings are provided, which can be passed to the with parameter.

  • source-dir: Directory containing files to deploy.

    E.g. if your project builds to ./dist/ you would put ./dist/ (or just dist) here. For the root directory of your project, put . here.

    Equivalent to JamesIves/github-pages-deploy-action 'folder' setting.

    Will be ignored when removing a preview.

    Default: .

  • preview-branch: Branch on which the previews will be deployed. This should be the same branch that your GitHub Pages site is deployed from.

    Default: gh-pages

  • umbrella-dir: Name of the directory containing all previews. All previews will be created inside this directory.

    The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

    Set to . to place preview directories into the root directory, but be aware that this may cause your main branch's deployment to interfere with your preview deployments (and vice-versa!)

    Default: pr-preview

  • custom-url: Base URL to use when providing a link to the preview site.

    Default: Will attempt to calculate the repository's GitHub Pages URL (e.g. "rossjrw.github.io").

  • deploy-repository: The repository to deploy the preview to.

    If this value is changed from the default, the token parameter must also be set (see below).

    Default: The current repository that the pull request was made in.

  • token: The token to use for the preview deployment. The default value is fine for deployments to the current repository, but if you want to deploy the preview to a different repository (see deploy-repository above), you will need to create a Personal Access Token with permission to access it, and store it as a secret in your repository. E.g. you might name that secret 'PREVIEW_TOKEN' and use it with token: ${{ secrets.PREVIEW_TOKEN }}.

    Default: ${{ github.token }}, which gives the action permission to deploy to the current repository.

  • (Advanced) action: Determines what this action will do when it is executed. Supported values: deploy, remove, none, auto.

    • deploy: will attempt to deploy the preview and overwrite any existing preview in that location.
    • remove: will attempt to remove the preview in that location.
    • auto: the action will try to determine whether to deploy or remove the preview based on the emitted event. It will deploy the preview on pull_request.types.opened, .reopened and .synchronize events; and remove it on pull_request.types.closed events. It will not do anything for all other events, even if you explicitly specify that the workflow should run on them.
    • none and all other values: the action will not do anything.

    Default value: auto

Outputs

  • deployment-url: the URL at which the preview has been deployed.

Examples

Full example

Full example with all default values added:

# .github/workflows/preview.yml
name: Deploy PR previews
concurrency: preview-${{ github.ref }}
on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed
jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm i && npm run build
        if: github.event.action != 'closed'
      - uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: .
          preview-branch: gh-pages
          umbrella-dir: pr-preview
          action: auto

...and an accompanying main deployment workflow:

# .github/workflows/deploy.yml
name: Deploy PR previews
on:
  push:
    branches:
      - main
jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm i && npm run build
      - uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: .
          branch: gh-pages
          clean-exclude: pr-preview
          force: false

Deployment from docs/

If your Pages site is built to build/ and deployed from the docs/ directory on the main branch:

# .github/workflows/preview.yml
steps:
  ...
  - uses: rossjrw/pr-preview-action@v1
    with:
      source-dir: build
      preview-branch: main
      umbrella-dir: docs/pr-preview

You should definitely limit this workflow to run only on changes to directories other than docs/, otherwise this workflow will call itself recursively.

Only remove previews for unmerged PRs

Information from the context and conditionals can be used to make more complex decisions about what to do with previews; for example, removing only those associated with unmerged PRs when they are closed:

# .github/workflows/preview.yml
steps:
  ...
  - uses: rossjrw/pr-preview-action@v1
    if: contains(['opened', 'reopened', 'synchronize'], github.event.action)
    with:
      source-dir: ./build/
      action: deploy
  - uses: rossjrw/pr-preview-action@v1
    if: github.event.action == "closed" && !github.event.pull_request.merged
    with:
      source-dir: ./build/
      action: remove

Permanent previews

If you want to keep PR previews around forever, even after the associated PR has been closed, you don't want the cleanup behaviour of auto — call deploy and never call remove:

# .github/workflows/everlasting-preview.yml
name: Deploy everlasting PR preview
concurrency: preview-${{ github.ref }}
on:
  pull_request:
    types:
      - opened
      - synchronize
jobs:
  deploy-preview:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - run: npm i && npm run build
      - uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./build/
          action: deploy

Acknowledgements

Big thanks to the following:

pr-preview-action's People

Contributors

boneskull avatar cxong avatar dan-hanlon avatar ejfrick avatar electrofelix avatar endbug avatar erikwrede avatar jleifeld avatar kolibril13 avatar mh4gf avatar netomi avatar noatamir avatar okinawaa avatar renestalder avatar rossjrw avatar vorburger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pr-preview-action's Issues

Do not create a new comment on removal

A comment should only be created when deploying a new preview. When that preview is removed, that comment should be edited, but it should not be created if it does not already exist.

This hasn't actually happened yet, but it will.

Investigate Deployment Reviews as a method of informed consent acquisition

As suggested by @aspiers in #6: Deployment Reviews as a potential workaround option for getting previews from forks by blocking the preview behind a manual review step, with information about the risks

  • Native GitHub feature that blocks a specific workflow from running until manually approved
  • Documentation is not 100% clear on whether it's a free feature / if it will work in free private repos
  • Totally unmentioned how to implement/configure the feature - can I pass a message explaining the risks?

Help setting up pr-previews

Hey

I am trying to use pr-previews for one of my projects - compressed-sensing/algorithms#11

I want my production website gh-pages branch and preview website preview-{pr-num} branch to be separate as I don't want the original production website to be overwritten whenever I create a pr

I also want multiple prs to have previews available along with the production website running so that we can compare.

Could you help me out? I think its doable with your action but Im struggling to set it up

Please allow passing the token

The JamesIves/github-pages-deploy-action and marocchino/sticky-pull-request-comment actions allow using personal access token in addition to the GITHUB_TOKEN. Please allow users of your action to also specify the token to be passed to the called actions.

Add ability to specify GitHub pages directory

Currently as far as I can tell, you are not able to specify the directory you are using to serve your GitHub pages project.
This means when the preview URL is generated it still includes the pages directory pointing to a broken link.

For example, my repo uses docs/ as the GitHub pages base directory, with the following config I get this output:

          source-dir: 'build'
          preview-branch: 'master'
          umbrella-dir: 'docs/preview'
          custom-url: 'example.site'

Output URL: https://example.site/docs/preview/pr-123
Expected URL: https://example.site/preview/pr-123

Incompatible with GitHub github.io convention

It seems that pr-preview-action is incompatible with the GitHub convention of naming the repository <org/user>.github.io where GitHub serves the pages with the website root at the origin of the site from the repo, rather than adding the repo name as the first path in the URL.

In particular this line

https://github.com/rossjrw/pr-preview-action/blob/main/action.yml#L63

just inserts github.io between the organisation name and the repo name. So I get

Screen Shot 2022-10-21 at 2 38 38 pm

as the preview URL. Everything else works fine, and I can edit the url so it is correct, but clearly in this case the logic is incorrect. I'm half convinced I must be missing something, because this is a long standing convention which I was sure the tool should understand.

I'm guessing some simple logic of checking if the repo name is already ends with <org/user>.github.io might be sufficient, in which case pagesurl should just be the .

Fail gracefully when run from fork

Running this action from a fork is not currently supported. Right now, behaviour when running from a fork is undefined; I don't remember exactly what happens, I think it just errors and tells you the action failed. It would be good to have some sort of non-fail (warning?) alert happen instead.

Combine with `actions/deploy-pages`

This workflow seems to target the "classic" GitHub Pages experience. However, nowadays there's also the newer workflow using https://github.com/marketplace/actions/deploy-github-pages-site

image

I'm trying to combine the two actions but so far I've been unsuccessful: either nothing happens, or I'm getting errors coming from actions/deploy-pages saying Branch "..." is not allowed to deploy to github-pages due to environment protection rules.

Do you have an example on how to use rossjrw/pr-preview-action with the new workflow?

Rename parameters to be consistent with JamesIves' action

It was a silly idea to make this action so parallel to JamesIves', while also renaming parameters.

E.g. 'source-dir' should be renamed to 'folder'.

This will result in much easier integration with projects using JamesIves/github-pages-deploy-action, which is the intended use case.

I should retain the current parameters, but mark them as deprecated.

Check for org pages repo matches unexpectedly

I'm using this action in https://github.com/vagrant-libvirt/vagrant-libvirt, unfortunately because the repo name matches the org name, the current behaviour of identifying repos for updating org gh-pages repos things that previews for the main application which should show up under https://vagrant-libvirt.github.io/vagrant-libvirt/ as though they should appear under https://vagrant-libvirt.github.io/

This can be seen with vagrant-libvirt/vagrant-libvirt#1733 (comment) which indicates the preview should appear as https://vagrant-libvirt.github.io/pr-preview/pr-1733/ when the correct URL is https://vagrant-libvirt.github.io/vagant-libvirt/pr-preview/pr-1733/

It appears this was caused by an improvement to try and generate the correct preview URLs for PRs in repos updating org pages

org=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 1)
thirdleveldomain=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2 | cut -d "." -f 1)
if [ ! -z "$customurl" ]; then
pagesurl="$customurl"
elif [ "$org" == "$thirdleveldomain" ]; then
pagesurl="${org}.github.io"

It appears that the code behaviour is expecting to match the following scenario:

GITHUB_REPOSITORY="vagrant-libvirt/vagrant-libvirt.github.io"

org=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 1)
thirdleveldomain=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2 | cut -d "." -f 1)
[ "$org" == "$thirdleveldomain" ] && echo "${org} == ${thirdleveldomain}"

Unfortunately it will also match on the following:

GITHUB_REPOSITORY="vagrant-libvirt/vagrant-libvirt"

org=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 1)
thirdleveldomain=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2 | cut -d "." -f 1)
[ "$org" == "$thirdleveldomain" ] && echo "${org} == ${thirdleveldomain}"

A simple fix should be to skip removal of the dot domain components:

org=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 1)
thirdleveldomain=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2)
[ "${org}.github.io" == "$thirdleveldomain" ] && echo "${org} == ${thirdleveldomain}"

will correctly skip outputting when GITHUB_REPOSITORY="vagrant-libvirt/vagrant-libvirt" while still detecting GITHUB_REPOSITORY="vagrant-libvirt/vagrant-libvirt.github.io"

not finding the content

Hello i have to sites with this feature.

One works perfectly after compiling all the files into html.

The other is just a site with static html file, doesnt compile nothing and these are the settings:
name: Deploy PR previews
on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
jobs:
deploy-preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy PR Preview
uses: rossjrw/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
preview-branch: gh-pages
source-dir: .
umbrella-dir: pr-preview
action: auto

     *When open the PR it provides a link with my preview but that preview is empty, does anyone knows how to fix it or maybe im putting the wrong source-dir?
     
     Any help is well appreaciated.
     Thanks.

Error: Resource not accessible by integration

In https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6727753248/job/18286039868 I have run into this:

Run marocchino/sticky-pull-request-comment@v2
  with:
    header: pr-preview
    message: [PR Preview Action](https://github.com/rossjrw/pr-preview-action) v1.4.4
  :---:
  :rocket: Deployed preview to https://www-learn-study.github.io/previews/pr-preview/pr-43/
  on branch [`gh-pages`](https://github.com/www-learn-study/saraswati.learn.study/tree/gh-pages) at 2023-11-02 03:00 UTC 
    append: false
    recreate: false
    delete: false
    only_create: false
    only_update: false
    hide_details: false
    hide: false
    hide_and_recreate: false
    hide_classify: OUTDATED
    ignore_empty: false
    skip_unchanged: false
    follow_symbolic_links: false
    GITHUB_TOKEN: ***
  env:
    action: deploy
    targetdir: pr-preview/pr-43
    pr: 43
    pagesurl: www-learn-study.github.io/previews
    emptydir: /tmp/tmp.NX01XUKSxB
    datetime: [202](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6727753248/job/18286039868#step:4:208)3-11-02 03:00 UTC
    actionref: v1
    actionrepo: rossjrw/pr-preview-action
    deployrepo: www-learn-study/previews
    token: PREVIEW_TOKEN
    action_version: v1.4.4
    deployment_status: success
Error: Resource not accessible by integration

I've figured out how to fix it (permissions: ...), and will post a PR with a proposed docs clarification.

Don't post preview comment until actual GitHub Pages deployment workflow completed?

Testing this out, and it works great! One thing I noticed is that, understandably, the action only waits for itself to finish to post the comment on the pull request. It would be great if that could somehow be delayed until the actual GitHub Pages deployment process has completed so that the preview link works as soon as it's posted. Right now, there's a short period of time between when the comment is posted and when the link will actually go somewhere.

I'm using this action in a website template that will be used by many non-technical users, and I could see them getting confused by there being a link but the preview not being ready yet.

As for how to implement this, I'm not sure. Is it possible for this Action to somehow add the associated GitHub Pages deployment run as a Status Check to the PR? Maybe it's possible to use action-wait-for-check or some similar action to wait for the deployment to succeed in the middle of running this action (I guess after committing to the gh-pages branch so the build starts, but before returning successfully).

Or maybe a stop gap could simply be adding a message to the pull request comment that clarifies that the preview WILL be available (after a short moment) at the following link.

PR Preview Action v1.2.0
Built preview to branch gh-pages at 2022-11-21 20:33 UTC
🚀 Your preview will be available at https://vincerubinetti.github.io/test/pr-preview/pr-1/
once GitHub finishes deploying it

Use links of the deploy preview, not actual deployment

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.

Resolve permissions error when preview is initiated from fork

https://github.com/scpwiki/interwiki/runs/5834681840?check_suite_focus=true

This workflow run failed because it did not have permission to push to the upstream repository.

I believe this happened because the workflow was executed from the fork. The pull_request event is executed in the context of the merge commit, whereas the pull_request_target event is executed in the base repository. For internal PRs, these are identical; for forks, they are not. In particular this means that the action will not have permission to push to the repository when using the pull_request event type, as its GITHUB_TOKEN lacks that permission.

Switching to the pull_request_target event might resolve this, but there are security considerations to take into account: https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

Changes shouldn't be needed to this action, but I will need to amend documentation to cover this use case. The README is getting pretty bloated - I should take this chance to move some of the code samples into the repository proper.

How to build on `gh-pages` when push on `main` ?

https://github.com/rossjrw/pr-preview-action is really convenient to get a quick pull request preview!
I'd like to ask: Is it possible to also build the main GitHub pages site on the gh-pages branch, when I push a commit to the main branch?
Currently, I use this action https://github.com/JamesIves/github-pages-deploy-action
But the problem with this approach:
The github-pages-deploy-action overwrites all files on the gh-pages branch, and therefore the files created by pr-preview-action will be overwritten.
Here are the two workflows:
https://github.com/kolibril13/okapi2/tree/main/.github/workflows

fatal git error is not detected (return value ignored?) and check is reported as succesful although it failed

https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6727951896/job/18286524048 from www-learn-study/saraswati.learn.study#42 has an example of a fatal git error what was not detected (return value ignored?) and check is reported as succesful although it failed:

(...)
Checking if there are files to commit…
/usr/bin/git add --all .
/usr/bin/git checkout -b github-pages-deploy-action/ii3mg6sxx
Switched to a new branch 'github-pages-deploy-action/ii3mg6sxx'
/usr/bin/git commit -m Deploy preview for PR 42 🛫 --quiet --no-verify
Pushing changes… (attempt 1 of 3)
/usr/bin/git push --porcelain ***github.com/www-learn-study/previews.git github-pages-deploy-action/ii3mg6sxx:gh-pages
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/www-learn-study/previews.git/'
Changes committed to the gh-pages branch… 📦
Running post deployment cleanup jobs… 🗑️
/usr/bin/git checkout -B github-pages-deploy-action/ii3mg6sxx
Reset branch 'github-pages-deploy-action/ii3mg6sxx'
/usr/bin/chmod -R +rw github-pages-deploy-action-temp-deployment-folder
/usr/bin/git worktree remove github-pages-deploy-action-temp-deployment-folder --force
Completed deployment successfully! ✅
Run echo "url=https://www-learn-study.github.io/previews/pr-preview/pr-42/"  >> $GITHUB_OUTPUT
Run marocchino/sticky-pull-request-comment@v2

How can I load gh-pages/pr-preview file?

this is my workflows file.

name: Create Preview Site
on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize

permissions:
  contents: write
  issues: write
  pull-requests: write
  id-token: write
  pages: write
  deployments: write

concurrency: preview-${{ github.ref }}

jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Build
        run: yarn install && yarn build

      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./dist/
          preview-branch: gh-pages
          umbrella-dir: pr-preview/
          action: auto

and this is my pr-preview/pr-22/assets/ on gh-pages branch.

image

But I can not access my index-dd097f21.js file on my deployed site for preview.

image

How can I access pr-preview directory on gh-pages branch?

Preview URL in comment doesn't match Enterprise deployment URL

I'm working on a private repo so I hope you not having access to it won't be an issue, but what I'm facing right now is that the comment gives me the following content:

Deployed preview to https://noimosai.github.io/noimos-ui-kit/pr-preview/pr-18/

However this link doesn't work and gives me a 404. I checked the gh-pages branch and the content is there. Apparently it is a new GitHub thing or I configured something wrong but my actual deployment is another URL:

https://friendly-adventure-lnjz19e.pages.github.io/ and I can access the preview content using the adapted URL https://friendly-adventure-lnjz19e.pages.github.io/pr-preview/pr-18/

Here is my config:
image

Did I configure something wrong ?

Use custom domain for preview

First, thanks for the Awesome Project!

I have a repo that uses a custom URL, but the actions still uses the normal GitHub Pages URL.
It would be great if there is a way to set a custom URL for the action to use, that would also help if someone is using this on another page host.

Branch footer link in posted comment is wrong when a `deploy-repository` is used

E.g. in www-learn-study/saraswati.learn.study#28 the https://www-learn-study.github.io/previews/pr-preview/pr-28/ URL is correct (there was a preview there, it's now removed again), but the https://github.com/www-learn-study/saraswati.learn.study/tree/gh-pages (from on branch gh-pages at 2023-11-02 05:08 UTC) is wrong... that should be https://github.com/www-learn-study/previews/tree/gh-pages - it's not using the deploy-repository there.

Race condition with build workflow

Just encountered a situation where the build and preview workflow occurred at the same time (during the merge of a PR). They both attempted to write a commit on top of the current gh-pages commit. This resulted in the gh-pages branch moving to the commit that removed the preview for the PR, whereas the commit that deployed the build for the new commit made to the main branch was lost.

I will need to investigate running workflows synchronously.

Customize pr name

Preview URLs look like this: https://[owner].github.io/[repo]/pr-preview/pr-[number]/

Can we customized the path pr-[number]. Doesnt seem like we can

Customise comment text

Relates to #25 (comment)

It'd be good to be able to customise the text of the comment left by the action.

I figure it should be possible to let the user pass a string argument which can be used as the message for the marocchino/sticky-pull-request-comment step, with the current message string being the default argument.

The user could be able to use this argument to opt-out of the comment, e.g. it could skip the step if the comment is false or if it's an empty string. That behaviour might even make it possible for the user to change whether or not they want to post a comment based on various conditions. Possible use case: user wants to use their own comment/notification mechanism.

I was initially worried about code injection (https://docs.github.com/en/enterprise-cloud@latest/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections), given that I'd have to pass a string containing variables to be interpolated, but I don't think this is actually a problem at all because the user will write that string in their workflow file and the variables will be interpolated there.

I'm not clear on how to get env variables into the string that will only be known at runtime, though (e.g. ${{ env.pagesurl }} in the current string. Might Just Work with no changes needed, might need some thought.

Cleanup is not happening as it should on PR close

We currently have 200+ orphaned directories under our PR previews. The action runs, but nothing is being deleted. Here is the output:


Checking configuration and starting deployment… 🚦
Deploying using Deploy Token… 🔑
Configuring git…
/usr/bin/git config --global --add safe.directory /home/runner/work/vision-web/vision-web
/usr/bin/git config user.name xaviergmail
/usr/bin/git config user.email [email protected]
/usr/bin/git config core.ignorecase false
/usr/bin/git config --local --unset-all http.https://github.com/.extraheader
/usr/bin/git remote rm origin
/usr/bin/git remote add origin ***github.com/<redacted>
Git configured… 🔧
Starting to commit changes…
/usr/bin/git ls-remote --heads ***github.com/<redacted>.git refs/heads/gh-pages
6460bcf9c7b548bce1544e269a8ee0d4e21cc652	refs/heads/gh-pages
Creating worktree…
/usr/bin/git fetch --no-recurse-submodules --depth=1 origin gh-pages
From https://github.com/<redacted>
  * branch            gh-pages   -> FETCH_HEAD
 * [new branch]      gh-pages   -> origin/gh-pages
/usr/bin/git worktree add --no-checkout --detach github-pages-deploy-action-temp-deployment-folder
Preparing worktree (detached HEAD ef1ddab)
/usr/bin/git checkout -B gh-pages origin/gh-pages
Previous HEAD position was ef1ddab Merge 47a6e8a6cdf30c80c317f0b6d0896a4b8be0995f into cb1eea009e14ea6d3f882c373f2094c4f144a4df
Switched to a new branch 'gh-pages'
branch 'gh-pages' set up to track 'origin/gh-pages'.
/usr/bin/chmod -R +rw /tmp/tmp.MoYqQjkDJm
Creating target folder if it doesn't already exist… 📌
/usr/bin/rsync -q -av --checksum --progress /tmp/tmp.MoYqQjkDJm/. github-pages-deploy-action-temp-deployment-folder/pr-previews/pr-1923 --delete --exclude CNAME --exclude .nojekyll --exclude .ssh --exclude .git --exclude .github
Checking if there are files to commit…
Running post deployment cleanup jobs… 🗑️
/usr/bin/git checkout -B github-pages-deploy-action/z4xrvq1b2
Switched to a new branch 'github-pages-deploy-action/z4xrvq1b2'
/usr/bin/chmod -R +rw github-pages-deploy-action-temp-deployment-folder
/usr/bin/git worktree remove github-pages-deploy-action-temp-deployment-folder --force
There is nothing to commit. Exiting early… 📭

Improve Doc about fatal: Authentication failed when using custom `token`

https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6727951896/job/18286524048 from www-learn-study/saraswati.learn.study#42 has an example of a fatal: Authentication failed in an Action using custom token (with a deploy-repository, but unrelated).

I think I have a suspicion what the problem is, and will validate a fix - and likely propose a PR with a minor documentation improvement to clarify how token must be set.

Can this run in a container? Getting error

I'm trying to run this action in a container like below:

name: Deploy PR previews

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed

concurrency: preview-${{ github.ref }}

jobs:
  deploy-preview:
    runs-on: [ self-hosted ]
    container: artifactory.mycompany.com/swr-docker-release/build-image/base:stable
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 14.17.0

      - name: Yarn install
        uses: borales/actions-yarn@v4
        with:
          cmd: install
          dir: ./app

      - name: Build production bundle
        uses: borales/actions-yarn@v4
        with:
          cmd: build
          dir: ./app

      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./app/dist/

Note that this uses the container option with an artifactory URI to grab the image.

The following error is encountered:

Run /home/myuser/runners/01/_work/_actions/rossjrw/pr-preview-action/v1/lib/find-current-git-tag.sh -p $actionrepo -f $actionref
  /home/myuser/runners/01/_work/_actions/rossjrw/pr-preview-action/v1/lib/find-current-git-tag.sh -p $actionrepo -f $actionref
  shell: bash --noprofile --norc -e -o pipefail {0}
  env:
    action: auto
    targetdir: pr-preview/pr-232
    pr: 232
    pagesurl: swr.github.io/sre-portal
    emptydir: /tmp/tmp.GEsaOyeWhF
    datetime: 2023-04-26 05:10 UTC
    actionref: v1
    actionrepo: rossjrw/pr-preview-action
    deployrepo: swr/sre-portal
    token: ***
/__w/_temp/0dec96f1-b2e1-4f04-a548-60ec5383fe95.sh: line 1: /home/myuser/runners/01/_work/_actions/rossjrw/pr-preview-action/v1/lib/find-current-git-tag.sh: No such file or directory

`uses:` keyword is not currently supported

I have googled around the error uses: keyword is not currently supported, but couldn't figure out how to fix this issue (other actions are working fine, and they use uses). It's the first time I've seen this, so I thought I'd raise an issue here.

I can see that the Action works nice for public repos:

https://github.com/rossjrw/pr-preview-action/tree/gh-pages/pr-preview/pr-8
#8

I'm trying this in GitHub Enterprise on a self-hosted runner, but it fails with a really weird error.

Action run logs

2023-02-14T18:56:00.7683072Z Found online and idle self-hosted runner(s) in the current repository's organization/enterprise account that matches the required labels: 'self-hosted'
2023-02-14T18:56:00.7683941Z Waiting for a self-hosted runner to pick up this job...
2023-02-14T18:56:10.0154451Z Current runner version: '2.302.0'
2023-02-14T18:56:10.0162322Z Runner name: 'my-runner'
2023-02-14T18:56:10.0162762Z Runner group name: 'Default'
2023-02-14T18:56:10.0163385Z Machine name: 'abc123def'
2023-02-14T18:56:10.0165051Z ##[group]GITHUB_TOKEN Permissions
2023-02-14T18:56:10.0165679Z Actions: write
2023-02-14T18:56:10.0165898Z Checks: write
2023-02-14T18:56:10.0166176Z Contents: write
2023-02-14T18:56:10.0166437Z Deployments: write
2023-02-14T18:56:10.0166676Z Discussions: write
2023-02-14T18:56:10.0166887Z Issues: write
2023-02-14T18:56:10.0167107Z Metadata: read
2023-02-14T18:56:10.0167320Z Packages: write
2023-02-14T18:56:10.0167539Z PullRequests: write
2023-02-14T18:56:10.0167798Z RepositoryProjects: write
2023-02-14T18:56:10.0168015Z SecurityEvents: write
2023-02-14T18:56:10.0168300Z Statuses: write
2023-02-14T18:56:10.0168521Z ##[endgroup]
2023-02-14T18:56:10.0172360Z Prepare workflow directory
2023-02-14T18:56:10.1738386Z Prepare all required actions
2023-02-14T18:56:10.2070688Z Getting action download info
2023-02-14T18:56:16.5313348Z Download action repository 'actions/checkout@v3' (SHA:ac593985615ec2ede58e132d2e21d2b1cbd6127c)
2023-02-14T18:56:16.8288999Z Download action repository 'rossjrw/pr-preview-action@v1' (SHA:eac2838daf487e8f054a4bc10dc957431cd6270b)
2023-02-14T18:56:17.2948704Z ##[error]`uses:` keyword is not currently supported.

Workflow yaml

Indentaiton may be off due to copy paste, but here is the action - pretty much the same as an example from the readme:

name: Deploy PR previews
concurrency: preview-${{ github.ref }}
 
on:
  pull_request:
    types:
    - opened
    - reopened
    - synchronize
    - closed
jobs:
  deploy-preview:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v3
      - run: yarn install && yarn build
      - uses: rossjrw/pr-preview-action@v1

If I remove - uses: rossjrw/pr-preview-action@v1, the logs manage to checkout and then try and install yarn etc.
So, it seems like it gets confused on the very last line.

Error when setting action version via SHA

In https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20 I ran into this:

Run rossjrw/pr-preview-action@183082fd714654433c8e2f6daedbfb4f20f2a94a
  with:
    source-dir: BUILT/site/
    deploy-repository: www-learn-study/previews
    preview-branch: gh-pages
    umbrella-dir: pr-preview
    action: auto
Run echo "action=$action" >> $GITHUB_ENV
  echo "action=$action" >> $GITHUB_ENV
  echo "targetdir=$umbrella/pr-$pr" >> $GITHUB_ENV
  echo "pr=$pr" >> $GITHUB_ENV
  
  org=$(echo "$deployrepo" | cut -d "/" -f 1)
  thirdleveldomain=$(echo "$deployrepo" | cut -d "/" -f [2](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:2))
  
  if [ ! -z "$customurl" ]; then
    pagesurl="$customurl"
  elif [ "${org}.github.io" == "$thirdleveldomain" ]; then
    pagesurl="${org}.github.io"
  else
    pagesurl=$(echo "$deployrepo" | sed 's/\//.github.io\//')
  fi
  
  echo "pagesurl=$pagesurl" >> $GITHUB_ENV
  
  echo "emptydir=$(mktemp -d)" >> $GITHUB_ENV
  echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
  
  echo "actionref=$actionref" >> $GITHUB_ENV
  echo "actionrepo=$actionrepo" >> $GITHUB_ENV
  echo "deployrepo=$deployrepo" >> $GITHUB_ENV
  echo "token=$token" >> $GITHUB_ENV
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    action: auto
    umbrella: pr-preview
    pr: 20
    actionref: 18[3](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:3)082fd71[4](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:4)6[5](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:5)4433c8e2f[6](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:6)daedbfb4f20f2a94a
    actionrepo: rossjrw/pr-preview-action
    customurl: 
    deployrepo: www-learn-study/previews
    token: 
Run action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p $actionrepo -f $actionref)
  action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p $actionrepo -f $actionref)
  echo "action_version=$action_version" >> "$GITHUB_ENV"
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    action: auto
    targetdir: pr-preview/pr-20
    pr: 20
    pagesurl: www-learn-study.github.io/previews
    emptydir: /tmp/tmp.bCj8nD[7](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:7)DOE
    datetime: 2023-11-02 06:39 UTC
    actionref: 1[8](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:9)3082fd714654433c8e2f6daedbfb4f20f2a[9](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:10)4a
    actionrepo: rossjrw/pr-preview-action
    deployrepo: www-learn-study/previews
    token: 
Determining Git tag for rossjrw/pr-preview-action/183082fd7[14](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:15)654433c8e2f6daedbfb4f20f2a94a
Cloning repository rossjrw/pr-preview-action at ref [18](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:19)3082fd714654433c8e2f6daedbfb4f[20](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:21)f2a94a
Cloning into bare repository 'bare_pr_preview'...
warning: Could not find remote branch 183082fd714654433c8e2f6daedbfb4f20f2a94a to clone.
fatal: Remote branch 183082fd714654433c8e2f6daedbfb4f20f2a94a not found in upstream origin
/home/runner/work/_actions/rossjrw/pr-preview-action/183082fd714654433c8e2f6daedbfb4f20f2a94a/lib/find-current-git-tag.sh: line [29](https://github.com/www-learn-study/saraswati.learn.study/actions/runs/6729181929/job/18289629833?pr=20#step:5:30): cd: bare_pr_preview: No such file or directory
Error: Process completed with exit code 1.

Comment shows target repo version, not action version

We have a lot of different tags in our repo using it in documentation preview title is kind of silly. Please add feature to change or remove it, it would make our PR comment much nicer!

Example of how it looks right now:
image

Support custom 404 previews

Hello, thanks for making this great Action.
I'm trying to test adding a new 404 page and wondered if I'm going about it the wrong way.

The docs for GitHub Pages say that you can have a custom 404 page by putting a 404.html in the root of the deployment.

If I create a 404 page and add it as part of a PR, I can see it by going to https://[owner].github.io/[repo]/pr-preview/pr-[number]/404.html but it does not function as a catch-all, for example when I go to https://[owner].github.io/[repo]/pr-preview/pr-[number]/this/page/does/not/exist I see the default GitHub Pages 404 instead of mine. Would it be possible to get this working?

404 page not found

After following the Usage, I'm able to see deploy preview link in the PR. However, when I access the link, I get taken to a 404 page not found.

In the screenshot below, it shows the gh-pages branch which contains my index.html file (ie: pr-preview > pr-90 > index.html). Am I missing a step?

Screen Shot 2022-08-17 at 5 15 39 PM

Below is my config. In the master branch, my index.html lives in docs > index.html

name: Deploy PR previews

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed

concurrency: preview-${{ github.ref }}

jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14
          registry-url: 'https://registry.npmjs.org'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

      - name: Install and Build
        run: |
          npm install
          npm run build
          npm run build-storybook

      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: ./docs/

Customize the directory to serve from

Our Github Pages configuration is set up to serve from a subdirectory and there is currently no documented way to address this with pr-preview-action.

Example:

Preview Deployment Action

      - name: Deploy Preview
        uses: rossjrw/pr-preview-action@v1
        with:
          source-dir: docs
          preview-branch: gh-pages
          umbrella-dir: docs/pr-preview
          action: auto

Deployment Action

      - uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: docs
          target-folder: docs
          branch: gh-pages
          clean-exclude: docs/pr-preview

With the above configurations, we end up with everything correctly being saved to the docs directory.  The generated preview link is /docs/pr-preview/pr-xx.  I can delete the /docs path part and see the preview just fine, but the generated link shows a totally valid 404 because we are serving directly from the docs directory that things are being saved to rather than serving from the root directory.

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.