From 83ffe76d6ea8d2b5d473fa47f86713a05e2ff53d Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 11:48:27 +0000 Subject: [PATCH 01/15] Concat comment lines --- action.yml | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/action.yml b/action.yml index bf291a3..afd5691 100644 --- a/action.yml +++ b/action.yml @@ -156,21 +156,11 @@ runs: uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview - message: "\ - [PR Preview Action]\ - (https://github.com/${{ env.actionrepo }}) - ${{ env.action_version }} - + message: | + [PR Preview Action](https://github.com/${{ env.actionrepo }}) ${{ env.action_version }} :---: - - :rocket: Deployed preview to - https://${{ env.pagesurl }}/${{ env.pagespath }}/ - - on branch [`${{ inputs.preview-branch }}`](\ - ${{ github.server_url }}/${{ env.deployrepo }}\ - /tree/${{ inputs.preview-branch }}) - at ${{ env.datetime }} - " + :rocket: Deployed preview to https://${{ env.pagesurl }}/${{ env.pagespath }}/ + on branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ env.deployrepo }}/tree/${{ inputs.preview-branch }}) at ${{ env.datetime }} - name: Remove preview directory if: env.action == 'remove' @@ -189,14 +179,8 @@ runs: uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview - message: "\ - [PR Preview Action]\ - (https://github.com/${{ env.actionrepo }}) - ${{ env.action_version }} - + message: | + [PR Preview Action](https://github.com/${{ env.actionrepo }}) ${{ env.action_version }} :---: - Preview removed because the pull request was closed. - ${{ env.datetime }} - " From feadd1d062fb10737427c773f239dc836b9df389 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 12:37:03 +0000 Subject: [PATCH 02/15] Extract action procedure to script file --- action.yml | 48 ++---------------------------------- lib/determine-auto-action.sh | 38 ++++++++++++++-------------- lib/main.sh | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 65 deletions(-) create mode 100755 lib/main.sh diff --git a/action.yml b/action.yml index afd5691..36b235d 100644 --- a/action.yml +++ b/action.yml @@ -76,7 +76,7 @@ outputs: runs: using: composite steps: - - name: Store environment variables + - name: Setup preview environment env: action: ${{ inputs.action }} umbrella: ${{ inputs.umbrella-dir }} @@ -87,51 +87,7 @@ runs: customurl: ${{ inputs.custom-url }} deployrepo: ${{ inputs.deploy-repository }} token: ${{ inputs.token }} - run: | - echo "action=$action" >> $GITHUB_ENV - echo "pr=$pr" >> $GITHUB_ENV - - repo_org=$(echo "$deployrepo" | cut -d "/" -f 1) - repo_name=$(echo "$deployrepo" | cut -d "/" -f 2) - - if [ ! -z "$customurl" ]; then - pagesurl="$customurl" - elif [ "${repo_org}.github.io" == "$repo_name" ]; then - pagesurl="${repo_org}.github.io" - else - pagesurl=$(echo "$deployrepo" | sed 's/\//.github.io\//') - fi - - echo "pagesurl=$pagesurl" >> $GITHUB_ENV - - targetdir="$umbrella/pr-$pr" - echo "targetdir=$targetdir" >> $GITHUB_ENV - - pagespath=$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "$pagesbase" -o "$targetdir") - if [ -n "$pagesbase" ] && [ "$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "" -o "$targetdir")" = "$pagespath" ]; then - echo "::warning title=pages-base-path doesn't match::pages-base-path directory ($pagesbase) does not contain umbrella-dir ($umbrella). pages-base-path has been ignored." - pagespath=$targetdir - fi - echo "pagespath=$pagespath" >> $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: bash - - - name: Determine action version - run: | - action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p $actionrepo -f $actionref) - echo "action_version=$action_version" >> "$GITHUB_ENV" - shell: bash - - - name: Determine auto action - if: env.action == 'auto' - run: $GITHUB_ACTION_PATH/lib/determine-auto-action.sh + run: $GITHUB_ACTION_PATH/lib/main.sh shell: bash - name: Deploy preview directory diff --git a/lib/determine-auto-action.sh b/lib/determine-auto-action.sh index 55d16d9..1a2348c 100755 --- a/lib/determine-auto-action.sh +++ b/lib/determine-auto-action.sh @@ -1,28 +1,28 @@ #!/usr/bin/env bash case $GITHUB_EVENT_NAME in - "pull_request" | "pull_request_target") - echo "event_name is $GITHUB_EVENT_NAME; proceeding" - ;; - *) - echo "unknown event $GITHUB_EVENT_NAME; no action to take" - echo "action=none" >> "$GITHUB_ENV" - exit 0 - ;; +"pull_request" | "pull_request_target") + echo >&2 "event_name is $GITHUB_EVENT_NAME; proceeding" + ;; +*) + echo >&2 "unknown event $GITHUB_EVENT_NAME; no action to take" + echo "none" + exit 0 + ;; esac event_type=$(jq -r ".action" "$GITHUB_EVENT_PATH") -echo "event_type is $event_type" +echo >&2 "event_type is $event_type" case $event_type in - "opened" | "reopened" | "synchronize") - echo "action=deploy" >> "$GITHUB_ENV" - ;; - "closed") - echo "action=remove" >> "$GITHUB_ENV" - ;; - *) - echo "unknown event type $event_type; no action to take" - echo "action=none" >> "$GITHUB_ENV" - ;; +"opened" | "reopened" | "synchronize") + echo "deploy" + ;; +"closed") + echo "remove" + ;; +*) + echo >&2 "unknown event type $event_type; no action to take" + echo "none" + ;; esac diff --git a/lib/main.sh b/lib/main.sh new file mode 100755 index 0000000..99d12b3 --- /dev/null +++ b/lib/main.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +declare action pr deployrepo customurl pagesbase umbrella actionref actionrepo token + +repo_org=$(echo "$deployrepo" | cut -d "/" -f 1) +repo_name=$(echo "$deployrepo" | cut -d "/" -f 2) + +if [ -n "$customurl" ]; then + pagesurl="$customurl" +elif [ "${repo_org}.github.io" == "$repo_name" ]; then + pagesurl="${repo_org}.github.io" +else + pagesurl=$(echo "$deployrepo" | sed 's/\//.github.io\//') +fi + +targetdir="$umbrella/pr-$pr" + +pagespath=$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "$pagesbase" -o "$targetdir") +if [ -n "$pagesbase" ] && [ "$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "" -o "$targetdir")" = "$pagespath" ]; then + echo "::warning title=pages-base-path doesn't match::pages-base-path directory ($pagesbase) does not contain umbrella-dir ($umbrella). pages-base-path has been ignored." + pagespath=$targetdir +fi + +if [ "$action" = "auto" ]; then + echo >&2 "Determining auto action" + action=$("$GITHUB_ACTION_PATH/lib/determine-auto-action.sh") + echo >&2 "Auto action is $action" +fi + +# Export variables for later use by this action +{ + echo "emptydir=$(mktemp -d)" + echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" + + echo "action=$action" + echo "pr=$pr" + + echo "targetdir=$targetdir" + echo "pagesurl=$pagesurl" + echo "pagespath=$pagespath" + + echo "actionref=$actionref" + echo "actionrepo=$actionrepo" + echo "action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p "$actionrepo" -f "$actionref")" + + echo "deployrepo=$deployrepo" + echo "token=$token" +} >>"$GITHUB_ENV" From 4f3076a989052cfe8690febdf6c44c73fbf78d5e Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 13:07:35 +0000 Subject: [PATCH 03/15] Try exporting output from variables directly --- action.yml | 13 ++++++------- lib/main.sh | 9 +++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index 36b235d..bdd69dc 100644 --- a/action.yml +++ b/action.yml @@ -70,13 +70,17 @@ inputs: outputs: deployment-url: - description: The URL at which the preview has been deployed - value: ${{ steps.url.outputs.url }} + description: Preview deployment URL + value: ${{ steps.setup.outputs.preview_url }} + url: + description: Preview deployment URL + value: https://${{ env.pagesurl }}/${{ env.pagespath }}/ runs: using: composite steps: - name: Setup preview environment + id: setup env: action: ${{ inputs.action }} umbrella: ${{ inputs.umbrella-dir }} @@ -102,11 +106,6 @@ runs: commit-message: Deploy preview for PR ${{ env.pr }} 🛫 force: false - - name: Expose deployment URL - id: url - run: echo "url=https://${{ env.pagesurl }}/${{ env.pagespath }}/" >> $GITHUB_OUTPUT - shell: bash - - name: Leave a comment after deployment if: env.action == 'deploy' && env.deployment_status == 'success' uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 diff --git a/lib/main.sh b/lib/main.sh index 99d12b3..fe9355b 100755 --- a/lib/main.sh +++ b/lib/main.sh @@ -46,3 +46,12 @@ fi echo "deployrepo=$deployrepo" echo "token=$token" } >>"$GITHUB_ENV" + +# Export variables for use by later actions in user workflow +{ + echo "pages_base_url=$pagesurl" + echo "preview_url_path=$pagespath" + echo "preview_url=https://$pagesurl/$pagespath/" + echo "file_path=$targetdir" + echo "action=$action" +} >>"$GITHUB_OUTPUT" From f4acd0b1955c15a746b480b9008effec40fb1601 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 16:00:24 +0000 Subject: [PATCH 04/15] Clean up internal variable names --- README.md | 2 +- action.yml | 56 ++++++++++++++++++++-------------------- lib/main.sh | 73 ++++++++++++++++++++++++++++------------------------- 3 files changed, 67 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 5b5dfe7..a6f958b 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ the `with` parameter. Default: `gh-pages` -- `umbrella-dir`: Name of the directory containing all previews. All +- `umbrella-dir`: Path to the directory containing all previews. All previews will be created inside this directory. The umbrella directory is used to namespace previews from your main diff --git a/action.yml b/action.yml index bdd69dc..a48c0d4 100644 --- a/action.yml +++ b/action.yml @@ -22,7 +22,7 @@ inputs: required: false default: gh-pages umbrella-dir: - description: Name of the directory containing all previews. + description: Path to the directory containing all previews. required: false default: pr-preview pages-base-path: @@ -74,7 +74,7 @@ outputs: value: ${{ steps.setup.outputs.preview_url }} url: description: Preview deployment URL - value: https://${{ env.pagesurl }}/${{ env.pagespath }}/ + value: https://${{ env.pages_base_url }}/${{ env.preview_url_path }}/ runs: using: composite @@ -82,60 +82,60 @@ runs: - name: Setup preview environment id: setup env: - action: ${{ inputs.action }} - umbrella: ${{ inputs.umbrella-dir }} - pagesbase: ${{ inputs.pages-base-path }} - pr: ${{ github.event.number }} - actionref: ${{ github.action_ref }} - actionrepo: ${{ github.action_repository }} - customurl: ${{ inputs.custom-url }} - deployrepo: ${{ inputs.deploy-repository }} + deployment_action: ${{ inputs.action }} + umbrella_path: ${{ inputs.umbrella-dir }} + pages_base_path: ${{ inputs.pages-base-path }} + pr_number: ${{ github.event.number }} + github_action_ref: ${{ github.action_ref }} + github_action_repository: ${{ github.action_repository }} + custom_pages_base_url: ${{ inputs.custom-url }} + deployment_repository: ${{ inputs.deploy-repository }} token: ${{ inputs.token }} run: $GITHUB_ACTION_PATH/lib/main.sh shell: bash - name: Deploy preview directory - if: env.action == 'deploy' + if: env.deployment_action == 'deploy' uses: JamesIves/github-pages-deploy-action@65b5dfd4f5bcd3a7403bbc2959c144256167464e # v4.5.0 with: - token: ${{ env.token }} - repository-name: ${{ env.deployrepo }} + token: ${{ inputs.token }} + repository-name: ${{ inputs.deploy-repository }} branch: ${{ inputs.preview-branch }} folder: ${{ inputs.source-dir }} - target-folder: ${{ env.targetdir }} - commit-message: Deploy preview for PR ${{ env.pr }} 🛫 + target-folder: ${{ env.preview_file_path }} + commit-message: Deploy preview for PR ${{ github.event.number }} 🛫 force: false - name: Leave a comment after deployment - if: env.action == 'deploy' && env.deployment_status == 'success' + if: env.deployment_action == 'deploy' && env.deployment_status == 'success' uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview message: | - [PR Preview Action](https://github.com/${{ env.actionrepo }}) ${{ env.action_version }} + [PR Preview Action](https://github.com/${{ github.action_repository }}) ${{ env.action_version }} :---: - :rocket: Deployed preview to https://${{ env.pagesurl }}/${{ env.pagespath }}/ - on branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ env.deployrepo }}/tree/${{ inputs.preview-branch }}) at ${{ env.datetime }} + :rocket: Deployed preview to https://${{ env.pages_base_url }}/${{ env.preview_url_path }}/ + on branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} - name: Remove preview directory - if: env.action == 'remove' + if: env.deployment_action == 'remove' uses: JamesIves/github-pages-deploy-action@65b5dfd4f5bcd3a7403bbc2959c144256167464e # v4.5.0 with: - token: ${{ env.token }} - repository-name: ${{ env.deployrepo }} + token: ${{ inputs.token }} + repository-name: ${{ inputs.deploy-repository }} branch: ${{ inputs.preview-branch }} - folder: ${{ env.emptydir }} - target-folder: ${{ env.targetdir }} - commit-message: Remove preview for PR ${{ env.pr }} 🛬 + folder: ${{ env.empty_dir_path }} + target-folder: ${{ env.preview_file_path }} + commit-message: Remove preview for PR ${{ github.event.number }} 🛬 force: false - name: Leave a comment after removal - if: env.action == 'remove' && env.deployment_status == 'success' + if: env.deployment_action == 'remove' && env.deployment_status == 'success' uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview message: | - [PR Preview Action](https://github.com/${{ env.actionrepo }}) ${{ env.action_version }} + [PR Preview Action](https://github.com/${{ github.action_repository }}) ${{ env.action_version }} :---: Preview removed because the pull request was closed. - ${{ env.datetime }} + ${{ env.action_start_time }} diff --git a/lib/main.sh b/lib/main.sh index fe9355b..26249af 100755 --- a/lib/main.sh +++ b/lib/main.sh @@ -1,57 +1,60 @@ #!/usr/bin/env bash -declare action pr deployrepo customurl pagesbase umbrella actionref actionrepo token +declare deployment_action pr_number deployment_repository custom_pages_base_url pages_base_path umbrella_path github_action_ref github_action_repository -repo_org=$(echo "$deployrepo" | cut -d "/" -f 1) -repo_name=$(echo "$deployrepo" | cut -d "/" -f 2) +repo_org=$(echo "$deployment_repository" | cut -d "/" -f 1) +repo_name=$(echo "$deployment_repository" | cut -d "/" -f 2) -if [ -n "$customurl" ]; then - pagesurl="$customurl" -elif [ "${repo_org}.github.io" == "$repo_name" ]; then - pagesurl="${repo_org}.github.io" +if [ -n "$custom_pages_base_url" ]; then + pages_base_url="$custom_pages_base_url" +elif [ "$repo_name" = "${repo_org}.github.io" ]; then + pages_base_url="${repo_org}.github.io" else - pagesurl=$(echo "$deployrepo" | sed 's/\//.github.io\//') + pages_base_url=$(echo "$deployment_repository" | sed 's/\//.github.io\//') fi -targetdir="$umbrella/pr-$pr" +preview_file_path="$umbrella_path/pr-$pr_number" -pagespath=$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "$pagesbase" -o "$targetdir") -if [ -n "$pagesbase" ] && [ "$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "" -o "$targetdir")" = "$pagespath" ]; then - echo "::warning title=pages-base-path doesn't match::pages-base-path directory ($pagesbase) does not contain umbrella-dir ($umbrella). pages-base-path has been ignored." - pagespath=$targetdir +preview_url_path=$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "$pages_base_path" -o "$preview_file_path") +if [ -n "$pages_base_path" ] && [ "$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "" -o "$preview_file_path")" = "$preview_url_path" ]; then + echo "::warning title=pages-base-path doesn't match::The pages-base-path directory ($pages_base_path) does not contain umbrella-dir ($umbrella_path). pages-base-path has been ignored. The value of umbrella-dir should start with the value of pages-base-path." + preview_url_path=$preview_file_path fi -if [ "$action" = "auto" ]; then +if [ "$deployment_action" = "auto" ]; then echo >&2 "Determining auto action" - action=$("$GITHUB_ACTION_PATH/lib/determine-auto-action.sh") - echo >&2 "Auto action is $action" + deployment_action=$("$GITHUB_ACTION_PATH/lib/determine-auto-action.sh") + echo >&2 "Auto action is $deployment_action" fi +action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p "$github_action_repository" -f "$github_action_ref") +action_start_timestamp=$(date '+%s') +action_start_time=$(date '+%Y-%m-%d %H:%M %Z') + # Export variables for later use by this action { - echo "emptydir=$(mktemp -d)" - echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" - - echo "action=$action" - echo "pr=$pr" - - echo "targetdir=$targetdir" - echo "pagesurl=$pagesurl" - echo "pagespath=$pagespath" + echo "empty_dir_path=$(mktemp -d)" + echo "deployment_action=$deployment_action" - echo "actionref=$actionref" - echo "actionrepo=$actionrepo" - echo "action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p "$actionrepo" -f "$actionref")" + echo "preview_file_path=$preview_file_path" + echo "pages_base_url=$pages_base_url" + echo "preview_url_path=$preview_url_path" + echo "preview_url=https://$pages_base_url/$preview_url_path/" - echo "deployrepo=$deployrepo" - echo "token=$token" + echo "action_version=$action_version" + echo "action_start_time=$action_start_time" } >>"$GITHUB_ENV" # Export variables for use by later actions in user workflow { - echo "pages_base_url=$pagesurl" - echo "preview_url_path=$pagespath" - echo "preview_url=https://$pagesurl/$pagespath/" - echo "file_path=$targetdir" - echo "action=$action" + echo "deployment_action=$deployment_action" + + echo "preview_file_path=$preview_file_path" + echo "pages_base_url=$pages_base_url" + echo "preview_url_path=$preview_url_path" + echo "preview_url=https://$pages_base_url/$preview_url_path/" + + echo "action_version=$action_version" + echo "action_start_timestamp=$action_start_timestamp" + echo "action_start_time=$action_start_time" } >>"$GITHUB_OUTPUT" From 2a0d59b6cf0331708fae46dcd0684547924f75ee Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 16:57:36 +0000 Subject: [PATCH 05/15] Add and document output values --- README.md | 150 ++++++++++++++++++---------------------------------- action.yml | 27 ++++++++-- lib/main.sh | 1 - 3 files changed, 74 insertions(+), 104 deletions(-) diff --git a/README.md b/README.md index a6f958b..5ce09d3 100644 --- a/README.md +++ b/README.md @@ -89,15 +89,15 @@ jobs: source-dir: ./build/ ``` -### Important things to be aware of +## Important things to be aware of -#### Run only when files are changed +### Run only when files are changed Consider limiting this workflow to run [only when relevant files are edited](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore) to avoid deploying previews unnecessarily. -#### Run on all appropriate pull request events +### Run on all appropriate pull request events Be sure to [pick the right event types](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request) @@ -106,14 +106,14 @@ for the `pull_request` event. It only comes with `opened`, `reopened`, and 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 +### 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 +### Set a concurrency group I highly recommend [setting a concurrency group](https://docs.github.com/en/actions/using-jobs/using-concurrency) @@ -121,7 +121,7 @@ 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 +### 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 @@ -161,11 +161,11 @@ vice-versa. 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. + branch to respect other ongoing deployments. For example, if you are using [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) - to deploy your build, be aware that at the time of writing (v4.3.0) it + to deploy your build, be aware that at the time of writing (v4.7.2) 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: @@ -183,96 +183,50 @@ vice-versa. This feature was introduced in v4.3.0 of the above Action. -#### +# Inputs (configuration) -## Configuration +The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value. -The following configuration settings are provided, which can be passed to -the `with` parameter. +Input parameter | Description +--- | --- +`source-dir` | When creating or updating a preview, the path to the directory that contains the files to deploy. E.g. if your project builds to `./dist/` you would put `./dist/` (or `dist`, etc.).
Equivalent to [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) 'folder' setting.

