Skip to content

Commit 8d0e59d

Browse files
authored
Merge pull request #98 from lredoban/qr-code
Add QR code to comment
2 parents c739ba4 + 66ce60d commit 8d0e59d

8 files changed

Lines changed: 159 additions & 138 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,52 +33,10 @@ jobs:
3333
- name: Run tests
3434
run: bash test/run-unit-tests.sh
3535

36-
test-comment-content:
37-
name: Comment content
38-
# Skip on pull_request_target if already ran via pull_request (same-repo PRs)
39-
if: |
40-
github.event_name != 'pull_request_target' ||
41-
github.event.pull_request.head.repo.full_name != github.repository
42-
runs-on: ubuntu-latest
43-
steps:
44-
- name: Checkout
45-
uses: actions/checkout@v4
46-
with:
47-
# For pull_request_target, checkout the PR head safely
48-
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.ref }}
49-
50-
- name: Test deploy comment
51-
env:
52-
GITHUB_ACTION_PATH: ${{ github.workspace }}
53-
umbrella_path: pr-preview
54-
pages_base_url: ""
55-
pages_base_path: ""
56-
pr_number: "12345"
57-
github_action_ref: ${{ github.ref }}
58-
github_action_repository: ${{ github.repository }}
59-
deployment_repository: test-owner/test-repo
60-
token: fake-token
61-
deprecated_custom_url: ""
62-
run: bash test/integration/test-comment-content.sh deploy
63-
64-
- name: Test remove comment
65-
env:
66-
GITHUB_ACTION_PATH: ${{ github.workspace }}
67-
umbrella_path: pr-preview
68-
pages_base_url: ""
69-
pages_base_path: ""
70-
pr_number: "12345"
71-
github_action_ref: ${{ github.ref }}
72-
github_action_repository: ${{ github.repository }}
73-
deployment_repository: test-owner/test-repo
74-
token: fake-token
75-
deprecated_custom_url: ""
76-
run: bash test/integration/test-comment-content.sh remove
77-
7836
# Integration tests require secrets, so only run when secrets are available
7937
integration-tests:
8038
name: Integration tests
81-
needs: [test-unit, test-comment-content]
39+
needs: [test-unit]
8240
# Run integration tests when:
8341
# 1. Push to main
8442
# 2. Same-repo PR (secrets available via pull_request)

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Features:
77
- Creates and deploys previews of pull requests to your GitHub Pages site
88
- 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
99
- Updates the deployment and the comment whenever new commits are pushed to the pull request
10+
- Can optionally include a QR code in the preview comment for easy mobile access
1011
- Cleans up after itself — removes deployed previews when the pull request is closed
1112
- Can be configured to override any of these behaviours
1213

@@ -21,13 +22,15 @@ Preview URLs look like this: `https://[owner].github.io/[repo]/pr-preview/pr-[nu
2122

2223
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).
2324

24-
# Usage
25+
# Setup
2526

2627
A [GitHub Actions workflow](https://docs.github.com/en/actions/learn-github-actions) is required to use this Action.
2728

28-
All the workflow needs to do first is checkout the repository and build the Pages site.
29+
You just need to do two things to set up your repository to support previews, both in the repository settings:
30+
31+
### 1. Deploy Pages from branch
2932

30-
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**:
33+
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**:
3134

3235
<p align="center">
3336
<img src="https://github.com/rossjrw/pr-preview-action/blob/main/.github/deployment-settings.png" alt="GitHub Pages settings">
@@ -36,7 +39,16 @@ First, ensure that your repository is configured to have its GitHub Pages site d
3639
Pictured: Repository Pages settings at /settings/page
3740
</p>
3841

39-
The `gh-pages` branch is used for GitHub Pages deployments by convention, and will be used in examples here as well.
42+
> [!IMPORTANT]
43+
> The other option (called "GitHub Actions") has a [misleading name](https://github.com/orgs/community/discussions/30113#discussioncomment-7650234) and does not work with this action.
44+
45+
### 2. Let your workflow write to the repo
46+
47+
In **Settings** > **Actions** > **General** > **Workflow permissions**, select "Read and write permissions" to allow action runs to make changes to your deployment branch (in this case, to add and remove previews).
48+
49+
# Usage
50+
51+
All the workflow needs to do before running the preview action is checkout the repository and build the Pages site.
4052

4153
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:
4254

@@ -71,11 +83,16 @@ jobs:
7183
uses: rossjrw/pr-preview-action@v1
7284
with:
7385
source-dir: ./build/
86+
preview-branch: gh-pages
87+
qr-code: true
7488
```
7589
90+
> [!TIP]
91+
> The `gh-pages` branch is used for GitHub Pages deployments by convention, and will be used in examples here as well, but you can use whatever branch you like (just make sure to change the `preview-branch` input).
92+
7693
## Inputs (configuration)
7794

