diff --git a/README.md b/README.md index 6efde70..c9ff6a9 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Features: 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 +- Can optionally include a QR code in the preview comment for easy mobile access - Cleans up after itself — removes deployed previews when the pull request is closed - Can be configured to override any of these behaviours @@ -102,6 +103,7 @@ Input parameter | Description `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` +`qr-code`
(boolean) | Whether to include a QR code in the preview comment for easy mobile access. The QR code links to the preview URL.

Default: `false` `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:

Default: `auto` @@ -260,6 +262,7 @@ jobs: preview-branch: gh-pages umbrella-dir: pr-preview action: auto + qr-code: false ``` ...and an accompanying main deployment workflow: diff --git a/action.yml b/action.yml index 8637d86..cd2f0df 100644 --- a/action.yml +++ b/action.yml @@ -77,6 +77,10 @@ inputs: all other events. `auto` is the default value. required: false default: auto + qr-code: + description: Whether to show a QR code in the preview comment for easy mobile access + required: false + default: false outputs: deployment-action: @@ -136,6 +140,25 @@ runs: commit-message: Deploy preview for PR ${{ github.event.number }} 🛫 force: false + - name: Generate QR Code + if: | + env.deployment_action == 'deploy' && + env.deployment_status == 'success' && + (inputs.qr-code == 'true' || inputs.qr-code == true) + id: qr + run: | + cd "$GITHUB_ACTION_PATH" + npm install + raw_qr=$(node lib/generate-qr.js "${{ env.preview_url }}") + { + echo 'qr_code<

📱 Scan QR code to open on mobile
" + echo "$raw_qr" + echo "

" + echo 'EOFQR' + } >> $GITHUB_OUTPUT + shell: bash + - name: Leave a comment after deployment if: | env.deployment_action == 'deploy' && @@ -149,6 +172,7 @@ runs: :---: |

: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.

+ ${{ inputs.qr-code == 'true' && steps.qr.outputs.qr_code || '' }} - name: Remove preview directory if: env.deployment_action == 'remove' diff --git a/lib/generate-qr.js b/lib/generate-qr.js new file mode 100755 index 0000000..a20a732 --- /dev/null +++ b/lib/generate-qr.js @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +import { renderUnicodeCompact } from 'uqr' + +const url = process.argv[2] +if (!url) { + console.error("Please provide a URL as an argument") + process.exit(1) +} + +try { + const qrCode = renderUnicodeCompact(url) + console.log('\n```\n' + qrCode + '\n```') +} catch (error) { + console.error("Error generating QR code:", error) + process.exit(1) +} diff --git a/package.json b/package.json index ab353d8..8d83ce9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,10 @@ { "name": "pr-preview", "version": "0.0.0", + "type": "module", + "dependencies": { + "uqr": "^0.1.2" + }, "devDependencies": { "prettier": "2.5.1", "prettier-plugin-sh": "^0.8.1"