Default: `.` (repository root) +`deploy-repository` | The repository to deploy the preview to.
__Note:__ The `token` parameter must also be set if changing this from the default.

Default: The pull request's target repository. +`preview-branch` | Branch to save previews to. This should be the same branch that your GitHub Pages site is deployed from.

Default: `gh-pages` +`umbrella-dir` | Path to the directory to place previews in.
The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

Default: `pr-preview` +`custom-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) +`pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL, this is removed from the beginning of the path.

Default: `.` (repository root) +`token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. +`action`
(Advanced) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` -- `source-dir`: Directory containing files to deploy. +# Outputs - 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. +Several output values are provided to use after this Action in your workflow. To use them, give this Action's step an `id` and reference the value with `${{ steps..outputs. }}`, e.g.: - Equivalent to - [JamesIves/github-pages-deploy-action](https://github.com/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`: Path to 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` - -- `pages-base-path`: Path that GitHub Pages is being served from, as configured in your repository settings. When generating the preview URL, this is removed from the beginning of the path. - - Default: ` ` (repository root) - -- `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](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) - with permission to access it, and [store it as a - secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) - 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](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). - 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` +```yml +# .github/workflows/preview.yml +jobs: + deploy-preview: + steps: + - uses: rossjrw/pr-preview-action@v1 + id: preview-step + - run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" +``` -## Outputs +You could use these outputs to e.g. write your own sticky comment after the Action has run. -- `deployment-url`: the URL at which the preview has been deployed. +Output | Description +--- | --- +`deployment-action` | Resolved value of the `action` input parameter (deploy, remove, none). +`pages-base-url` | What this Action thinks the base URL of the GitHub Pages site is. +`preview-url-path` | Path to the preview from the Pages base URL. +`preview-url` | Full URL to the preview (`https:////`). +`action-version` | The version of this Action when it was run. +`action-start-timestamp` | The time that the workflow step started as a Unix timestamp. +`action-start-time` | The time that the workflow step started in a readable format (UTC, depending on runner). -## Examples +# Examples -### Full example +## Full example Full example with all default values added: @@ -325,7 +279,7 @@ jobs: force: false ``` -### Deployment from `docs/` +## Deployment from `docs/` If your Pages site is built to `build/` and deployed from the `docs/` directory on the `main` branch: @@ -345,7 +299,7 @@ steps: 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 +## 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 @@ -367,7 +321,7 @@ steps: action: remove ``` -### Permanent previews +## 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` — @@ -394,13 +348,11 @@ jobs: action: deploy ``` -## Acknowledgements +# Acknowledgements Big thanks to the following: -- [shlinkio/deploy-preview-action](https://github.com/shlinkio/deploy-preview-action) - (MIT), prior art that informed the direction of this Action -- [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) - (MIT), used by this Action to deploy previews -- [marocchino/sticky-pull-request-comment](https://github.com/marocchino/sticky-pull-request-comment) - (MIT), used by this Action to leave a sticky comment on pull requests +- [shlinkio/deploy-preview-action](https://github.com/shlinkio/deploy-preview-action) (MIT), prior art that informed the direction of this Action +- [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) (MIT), used by this Action to deploy previews +- [marocchino/sticky-pull-request-comment](https://github.com/marocchino/sticky-pull-request-comment) (MIT), used by this Action to leave a sticky comment on pull requests +- [Everyone who has contributed](https://github.com/rossjrw/pr-preview-action/graphs/contributors) to this Action diff --git a/action.yml b/action.yml index a48c0d4..0ef4eec 100644 --- a/action.yml +++ b/action.yml @@ -69,12 +69,31 @@ inputs: default: auto outputs: + deployment-action: + description: Resolved value of the 'action' input parameter (deploy, remove, none). + value: ${{ steps.setup.outputs.deployment_action }} + pages-base-url: + description: What this Action thinks the base URL of the GitHub Pages site is. + value: ${{ steps.setup.outputs.pages_base_url }} + preview-url-path: + description: Path to the preview from the Pages base URL. + value: ${{ steps.setup.outputs.preview_url_path }} + preview-url: + description: Full URL to the preview (https://[pages-base-url]/[preview-url-path]/). + value: ${{ steps.setup.outputs.preview_url }} + action-version: + description: The version of this Action when it was run. + value: ${{ steps.setup.outputs.action_version }} + action-start-timestamp: + description: The Unix timestamp that the action started. + value: ${{ steps.setup.outputs.action_start_timestamp }} + action-start-time: + description: The time that the action started in a readable format (UTC, depending on runner). + value: ${{ steps.setup.outputs.action_start_time }} + deployment-url: - description: Preview deployment URL + description: Deprecated, use `preview-url` instead. value: ${{ steps.setup.outputs.preview_url }} - url: - description: Preview deployment URL - value: https://${{ env.pages_base_url }}/${{ env.preview_url_path }}/ runs: using: composite diff --git a/lib/main.sh b/lib/main.sh index 26249af..3cc37d4 100755 --- a/lib/main.sh +++ b/lib/main.sh @@ -49,7 +49,6 @@ action_start_time=$(date '+%Y-%m-%d %H:%M %Z') { echo "deployment_action=$deployment_action" - echo "preview_file_path=$preview_file_path" echo "pages_base_url=$pages_base_url" echo "preview_url_path=$preview_url_path" echo "preview_url=https://$pages_base_url/$preview_url_path/" From d23d07a72a95e010d68ca605d7b9b8fc3c678b1d Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 17:17:21 +0000 Subject: [PATCH 06/15] Reorder readme --- README.md | 110 +++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 5ce09d3..4edfa64 100644 --- a/README.md +++ b/README.md @@ -89,22 +89,48 @@ jobs: source-dir: ./build/ ``` -## Important things to be aware of +# Inputs (configuration) -### Run only when files are changed +The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value. -Consider limiting this workflow to run [only when relevant files are -edited](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore) -to avoid deploying previews unnecessarily. +Input parameter | Description +--- | --- +`source-dir` | When creating or updating a preview, the path to the directory that contains the files to deploy. E.g. if your project builds to `./dist/` you would put `./dist/` (or `dist`, etc.).
Equivalent to [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) 'folder' setting.