78-
The following input parameters are provided, which can be passed to the `with` parameter. All parameters are optional and have a default value.
95+
The following input parameters are provided, which can be passed to the `with` parameter. ALL parameters are optional and have a default value.
7996

8097
| Input&nbsp;parameter | Description |
8198
| --- | --- |
@@ -88,6 +105,7 @@ The following input parameters are provided, which can be passed to the `with` p
88105
| `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. <br><br> Default: `.` (repository root) |
89106
| `wait-for-pages-deployment` <br> (boolean) | Whether to wait for the GitHub Pages deployment to complete. When enabled, the action will poll the GitHub Deployments API and delay workflow completion until the Pages deployment finishes, e.g. to ensure the preview URL is accessible when the comment is posted. <br><br> Default: `false` (this will be `true` in a future version of this Action) |
90107
| `comment` <br> (boolean) | Whether to leave a [sticky comment](https://github.com/marocchino/sticky-pull-request-comment) on the PR after the preview is built.<br> The comment may be added before the preview finishes deploying unless `wait-for-pages-deployment` is enabled. <br><br> Default: `true` |
108+
| `qr-code` <br> (booleanish) | Whether to include a QR code in the sticky comment for easy mobile access, which links to the preview URL. Does nothing if `comment` is `false`. <br> Set to `"true"`/`"false"` to enable/disable, or to a string to be used as a custom QR code provider ([see below](#use-a-different-qr-code-provider)). <br><br> Default: `false` |
91109
| `token` | Authentication token for the preview deployment. <br> 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 }}`. <br><br> Default: `${{ github.token }}`, which gives the action permission to deploy to the current repository. |
92110
| `action` <br> (enum) | Determines what this action will do when it is executed. Supported values: <br><br> <ul><li>`deploy` - create and deploy the preview, overwriting any existing preview in that location.</li><li>`remove` - remove the preview.</li><li>`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.</li><li>`none` and all other values: does not do anything.</li></ul> Default: `auto` |
93111

@@ -227,6 +245,7 @@ jobs:
227245
action: auto
228246
wait-for-pages-deployment: false
229247
comment: true
248+
qr-code: false
230249
```
231250

