|
| 1 | +name: pkg.pr.new Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + checks: write |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +concurrency: |
| 15 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 16 | + cancel-in-progress: true |
| 17 | + |
| 18 | +jobs: |
| 19 | + publish-preview: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + timeout-minutes: 30 |
| 22 | + steps: |
| 23 | + - name: Checkout Repository |
| 24 | + uses: actions/checkout@v5 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Setup pnpm |
| 29 | + uses: pnpm/action-setup@v4 |
| 30 | + |
| 31 | + - name: Setup Node.js 20 |
| 32 | + uses: actions/setup-node@v6 |
| 33 | + with: |
| 34 | + node-version: '20' |
| 35 | + cache: 'pnpm' |
| 36 | + cache-dependency-path: '**/pnpm-lock.yaml' |
| 37 | + |
| 38 | + - name: Cache Tool Downloads |
| 39 | + uses: actions/cache@v5 |
| 40 | + with: |
| 41 | + path: ~/.cache |
| 42 | + key: ${{ runner.os }}-toolcache-${{ hashFiles('pnpm-lock.yaml') }} |
| 43 | + restore-keys: | |
| 44 | + ${{ runner.os }}-toolcache- |
| 45 | +
|
| 46 | + - name: Remove cached node_modules |
| 47 | + run: rm -rf node_modules .nx |
| 48 | + |
| 49 | + - name: Set Nx SHA |
| 50 | + uses: nrwl/nx-set-shas@v4 |
| 51 | + |
| 52 | + - name: Install dependencies |
| 53 | + run: pnpm install --frozen-lockfile |
| 54 | + |
| 55 | + - name: Build package workspace targets |
| 56 | + run: | |
| 57 | + # Some package dts builds are intermittently flaky in CI; retry once. |
| 58 | + npx nx run-many --targets=build --projects=tag:type:pkg --skip-nx-cache || \ |
| 59 | + (sleep 5 && npx nx run-many --targets=build --projects=tag:type:pkg --skip-nx-cache) |
| 60 | +
|
| 61 | + - name: Resolve synced Changesets package paths |
| 62 | + id: package_paths |
| 63 | + run: | |
| 64 | + paths_json=$(node -e ' |
| 65 | + const fs = require("fs"); |
| 66 | + const path = require("path"); |
| 67 | +
|
| 68 | + const config = JSON.parse(fs.readFileSync(".changeset/config.json", "utf8")); |
| 69 | + const fixed = new Set((config.fixed || []).flat()); |
| 70 | + const roots = ["packages", "apps"]; |
| 71 | + const results = []; |
| 72 | +
|
| 73 | + function walk(dir) { |
| 74 | + if (!fs.existsSync(dir)) return; |
| 75 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 76 | + const fullPath = path.join(dir, entry.name); |
| 77 | + if (entry.isDirectory()) { |
| 78 | + walk(fullPath); |
| 79 | + continue; |
| 80 | + } |
| 81 | + if (entry.isFile() && entry.name === "package.json") { |
| 82 | + const pkgDir = path.dirname(fullPath); |
| 83 | + const pkg = JSON.parse(fs.readFileSync(fullPath, "utf8")); |
| 84 | + if (fixed.has(pkg.name) && pkg.private !== true) { |
| 85 | + results.push(pkgDir); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + for (const root of roots) { |
| 92 | + walk(root); |
| 93 | + } |
| 94 | +
|
| 95 | + // Deduplicate and sort for stable output. |
| 96 | + const sorted = Array.from(new Set(results)).sort(); |
| 97 | + process.stdout.write(JSON.stringify(sorted)); |
| 98 | + ') |
| 99 | +
|
| 100 | + echo "paths_json=$paths_json" >> "$GITHUB_OUTPUT" |
| 101 | +
|
| 102 | + - name: Publish pkg.pr.new previews |
| 103 | + env: |
| 104 | + PKG_PATHS_JSON: ${{ steps.package_paths.outputs.paths_json }} |
| 105 | + run: | |
| 106 | + # shellcheck disable=SC2016 |
| 107 | + node -e ' |
| 108 | + const { spawnSync } = require("child_process"); |
| 109 | + const paths = JSON.parse(process.env.PKG_PATHS_JSON || "[]"); |
| 110 | + if (!paths.length) { |
| 111 | + console.log("No synced Changesets packages found to publish."); |
| 112 | + process.exit(0); |
| 113 | + } |
| 114 | + const args = [ |
| 115 | + "dlx", |
| 116 | + "pkg-pr-new", |
| 117 | + "publish", |
| 118 | + "--pnpm", |
| 119 | + "--packageManager=pnpm", |
| 120 | + "--comment=update", |
| 121 | + ...paths, |
| 122 | + ]; |
| 123 | + const result = spawnSync("pnpm", args, { encoding: "utf8" }); |
| 124 | + if (result.stdout) process.stdout.write(result.stdout); |
| 125 | + if (result.stderr) process.stderr.write(result.stderr); |
| 126 | +
|
| 127 | + if (result.status !== 0) { |
| 128 | + const combinedOutput = `${result.stdout || ""}\n${result.stderr || ""}`; |
| 129 | + if (combinedOutput.includes("https://github.com/apps/pkg-pr-new is not installed")) { |
| 130 | + console.log("pkg.pr.new app is not installed on this repository; skipping preview publish."); |
| 131 | + process.exit(0); |
| 132 | + } |
| 133 | + process.exit(result.status || 1); |
| 134 | + } |
| 135 | + ' |
0 commit comments