Default: `.` (repository root) +`deploy-repository` | The repository to deploy the preview to.
__Note:__ The `token` parameter must also be set if changing this from the default.

Default: The pull request's target repository. +`preview-branch` | Branch to save previews to. This should be the same branch that your GitHub Pages site is deployed from.

Default: `gh-pages` +`umbrella-dir` | Path to the directory to place previews in.
The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

Default: `pr-preview` +`custom-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) +`pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL, this is removed from the beginning of the path.

Default: `.` (repository root) +`token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. +`action`
(Advanced) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` -### Run on all appropriate pull request events +# Outputs -Be sure to [pick the right event -types](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request) -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. +Several output values are provided to use after this Action in your workflow. To use them, give this Action's step an `id` and reference the value with `${{ steps..outputs. }}`, e.g.: + +```yml +# .github/workflows/preview.yml +jobs: + deploy-preview: + steps: + - uses: rossjrw/pr-preview-action@v1 + id: preview-step + - run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" +``` + +You could use these outputs to e.g. write your own sticky comment after the Action has run. + +Output | Description +--- | --- +`deployment-action` | Resolved value of the `action` input parameter (deploy, remove, none). +`pages-base-url` | What this Action thinks the base URL of the GitHub Pages site is. +`preview-url-path` | Path to the preview from the Pages base URL. +`preview-url` | Full URL to the preview (`https:////`). +`action-version` | The version of this Action when it was run. +`action-start-timestamp` | The time that the workflow step started as a Unix timestamp. +`action-start-time` | The time that the workflow step started in a readable format (UTC, depending on runner). + +# Common pitfalls ### Grant Actions permission to read and write to the repository @@ -113,13 +139,14 @@ 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 +### Run on all appropriate pull request events -I highly recommend [setting a concurrency -group](https://docs.github.com/en/actions/using-jobs/using-concurrency) -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. +Be sure to [pick the right event +types](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request) +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. ### Ensure your main deployment is compatible @@ -183,46 +210,21 @@ vice-versa. This feature was introduced in v4.3.0 of the above Action. -# Inputs (configuration) - -The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value. +# Best practices -Input parameter | Description ---- | --- -`source-dir` | When creating or updating a preview, the path to the directory that contains the files to deploy. E.g. if your project builds to `./dist/` you would put `./dist/` (or `dist`, etc.).
Equivalent to [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) 'folder' setting.

