name: Deploy website # Deploys docs/ (gitlocal.dev) to Cloudflare Pages with Vizoalica analytics # added as one static script tag, as a Vizoalica website without signed tokens # ("One script tag" in ehud-am/vizoalica docs/operations/pages.md). The # backend serves the SDK and accepts events only from the website's allowed # origins, so the site needs no Pages Functions, token endpoint or signing # secret. # # Every page in docs/ carries the marker comment where the # tag goes (tests/integration/website-structure.test.ts enforces it). This # workflow replaces the marker in the deployed copy only, so the committed # pages and the GitHub Pages mirror never load analytics. # # Each setting is read from a repository secret or, if no secret of that name # exists, a repository variable: # VIZOALICA_ENDPOINT Vizoalica backend address, e.g. https://prod-vizoalica..workers.dev # (any path is ignored; only the origin is used) # VIZOALICA_WEBSITE_KEY The website key from the console's Install page # CF_ACCOUNT_ID, CF_PAGES_PROJECT # CF_API_TOKEN (Cloudflare Pages: Edit scope only) must be a secret. on: push: branches: [main] paths: ["docs/**", ".github/workflows/deploy-website.yml"] workflow_dispatch: {} permissions: contents: read jobs: deploy: name: Deploy to Cloudflare Pages runs-on: ubuntu-latest timeout-minutes: 15 steps: - name: Check out website repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Use Node.js 22 uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 with: node-version: 22 - name: Add the Vizoalica script tag to every page env: # Values reach the shell as environment variables, never as text pasted into the script. VIZOALICA_ENDPOINT: ${{ secrets.VIZOALICA_ENDPOINT || vars.VIZOALICA_ENDPOINT }} VIZOALICA_WEBSITE_KEY: ${{ secrets.VIZOALICA_WEBSITE_KEY || vars.VIZOALICA_WEBSITE_KEY }} CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID || vars.CF_ACCOUNT_ID }} CF_PAGES_PROJECT: ${{ secrets.CF_PAGES_PROJECT || vars.CF_PAGES_PROJECT }} CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }} SITE_DIRECTORY: docs run: | set -euo pipefail missing=() for name in VIZOALICA_ENDPOINT VIZOALICA_WEBSITE_KEY CF_ACCOUNT_ID CF_PAGES_PROJECT CF_API_TOKEN; do if [ -z "${!name:-}" ]; then missing+=("$name") fi done if [ "${#missing[@]}" -gt 0 ]; then echo "::error::Missing required repository variables/secrets: ${missing[*]}" exit 1 fi # The backend serves the SDK at its root, so only the address's origin is used: a value # that names a path (for example the events endpoint, /v1/events:batch) works too. endpoint="$(node -e 'try { const u = new URL(process.env.VIZOALICA_ENDPOINT.trim()); if (u.protocol === "https:") console.log(u.origin) } catch {}')" # Both values are written into HTML, so each must match a strict pattern: no quote, # angle bracket, ampersand or space. if [[ ! "$endpoint" =~ ^https://[A-Za-z0-9.-]+(:[0-9]{1,5})?$ ]]; then echo "::error::VIZOALICA_ENDPOINT must be the backend's https address, e.g. https://prod-vizoalica..workers.dev" exit 1 fi if [[ ! "$VIZOALICA_WEBSITE_KEY" =~ ^[A-Za-z0-9_-]{1,128}$ ]]; then echo "::error::VIZOALICA_WEBSITE_KEY must be letters, digits, dashes and underscores (the console's website key)" exit 1 fi tag="" marker='' pages=0 while IFS= read -r -d '' page; do if [ "$(grep -cF "$marker" "$page")" -ne 1 ]; then echo "::error file=$page::must contain the marker $marker exactly once" exit 1 fi # The tag is literal text for awk (index/substr), so no character in it is special. awk -v marker="$marker" -v tag="$tag" '{ i = index($0, marker) if (i) $0 = substr($0, 1, i - 1) tag substr($0, i + length(marker)) print }' "$page" > "$page.tmp" mv "$page.tmp" "$page" pages=$((pages + 1)) done < <(find "$SITE_DIRECTORY" -name '*.html' -print0) echo "Added the Vizoalica script tag to $pages pages" - name: Deploy to Cloudflare Pages env: CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID || vars.CF_ACCOUNT_ID }} CF_PAGES_PROJECT: ${{ secrets.CF_PAGES_PROJECT || vars.CF_PAGES_PROJECT }} SITE_DIRECTORY: docs # A branch name is chosen by whoever pushes it, so it too goes through the environment. DEPLOY_BRANCH: ${{ github.ref_name }} run: | set -euo pipefail # An exact Wrangler version: this step runs with the Cloudflare token, so it must not # fetch whatever "latest 4.x" is on the day. Update it deliberately. npx --yes wrangler@4.127.1 pages deploy "$SITE_DIRECTORY" \ --project-name "$CF_PAGES_PROJECT" \ --branch "$DEPLOY_BRANCH"