232251
...and an accompanying main deployment workflow:
@@ -327,6 +346,18 @@ Set `wait-for-deployment: true` to make the action automatically wait for Pages
327346
wait-for-pages-deployment: true
328347
```
329348

349+
### Use a different QR code provider
350+
351+
If you have this action include a QR code in the sticky comment with `qr-code: true`, the default QR code provider is [qr.rossjrw.com](https://qr.rossjrw.com/), a provider that I built for this project because I don't trust any pre-existing ones. Likewise, you probably shouldn't trust mine - what if I go rogue and change all your QR codes to point to something else? You never know.
352+
353+
To use a different QR code provider (I encourage you to make your own - consider forking https://github.com/rossjrw/qrcode-worker), set `qr-code` to its URL. The URI-encoded preview link will be appended to it. E.g.:
354+
355+
```yml
356+
- uses: rossjrw/pr-preview-action@v1
357+
with:
358+
qr-code: https://my-qrcode-provider.example.com/generate?url=
359+
```
360+
330361
### Customise the sticky comment
331362

332363
You can use `id`, `with: comment: false`, the output values and [context variables](https://docs.github.com/en/actions/reference/workflows-and-actions/contexts) to construct your own comment to be left on the PR. This example recreates this Action's default comment (complete with HTML spacing jank), but you could change it however you like, use a different commenting Action from the marketplace, etc.
@@ -365,7 +396,7 @@ jobs:
365396
message: |
366397
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
367398
:---:
368-
| <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br>
399+
| <p><img src="https://qr.rossjrw.com/?url=${preview_url}" height="100" align="right" alt="QR code for preview link"></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br>
369400
| <h6>Built to branch [`${{ env.PREVIEW_BRANCH }}`](${{ github.server_url }}/${{ github.repository }}/tree/${{ env.PREVIEW_BRANCH }}) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6>
370401

371402
- uses: marocchino/sticky-pull-request-comment@v2

action.yml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ inputs:
1313
description: >
1414
The token to use for the deployment. Default is GITHUB_TOKEN in the current repository. If you need more permissions for things such as deploying to another repository, you can add a Personal Access Token (PAT).
1515
16+
1617
required: false
1718
default: ${{ github.token }}
1819
preview-branch:
@@ -39,6 +40,7 @@ inputs:
3940
description: >
4041
The PR number to use for the preview path. Defaults to the PR number from the GitHub event context. Override this for testing or when not triggered by a pull_request event.
4142
43+
4244
required: false
4345
default: ""
4446
deploy-repository:
@@ -47,18 +49,24 @@ inputs:
4749
4850
You will need to add a Personal Access Token (PAT) in the `token` input in order to allow the action running in one repository to make changes to another repository.
4951
52+
5053
required: false
5154
default: ${{ github.repository }}
5255
wait-for-pages-deployment:
5356
description: >
5457
Whether to wait for GitHub Pages deployment before continuing, e.g. to ensure the preview URL is actually accessible when the sticky comment is posted.
5558
59+
5660
required: false
5761
default: "false"
5862
comment:
5963
description: Whether to leave a sticky comment on the calling PR at the end of the workflow.
6064
required: false
6165
default: "true"
66+
qr-code:
67+
description: Whether to display a QR code in the sticky comment.
68+
required: false
69+
default: "false"
6270
deploy-commit-message:
6371
description: The commit message to use when adding/updating a preview.
6472
required: false
@@ -89,6 +97,7 @@ inputs:
8997
9098
If set to `auto`, the action will try to determine whether to deploy or remove the preview. It will deploy the preview on `pull_request.types.synchronize` and `.opened` events, and remove it on `pull_request.types.closed` events. It will not do anything for all other events. `auto` is the default value.
9199
100+
92101
required: false
93102
default: auto
94103

@@ -133,11 +142,12 @@ runs:
133142
pages_base_url: ${{ inputs.pages-base-url }}
134143
pages_base_path: ${{ inputs.pages-base-path }}
135144
pr_number: ${{ inputs.pr-number || github.event.number }}
136-
github_action_ref: ${{ github.action_ref || github.ref }}
137-
github_action_repository: ${{ github.action_repository || github.repository }}
145+
action_repository: ${{ github.action_repository || github.repository }}
146+
action_ref: ${{ github.action_ref || github.ref }}
138147
deployment_repository: ${{ inputs.deploy-repository }}
139148
token: ${{ inputs.token }}
140149
deprecated_custom_url: ${{ inputs.custom-url }}
150+
qr_code_wanted: ${{ inputs.qr-code }}
141151
run: $GITHUB_ACTION_PATH/lib/main.sh
142152
shell: bash
143153

@@ -192,7 +202,8 @@ runs:
192202
"${{ github.server_url }}" \
193203
"${{ inputs.deploy-repository }}" \
194204
"${{ env.action_start_time }}" \
195-
"deploy")
205+
"deploy" \
206+
"${{ env.qr_code_provider }}")
196207
{
197208
echo "content<<EOF"
198209
echo "$CONTENT"
@@ -262,7 +273,8 @@ runs:
262273
"${{ github.server_url }}" \
263274
"${{ inputs.deploy-repository }}" \
264275
"${{ env.action_start_time }}" \
265-
"remove")
276+
"remove" \
277+
"${{ env.qr_code_provider }}")
266278
{
267279
echo "content<<EOF"
268280
echo "$CONTENT"

lib/generate-comment.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
set -euo pipefail
33

44
# Generate comment content for PR preview deployment
5-
# Usage: generate-comment.sh <action_repository> <action_version> <preview_url> <preview_branch> <server_url> <deploy_repository> <action_start_time> <deployment_action>
65

76
action_repository=${1:?missing action_repository}
87
action_version=${2:?missing action_version}
@@ -12,12 +11,18 @@ server_url=${5:?missing server_url}
1211
deploy_repository=${6:?missing deploy_repository}
1312
action_start_time=${7:?missing action_start_time}
1413
deployment_action=${8:?missing deployment_action}
14+
qr_code_provider=${9:-} # falsy qr code provider means no QR code
1515

1616
if [ "$deployment_action" = "deploy" ]; then
17+
qr_code=""
18+
if [ -n "$qr_code_provider" ]; then
19+
qr_code="<img src=\"${qr_code_provider}${preview_url}\" height=\"100\" align=\"right\" alt=\"QR code for preview link\">"
20+
fi
21+
1722
cat << EOF
1823
[PR Preview Action](https://github.com/${action_repository}) ${action_version}
1924
:---:
20-
| <p></p> :rocket: View preview at <br> ${preview_url} <br><br>
25+
| <p>$qr_code</p> :rocket: View preview at <br> ${preview_url} <br><br>
2126
| <h6>Built to branch [\`${preview_branch}\`](${server_url}/${deploy_repository}/tree/${preview_branch}) at ${action_start_time}. <br> Preview will be ready when the [GitHub Pages deployment](${server_url}/${deploy_repository}/deployments) is complete. <br><br> </h6>
2227
EOF
2328

lib/main.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source "$GITHUB_ACTION_PATH/lib/calculate-pages-base-url.sh"
55
source "$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh"
66
source "$GITHUB_ACTION_PATH/lib/determine-auto-action.sh"
77

8-
declare deployment_action pr_number deployment_repository pages_base_url pages_base_path umbrella_path github_action_ref github_action_repository deprecated_custom_url
8+
declare deployment_action pr_number deployment_repository pages_base_url pages_base_path umbrella_path action_repository action_ref deprecated_custom_url qr_code_wanted
99

1010
# Deprecation of custom-url in favour of pages-base-url
1111
if [ -z "$pages_base_url" ] && [ -n "$deprecated_custom_url" ]; then
@@ -31,10 +31,21 @@ if [ "$deployment_action" = "auto" ]; then
3131
echo >&2 "Auto action is $deployment_action"
3232
fi
3333

34-
action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p "$github_action_repository" -f "$github_action_ref")
34+
action_version=$("$GITHUB_ACTION_PATH/lib/find-current-git-tag.sh" -p "$action_repository" -f "$action_ref")
3535
action_start_timestamp=$(date '+%s')
3636
action_start_time=$(date '+%Y-%m-%d %H:%M %Z')
3737

38+
if [ "$qr_code_wanted" != "false" ]; then
39+
if [ "$qr_code_wanted" = "true" ]; then
40+
qr_code_provider="https://qr.rossjrw.com/?color.dark=0d1117&url="
41+
else
42+
qr_code_provider="$qr_code_wanted"
43+
fi
44+
echo >&2 "Using QR code provider: $qr_code_provider<URL>"
45+
else
46+
qr_code_provider=""
47+
fi
48+
3849
# Export variables for later use by this action
3950
{
4051
echo "empty_dir_path=$(mktemp -d)"
@@ -45,9 +56,11 @@ action_start_time=$(date '+%Y-%m-%d %H:%M %Z')
4556
echo "preview_url_path=$preview_url_path"
4657
echo "preview_url=https://$pages_base_url/$preview_url_path/"
4758

48-
echo "action_repository=$github_action_repository"
59+
echo "action_repository=$action_repository"
4960
echo "action_version=$action_version"
5061
echo "action_start_time=$action_start_time"
62+
63+
echo "qr_code_provider=$qr_code_provider"
5164
} >> "$GITHUB_ENV"
5265

5366
# Export variables for use by later actions in user workflow

0 commit comments

Comments
 (0)