Default: `.` (repository root) -`deploy-repository` | The repository to deploy the preview to.
__Note:__ The `token` parameter must also be set if changing this from the default.

Default: The pull request's target repository. -`preview-branch` | Branch to save previews to. This should be the same branch that your GitHub Pages site is deployed from.

Default: `gh-pages` -`umbrella-dir` | Path to the directory to place previews in.
The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

Default: `pr-preview` -`custom-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) -`pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL, this is removed from the beginning of the path.

Default: `.` (repository root) -`token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. -`action`
(Advanced) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` - -# Outputs - -Several output values are provided to use after this Action in your workflow. To use them, give this Action's step an `id` and reference the value with `${{ steps..outputs. }}`, e.g.: +### Run only when files are changed -```yml -# .github/workflows/preview.yml -jobs: - deploy-preview: - steps: - - uses: rossjrw/pr-preview-action@v1 - id: preview-step - - run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" -``` +Consider limiting this workflow to run [only when relevant files are +edited](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore) +to avoid deploying previews unnecessarily. -You could use these outputs to e.g. write your own sticky comment after the Action has run. +### Set a concurrency group -Output | Description ---- | --- -`deployment-action` | Resolved value of the `action` input parameter (deploy, remove, none). -`pages-base-url` | What this Action thinks the base URL of the GitHub Pages site is. -`preview-url-path` | Path to the preview from the Pages base URL. -`preview-url` | Full URL to the preview (`https:////`). -`action-version` | The version of this Action when it was run. -`action-start-timestamp` | The time that the workflow step started as a Unix timestamp. -`action-start-time` | The time that the workflow step started in a readable format (UTC, depending on runner). +I highly recommend [setting a concurrency +group](https://docs.github.com/en/actions/using-jobs/using-concurrency) +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. # Examples From 3fc0990f8fa3530688e1b9add986c2b97735cc52 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 17:29:31 +0000 Subject: [PATCH 07/15] Deprecate custom-url in favour of pages-base-url --- README.md | 4 ++-- action.yml | 11 +++++++++-- lib/main.sh | 24 +++++++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4edfa64..266a2f1 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,8 @@ Input parameter | Description `deploy-repository` | The repository to deploy the preview to.
__Note:__ The `token` parameter must also be set if changing this from the default.

