Skip to content

Your favorite shift management application!

chore(ci): allow disabling basic auth on PR previews (#231)

panteL1d279bc0b968
diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml
index cd23a88..3578e06 100644
--- a/.github/workflows/pr-preview.yml
+++ b/.github/workflows/pr-preview.yml
@@ -18,6 +18,7 @@ permissions:
env:
PREVIEW_LABEL: preview
+ PUBLIC_LABEL: preview-public
STACK_COMMENT_MARKER: "<!-- pr-preview -->"
# Hostname prefix, not the image tag: previews live at
# <prefix>-<n>.<PREVIEW_DOMAIN>, while ghcr.io keeps its pr-<n> tags.
@@ -45,7 +46,9 @@ jobs:
contains(github.event.pull_request.labels.*.name, 'preview') &&
(github.event.action == 'synchronize' ||
github.event.action == 'reopened' ||
- (github.event.action == 'labeled' && github.event.label.name == 'preview'))
+ (github.event.action == 'labeled' && github.event.label.name == 'preview') ||
+ ((github.event.action == 'labeled' || github.event.action == 'unlabeled') &&
+ github.event.label.name == 'preview-public'))
runs-on: ubuntu-latest
timeout-minutes: 40
# Job level, not workflow level: a skipped job must not cancel a running
@@ -84,6 +87,37 @@ jobs:
exit 1
fi
+ # Anyone with triage access can set a label, so the label alone is not
+ # enough: whoever applied it last must hold admin or maintain.
+ - name: Decide on basic auth
+ id: access
+ env:
+ GH_TOKEN: ${{ github.token }}
+ PR: ${{ github.event.number }}
+ HAS_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'preview-public') }}
+ run: |
+ set -o pipefail
+ if [ "$HAS_LABEL" != "true" ]; then
+ echo "public=false" >> "$GITHUB_OUTPUT"
+ exit 0
+ fi
+ actor=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/events" \
+ --jq ".[] | select(.event == \"labeled\" and .label.name == \"$PUBLIC_LABEL\") | .actor.login" | tail -n 1)
+ role=""
+ if [ -n "$actor" ]; then
+ # Fails closed: an API error leaves basic auth on.
+ role=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$actor/permission" --jq '.role_name') || role=""
+ fi
+ case "$role" in
+ admin|maintain)
+ echo "::notice::Basic auth disabled: $PUBLIC_LABEL was set by $actor ($role)."
+ echo "public=true" >> "$GITHUB_OUTPUT" ;;
+ *)
+ echo "::warning::Ignoring $PUBLIC_LABEL: set by ${actor:-unknown} (role: ${role:-none}), which needs admin or maintain. Basic auth stays on."
+ echo "public=false" >> "$GITHUB_OUTPUT"
+ echo "ignored_by=${actor:-unknown}" >> "$GITHUB_OUTPUT" ;;
+ esac
+
# Only needed for the imagetools fallback in the next step.
- name: Log in to ghcr.io
uses: docker/login-action@v4
@@ -146,6 +180,7 @@ jobs:
PREVIEW_BASIC_AUTH_HASH: ${{ secrets.PREVIEW_BASIC_AUTH_HASH }}
PREVIEW_BETTER_AUTH_SECRET: ${{ secrets.PREVIEW_BETTER_AUTH_SECRET }}
PREVIEW_CF_BYPASS_TOKEN: ${{ secrets.PREVIEW_CF_BYPASS_TOKEN }}
+ PREVIEW_PUBLIC: ${{ steps.access.outputs.public }}
run: node scripts/preview-stack.mjs deploy
- name: Wait for the instance
@@ -202,6 +237,8 @@ jobs:
ADMIN_PASSWORD: ${{ secrets.PREVIEW_ADMIN_PASSWORD }}
SHA: ${{ github.event.pull_request.head.sha }}
STALE_IMAGE: ${{ steps.image.outputs.stale }}
+ PUBLIC: ${{ steps.access.outputs.public }}
+ IGNORED_BY: ${{ steps.access.outputs.ignored_by }}
run: |
set -o pipefail
if [ "$STALE_IMAGE" = "true" ]; then
@@ -209,6 +246,14 @@ jobs:
else
commit_line="Commit: \`$SHA\`"
fi
+ if [ "$PUBLIC" = "true" ]; then
+ access_line="The instance is **public** (\`$PUBLIC_LABEL\`) — no basic auth in front of it."
+ else
+ access_line="The instance sits behind basic auth (user \`preview\`)."
+ if [ -n "$IGNORED_BY" ]; then
+ access_line="$access_line \`$PUBLIC_LABEL\` was ignored: $IGNORED_BY lacks admin or maintain access."
+ fi
+ fi
body=$(cat <<EOF
$STACK_COMMENT_MARKER
### Preview ready
@@ -220,7 +265,7 @@ jobs:
| Admin (superadmin, owns both calendars) | \`$ADMIN_EMAIL\` | \`$ADMIN_PASSWORD\` |
| Member (Contribute bundle on "Dienstplan Team A") | \`$MEMBER_EMAIL\` | \`$ADMIN_PASSWORD\` |
- The instance sits behind basic auth (user \`preview\`). Every further push resets
+ $access_line Every further push resets
it and seeds the demo data again.
$commit_line
diff --git a/docs/PR_PREVIEWS.md b/docs/PR_PREVIEWS.md
index 320af1c..367dcc1 100644
--- a/docs/PR_PREVIEWS.md
+++ b/docs/PR_PREVIEWS.md
@@ -5,6 +5,7 @@ This guide explains the automated preview environments BetterShift spins up for
## Table of Contents
1. [What It Does](#what-it-does)
+ - [Public Previews Without Basic Auth](#public-previews-without-basic-auth)
2. [Prerequisites on the Server](#prerequisites-on-the-server)
3. [Secrets](#secrets)
4. [Variables](#variables)
@@ -24,6 +25,16 @@ Closing a PR that carries the `preview` label, or removing the label again, tear
A pull request from a fork never gets a preview: `.github/workflows/pr-preview.yml` checks `head.repo.full_name` against the target repository and, for a fork PR that gets labeled `preview`, only writes an explanatory `::notice::` into the job log instead of deploying anything. It is a log line rather than a PR comment on purpose: a `pull_request` event from a fork gets a read-only `GITHUB_TOKEN` no matter what the workflow's `permissions:` block says, so posting a comment would simply fail with a 403. Whoever added the label has write access and can read the job. Fork PRs don't get repository secrets or a pushed image, so there would be nothing to deploy against anyway.
+### Public Previews Without Basic Auth
+
+Adding the `preview-public` label as well deploys the preview without the Basic Auth prompt, for example to show it to someone who shouldn't get the shared password. Adding or removing that label on a PR that has `preview` redeploys the instance right away, with the same reset and reseed as a push. Removing it puts Basic Auth back.
+
+Not everyone who can set labels gets this: anyone with triage access can apply a label. On every deploy the `Decide on basic auth` step looks up who applied `preview-public` most recently (in the PR's issue events) and checks that account's repository role. Only `admin` or `maintain` switch Basic Auth off. Otherwise the job writes a warning, keeps Basic Auth on and says so in the sticky comment. If a lookup fails, Basic Auth also stays on.
+
+The workflow runs from the PR's own branch, so anyone who can push to this repository could edit the check out of it. The role check stops triagers. It does not stop people with write access, but they can read the repository secrets anyway.
+
+A public preview is reachable by anyone who has the URL, and the sticky comment names the seeded superadmin login along with its password. Only use it for PRs whose preview you are happy for anyone to click through.
+
## Prerequisites on the Server
Before the first preview can go live, the server that Komodo deploys to needs:
@@ -160,6 +171,7 @@ PR gets the label "preview"
│ ├─ tag pr-<n> exists from an earlier push → carry on,
│ │ and mark the deploy as running an older image
│ └─ no tag at all → job fails
+ ├─ check who set "preview-public" (admin/maintain → no Basic Auth)
├─ node scripts/preview-stack.mjs deploy
│ ├─ Komodo /read/GetStack, then /write/CreateStack or /write/UpdateStack
│ └─ Komodo /execute/DeployStack (destroy_before_deploy: true)
@@ -190,6 +202,7 @@ PR carrying the label is closed / the "preview" label is removed
A few things distinguish a preview container from a normal deployment:
+- **Basic Auth** in front of the instance, unless `preview-public` is set by an admin or maintainer (see [Public Previews Without Basic Auth](#public-previews-without-basic-auth)). `scripts/preview-stack.mjs` then leaves the `caddy.basic_auth.preview` label out of the compose file when `PREVIEW_PUBLIC=true`.
- **No volumes.** The compose file `preview-stack.mjs` generates has no `volumes:` section at all — SQLite and any uploads live in the container's own writable layer. Combined with `destroy_before_deploy: true`, that means every push resets the instance completely and the seed script runs again from an empty database. This is deliberate, not a gap: state is meant to be disposable here.
- **`ALLOW_USER_REGISTRATION=true`** is set on the container so that `scripts/seed-preview.mjs` can register its accounts through the regular `/api/auth/sign-up/email` route, the same way a real user would.
- **The first account the seed script registers becomes superadmin automatically** — that's standard BetterShift behaviour for the first user on any fresh instance (`lib/auth/first-user.ts`), not something specific to previews.
diff --git a/scripts/preview-stack.mjs b/scripts/preview-stack.mjs
index e690595..542601f 100644
--- a/scripts/preview-stack.mjs
+++ b/scripts/preview-stack.mjs
@@ -127,7 +127,7 @@ function assertUpdateOk(label, result) {
* writes: a bcrypt hash contains `$`, which compose would read as a variable
* if it sat in the compose file itself.
*/
-function composeFile({ image, host, network, tz, locale }) {
+function composeFile({ image, host, network, tz, locale, basicAuth }) {
return [
"services:",
" app:",
@@ -153,7 +153,7 @@ function composeFile({ image, host, network, tz, locale }) {
// get its own certificate for a name that never resolves to it directly.
` caddy: http://${host}`,
' caddy.reverse_proxy: "{{upstreams 3000}}"',
- ' caddy.basic_auth.preview: "${BASIC_AUTH_HASH}"',
+ ...(basicAuth ? [' caddy.basic_auth.preview: "${BASIC_AUTH_HASH}"'] : []),
"",
"networks:",
` ${network}:`,
@@ -179,6 +179,7 @@ function stackConfig() {
network: process.env.PREVIEW_CADDY_NETWORK,
tz: process.env.PREVIEW_TZ || "Europe/Berlin",
locale: process.env.PREVIEW_LOCALE || "de",
+ basicAuth: process.env.PREVIEW_PUBLIC !== "true",
}),
environment: [
`BETTER_AUTH_SECRET=${escapeEnvValue(process.env.PREVIEW_BETTER_AUTH_SECRET)}`,