Skip to content

Commit cac6fea

Browse files
committed
generate qr-code with uqr
1 parent 40a0fc5 commit cac6fea

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features:
1111
you and your team can collaborate on new features faster
1212
- Updates the deployment and the comment whenever new commits are pushed to
1313
the pull request
14+
- Can optionally include a QR code in the preview comment for easy mobile access
1415
- Cleans up after itself — removes deployed previews when the pull
1516
request is closed
1617
- Can be configured to override any of these behaviours
@@ -87,6 +88,7 @@ jobs:
8788
uses: rossjrw/pr-preview-action@v1
8889
with:
8990
source-dir: ./build/
91+
qr-code: true # Optional: Add QR code
9092
```
9193
9294
## Inputs (configuration)
@@ -102,6 +104,7 @@ Input parameter | Description
102104
`pages-base-url` | Base URL to use when providing a link to the preview site. <br><br> Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`)
103105
`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)
104106
`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. <br><br> Default: `true`
107+
`qr-code` <br> (boolean) | Whether to include a QR code in the preview comment for easy mobile access. The QR code links to the preview URL. <br><br> Default: `false`
105108
`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.
106109
`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`
107110

@@ -260,6 +263,7 @@ jobs:
260263
preview-branch: gh-pages
261264
umbrella-dir: pr-preview
262265
action: auto
266+
qr-code: true
263267
```
264268

265269
...and an accompanying main deployment workflow:

action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ runs:
140140
commit-message: Deploy preview for PR ${{ github.event.number }} 🛫
141141
force: false
142142

143+
- name: Generate QR Code
144+
if: |
145+
env.deployment_action == 'deploy' &&
146+
env.deployment_status == 'success' &&
147+
(inputs.qr-code == 'true' || inputs.qr-code == true)
148+
id: qr
149+
run: |
150+
cd "$GITHUB_ACTION_PATH"
151+
npm install
152+
qr_code=$(node lib/generate-qr.js "${{ env.preview_url }}")
153+
echo "qr_code=| <p></p> ![QR Code]($qr_code) <br><br>" >> $GITHUB_OUTPUT
154+
shell: bash
155+
143156
- name: Leave a comment after deployment
144157
if: |
145158
env.deployment_action == 'deploy' &&
@@ -152,6 +165,7 @@ runs:
152165
[PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }}
153166
:---:
154167
| <p></p> :rocket: View preview at <br> ${{ env.preview_url }} <br><br>
168+
${{ inputs.qr-code == 'true' && steps.qr.outputs.qr_code || '' }}
155169
| <h6>Built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ inputs.deploy-repository }}/deployments) is complete. <br><br> </h6>
156170
157171
- name: Remove preview directory

lib/generate-qr.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
import { renderSVG } from 'uqr'
4+
5+
const url = process.argv[2]
6+
if (!url) {
7+
console.error("Please provide a URL as an argument")
8+
process.exit(1)
9+
}
10+
11+
try {
12+
const svgString = renderSVG(url, {pixelSize: 2})
13+
const base64 = Buffer.from(svgString).toString('base64')
14+
console.log(`data:image/svg+xml;base64,${base64}`)
15+
} catch (error) {
16+
console.error("Error generating QR code:", error)
17+
process.exit(1)
18+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "pr-preview",
33
"version": "0.0.0",
4+
"type": "module",
5+
"dependencies": {
6+
"uqr": "^0.1.2"
7+
},
48
"devDependencies": {
59
"prettier": "2.5.1",
610
"prettier-plugin-sh": "^0.8.1"

0 commit comments

Comments
 (0)