Default: The pull request's target repository. `preview-branch` | Branch to save previews to. This should be the same branch that your GitHub Pages site is deployed from.

Default: `gh-pages` `umbrella-dir` | Path to the directory to place previews in.
The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

Default: `pr-preview` -`custom-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) -`pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL, this is removed from the beginning of the path.

Default: `.` (repository root) +`pages-base-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) +`pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL path, this is removed from the beginning of the file path.

Default: `.` (repository root) `token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. `action`
(Advanced) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` diff --git a/action.yml b/action.yml index 0ef4eec..317d7c9 100644 --- a/action.yml +++ b/action.yml @@ -25,6 +25,10 @@ inputs: description: Path to the directory containing all previews. required: false default: pr-preview + pages-base-url: + description: URL of the repo's GitHub Pages site. + required: false + default: "" pages-base-path: description: Path that GitHub Pages is served from. required: false @@ -45,8 +49,10 @@ inputs: to another repository. required: false default: ${{ github.repository }} + custom-url: - description: Custom pages URL + description: Deprecated, use `pages-base-url` instead. + deprecationMessage: Use `pages-base-url` instead. required: false default: "" action: @@ -103,13 +109,14 @@ runs: env: deployment_action: ${{ inputs.action }} umbrella_path: ${{ inputs.umbrella-dir }} + pages_base_url: ${{ inputs.pages-base-url }} pages_base_path: ${{ inputs.pages-base-path }} pr_number: ${{ github.event.number }} github_action_ref: ${{ github.action_ref }} github_action_repository: ${{ github.action_repository }} - custom_pages_base_url: ${{ inputs.custom-url }} deployment_repository: ${{ inputs.deploy-repository }} token: ${{ inputs.token }} + deprecated_custom_url: ${{ inputs.custom-url }} run: $GITHUB_ACTION_PATH/lib/main.sh shell: bash diff --git a/lib/main.sh b/lib/main.sh index 3cc37d4..5f52064 100755 --- a/lib/main.sh +++ b/lib/main.sh @@ -1,16 +1,22 @@ #!/usr/bin/env bash -declare deployment_action pr_number deployment_repository custom_pages_base_url pages_base_path umbrella_path github_action_ref github_action_repository +declare deployment_action pr_number deployment_repository pages_base_url pages_base_path umbrella_path github_action_ref github_action_repository deprecated_custom_url -repo_org=$(echo "$deployment_repository" | cut -d "/" -f 1) -repo_name=$(echo "$deployment_repository" | cut -d "/" -f 2) +# Deprecation of custom-url in favour of pages-base-url +if [ -z "$pages_base_url" ] && [ -n "$deprecated_custom_url" ]; then + pages_base_url=$deprecated_custom_url +fi -if [ -n "$custom_pages_base_url" ]; then - pages_base_url="$custom_pages_base_url" -elif [ "$repo_name" = "${repo_org}.github.io" ]; then - pages_base_url="${repo_org}.github.io" -else - pages_base_url=$(echo "$deployment_repository" | sed 's/\//.github.io\//') +# If pages_base_url was not set by the user, try to guess +if [ -z "$pages_base_url" ]; then + # Either .github.io or .github.io/repo + repo_org=$(echo "$deployment_repository" | cut -d "/" -f 1) + repo_name=$(echo "$deployment_repository" | cut -d "/" -f 2) + if [ "$repo_name" = "${repo_org}.github.io" ]; then + pages_base_url="${repo_org}.github.io" + else + pages_base_url=$(echo "$deployment_repository" | sed -e 's/\//.github.io\//') + fi fi preview_file_path="$umbrella_path/pr-$pr_number" From 739333ba83a35ae16c831c17ffac85d81f5e7cbf Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 18:56:48 +0000 Subject: [PATCH 08/15] Add parameter to prevent comment --- README.md | 5 +++-- action.yml | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 266a2f1..cb6c840 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,9 @@ Input parameter | Description `umbrella-dir` | Path to the directory to place previews in.
The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages.

Default: `pr-preview` `pages-base-url` | Base URL to use when providing a link to the preview site.

Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) `pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL path, this is removed from the beginning of the file path.

Default: `.` (repository root) +`comment`
(boolean) | Whether to leave a [sticky comment](https://github.com/marocchino/sticky-pull-request-comment) on the PR after the preview is built.
The comment may be added before the preview finishes deploying.

Default: `true` `token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. -`action`
(Advanced) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` +`action`
(enum) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` # Outputs @@ -118,7 +119,7 @@ jobs: - run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" ``` -You could use these outputs to e.g. write your own sticky comment after the Action has run. +You could use these outputs and input parameter `comment: false` to write your own sticky comment after the Action has run. Output | Description --- | --- diff --git a/action.yml b/action.yml index 317d7c9..11f11f7 100644 --- a/action.yml +++ b/action.yml @@ -49,6 +49,10 @@ inputs: to another repository. required: false default: ${{ github.repository }} + comment: + description: Whether to leave a sticky comment on the calling PR. + required: false + default: "true" custom-url: description: Deprecated, use `pages-base-url` instead. @@ -133,7 +137,10 @@ runs: force: false - name: Leave a comment after deployment - if: env.deployment_action == 'deploy' && env.deployment_status == 'success' + if: | + env.deployment_action == 'deploy' && + env.deployment_status == 'success' && + (inputs.comment == 'true' || inputs.comment == true) uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview @@ -156,7 +163,10 @@ runs: force: false - name: Leave a comment after removal - if: env.deployment_action == 'remove' && env.deployment_status == 'success' + if: | + env.deployment_action == 'remove' && + env.deployment_status == 'success' && + (inputs.comment == 'true' || inputs.comment == true) uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0 with: header: pr-preview From 5bde42bc7de62bc98f10f321a71bc5322e109575 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 19:08:26 +0000 Subject: [PATCH 09/15] Adjust comment text to call out Pages build time --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 11f11f7..142639d 100644 --- a/action.yml +++ b/action.yml @@ -147,8 +147,8 @@ runs: message: | [PR Preview Action](https://github.com/${{ github.action_repository }}) ${{ env.action_version }} :---: - :rocket: Deployed preview to https://${{ env.pages_base_url }}/${{ env.preview_url_path }}/ - on branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} + :rocket: View preview at ${{ env.preview_url }}
(after the GitHub Pages build finishes) + built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} - name: Remove preview directory if: env.deployment_action == 'remove' From b4aa10f5073fdcf2082b52550e928968f36338f7 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 19:13:39 +0000 Subject: [PATCH 10/15] Put action repo back into env var Re-evaluating it each time it's used causes it to get the wrong repo sometimes (e.g. marocchino/sticky-pull-request-comment while generating the comment text) --- action.yml | 4 ++-- lib/main.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 142639d..e1fee27 100644 --- a/action.yml +++ b/action.yml @@ -145,7 +145,7 @@ runs: with: header: pr-preview message: | - [PR Preview Action](https://github.com/${{ github.action_repository }}) ${{ env.action_version }} + [PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }} :---: :rocket: View preview at ${{ env.preview_url }}
(after the GitHub Pages build finishes) built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} @@ -171,7 +171,7 @@ runs: with: header: pr-preview message: | - [PR Preview Action](https://github.com/${{ github.action_repository }}) ${{ env.action_version }} + [PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }} :---: Preview removed because the pull request was closed. ${{ env.action_start_time }} diff --git a/lib/main.sh b/lib/main.sh index 5f52064..c780050 100755 --- a/lib/main.sh +++ b/lib/main.sh @@ -47,6 +47,7 @@ action_start_time=$(date '+%Y-%m-%d %H:%M %Z') echo "preview_url_path=$preview_url_path" echo "preview_url=https://$pages_base_url/$preview_url_path/" + echo "action_repository=$github_action_repository" echo "action_version=$action_version" echo "action_start_time=$action_start_time" } >>"$GITHUB_ENV" From 27b614c5694e961ed8f45cfd746679468bac240e Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 19:23:05 +0000 Subject: [PATCH 11/15] Tweak wording and add deployments link --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e1fee27..5391391 100644 --- a/action.yml +++ b/action.yml @@ -147,7 +147,7 @@ runs: message: | [PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }} :---: - :rocket: View preview at ${{ env.preview_url }}
(after the GitHub Pages build finishes) + :rocket: View preview at ${{ env.preview_url }}
(after the [GitHub Pages deployment](${{ github.server_url }}/${{ inputs.deploy-repository }}/deployments) is complete) built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} - name: Remove preview directory From 00d424dac47994efe0637d7e07f35b1710295103 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 19:48:44 +0000 Subject: [PATCH 12/15] Leverage hacky html to rearrange comment --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 5391391..8637d86 100644 --- a/action.yml +++ b/action.yml @@ -147,8 +147,8 @@ runs: message: | [PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }} :---: - :rocket: View preview at ${{ env.preview_url }}
(after the [GitHub Pages deployment](${{ github.server_url }}/${{ inputs.deploy-repository }}/deployments) is complete) - built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }} + |

:rocket: View preview at
${{ env.preview_url }}

+ |
Built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }}.
Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ inputs.deploy-repository }}/deployments) is complete.

- name: Remove preview directory if: env.deployment_action == 'remove' From 48ba9bf1160c03cf297034d799e2c1cb8b7f2a82 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 20:11:40 +0000 Subject: [PATCH 13/15] Block line break in table heading --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb6c840..e6124ad 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ jobs: The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value. -Input parameter | Description +Input parameter | Description --- | --- `source-dir` | When creating or updating a preview, the path to the directory that contains the files to deploy. E.g. if your project builds to `./dist/` you would put `./dist/` (or `dist`, etc.).
Equivalent to [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) 'folder' setting.

Default: `.` (repository root) `deploy-repository` | The repository to deploy the preview to.
__Note:__ The `token` parameter must also be set if changing this from the default.

Default: The pull request's target repository. From 8c2bc036182ddb318e86b76b7504b6f5c540cc33 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 20:13:55 +0000 Subject: [PATCH 14/15] Add conditional example --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e6124ad..ca229a0 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,8 @@ jobs: steps: - uses: rossjrw/pr-preview-action@v1 id: preview-step - - run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" + - if: steps.preview-step.outputs.deployment-action == "deploy" + run: echo "Preview visible at ${{ steps.preview-step.outputs.preview-url }}" ``` You could use these outputs and input parameter `comment: false` to write your own sticky comment after the Action has run. From 9029298c744ebe13e455b360625b21dd738ee002 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Thu, 9 Jan 2025 20:16:15 +0000 Subject: [PATCH 15/15] Tweak heading levels --- README.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ca229a0..1ab865c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ This Action does not currently support deploying previews for PRs from forks, but will do so in [the upcoming v2](https://github.com/rossjrw/pr-preview-action/pull/6). -## Usage +# Usage A [GitHub Actions workflow](https://docs.github.com/en/actions/learn-github-actions) is @@ -89,7 +89,7 @@ jobs: source-dir: ./build/ ``` -# Inputs (configuration) +## Inputs (configuration) The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value. @@ -105,7 +105,7 @@ Input parameter | Description `token` | Authentication token for the preview deployment.
The default value works for non-fork pull requests to the same repository. For anything else, you will need a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to access it, and [store it as a secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) 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. `action`
(enum) | Determines what this action will do when it is executed. Supported values:

  • `deploy` - create and deploy the preview, overwriting any existing preview in that location.
  • `remove` - remove the preview.
  • `auto` - determine whether to deploy or remove the preview based on [the emitted event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request). If the event is `pull_request`, it will deploy the preview when the event type is `opened`, `reopened` and `synchronize`, and remove it on `closed` events. Does not do anything for other events or event types, even if you explicitly instruct the workflow to run on them.
  • `none` and all other values: does not do anything.
Default: `auto` -# Outputs +## Outputs Several output values are provided to use after this Action in your workflow. To use them, give this Action's step an `id` and reference the value with `${{ steps..outputs. }}`, e.g.: @@ -132,7 +132,9 @@ Output | Description `action-start-timestamp` | The time that the workflow step started as a Unix timestamp. `action-start-time` | The time that the workflow step started in a readable format (UTC, depending on runner). -# Common pitfalls +# Considerations + +## Common pitfalls ### Grant Actions permission to read and write to the repository @@ -212,7 +214,7 @@ vice-versa. This feature was introduced in v4.3.0 of the above Action. -# Best practices +## Best practices ### Run only when files are changed @@ -228,9 +230,9 @@ 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. -# Examples +## Examples -## Full example +### Full example Full example with all default values added: @@ -283,7 +285,7 @@ jobs: force: false ``` -## Deployment from `docs/` +### Deployment from `docs/` If your Pages site is built to `build/` and deployed from the `docs/` directory on the `main` branch: @@ -303,7 +305,7 @@ steps: 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 +### 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 @@ -325,7 +327,7 @@ steps: action: remove ``` -## Permanent previews +### 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` —