|
| 1 | +name: pr-package |
| 2 | + |
| 3 | +# Publishes alchemy + better-auth + pr-package tarballs to the pr-package |
| 4 | +# service at pkg.ing on every push to main and every PR sync. On PR close, |
| 5 | +# deletes every tag we ever assigned for that PR so the underlying tarballs |
| 6 | +# become orphaned and the bucket cleans them up. |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: [main] |
| 11 | + pull_request: |
| 12 | + types: [opened, synchronize, reopened, closed] |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +env: |
| 19 | + PR_PACKAGE_HOST: pkg.ing |
| 20 | + NODE_VERSION: "24" |
| 21 | + |
| 22 | +jobs: |
| 23 | + # ── Publish on push-to-main and PR sync. ────────────────────────────────── |
| 24 | + publish: |
| 25 | + if: github.event_name == 'push' || (github.event_name == 'pull_request' && |
| 26 | + github.event.action != 'closed') |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 30 | + |
| 31 | + - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 |
| 32 | + with: |
| 33 | + node-version: ${{ env.NODE_VERSION }} |
| 34 | + |
| 35 | + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 |
| 36 | + with: |
| 37 | + bun-version: latest |
| 38 | + |
| 39 | + - uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 40 | + with: |
| 41 | + path: ~/.bun/install/cache |
| 42 | + key: bun-${{ runner.os }}-${{ hashFiles('bun.lock') }} |
| 43 | + restore-keys: | |
| 44 | + bun-${{ runner.os }}- |
| 45 | +
|
| 46 | + - run: bun install |
| 47 | + |
| 48 | + # Build everything: tsdown bundles alchemy (bin/ + cli lib) and tsc -b |
| 49 | + # populates lib/ for the layer packages (better-auth, pr-package). |
| 50 | + # Their tarballs ship both src/ and lib/, and consumers in non-bun |
| 51 | + # runtimes resolve via the `import` condition → ./lib/index.js. |
| 52 | + - run: bun run build:packages |
| 53 | + |
| 54 | + - name: Compute tags |
| 55 | + id: tags |
| 56 | + env: |
| 57 | + EVENT: ${{ github.event_name }} |
| 58 | + # `head_ref` for PRs (source branch), `ref_name` for push (e.g. "main"). |
| 59 | + BRANCH: ${{ github.event_name == 'pull_request' && github.head_ref || |
| 60 | + github.ref_name }} |
| 61 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 62 | + # PR builds run on the merge commit by default; tag the actual head sha |
| 63 | + # so consumers can pin to a specific PR commit. |
| 64 | + SHA: ${{ github.event_name == 'pull_request' && |
| 65 | + github.event.pull_request.head.sha || github.sha }} |
| 66 | + run: | |
| 67 | + set -euo pipefail |
| 68 | + short="${SHA:0:7}" |
| 69 | + long="$SHA" |
| 70 | + tags=("$short" "$long" "$BRANCH") |
| 71 | + if [ "$EVENT" = "pull_request" ]; then |
| 72 | + tags+=("pr-${PR_NUMBER}") |
| 73 | + fi |
| 74 | + json=$(printf '%s\n' "${tags[@]}" | jq -R . | jq -s -c .) |
| 75 | + echo "tags=$json" >> "$GITHUB_OUTPUT" |
| 76 | + echo "short=$short" >> "$GITHUB_OUTPUT" |
| 77 | + echo "Tags: $json" |
| 78 | +
|
| 79 | + # PR builds expire after 1 week; pushes to main omit the header and |
| 80 | + # fall back to the worker's defaultTtl (3 weeks). |
| 81 | + - name: Publish alchemy |
| 82 | + env: |
| 83 | + TOKEN: ${{ secrets.PR_PACKAGE_TOKEN }} |
| 84 | + TAGS: ${{ steps.tags.outputs.tags }} |
| 85 | + TTL: ${{ github.event_name == 'pull_request' && '1 week' || '' }} |
| 86 | + working-directory: packages/alchemy |
| 87 | + run: | |
| 88 | + set -euo pipefail |
| 89 | + rm -f *.tgz |
| 90 | + bun pm pack --destination . |
| 91 | + tgz=$(ls *.tgz) |
| 92 | + echo "Publishing $tgz with tags $TAGS ttl=${TTL:-default}" |
| 93 | + curl -fsSL --show-error -X PUT \ |
| 94 | + "https://${PR_PACKAGE_HOST}/projects/alchemy/packages" \ |
| 95 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 96 | + -H "X-Tags: ${TAGS}" \ |
| 97 | + ${TTL:+-H "X-TTL: ${TTL}"} \ |
| 98 | + -H "Content-Type: application/gzip" \ |
| 99 | + --data-binary "@${tgz}" |
| 100 | +
|
| 101 | + - name: Publish better-auth |
| 102 | + env: |
| 103 | + TOKEN: ${{ secrets.PR_PACKAGE_TOKEN }} |
| 104 | + TAGS: ${{ steps.tags.outputs.tags }} |
| 105 | + TTL: ${{ github.event_name == 'pull_request' && '1 week' || '' }} |
| 106 | + working-directory: packages/better-auth |
| 107 | + run: | |
| 108 | + set -euo pipefail |
| 109 | + rm -f *.tgz |
| 110 | + bun pm pack --destination . |
| 111 | + tgz=$(ls *.tgz) |
| 112 | + echo "Publishing $tgz with tags $TAGS ttl=${TTL:-default}" |
| 113 | + curl -fsSL --show-error -X PUT \ |
| 114 | + "https://${PR_PACKAGE_HOST}/projects/@alchemy.run/better-auth/packages" \ |
| 115 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 116 | + -H "X-Tags: ${TAGS}" \ |
| 117 | + ${TTL:+-H "X-TTL: ${TTL}"} \ |
| 118 | + -H "Content-Type: application/gzip" \ |
| 119 | + --data-binary "@${tgz}" |
| 120 | +
|
| 121 | + - name: Publish pr-package |
| 122 | + env: |
| 123 | + TOKEN: ${{ secrets.PR_PACKAGE_TOKEN }} |
| 124 | + TAGS: ${{ steps.tags.outputs.tags }} |
| 125 | + TTL: ${{ github.event_name == 'pull_request' && '1 week' || '' }} |
| 126 | + working-directory: packages/pr-package |
| 127 | + run: | |
| 128 | + set -euo pipefail |
| 129 | + rm -f *.tgz |
| 130 | + bun pm pack --destination . |
| 131 | + tgz=$(ls *.tgz) |
| 132 | + echo "Publishing $tgz with tags $TAGS ttl=${TTL:-default}" |
| 133 | + curl -fsSL --show-error -X PUT \ |
| 134 | + "https://${PR_PACKAGE_HOST}/projects/@alchemy.run/pr-package/packages" \ |
| 135 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 136 | + -H "X-Tags: ${TAGS}" \ |
| 137 | + ${TTL:+-H "X-TTL: ${TTL}"} \ |
| 138 | + -H "Content-Type: application/gzip" \ |
| 139 | + --data-binary "@${tgz}" |
| 140 | +
|
| 141 | + # PR-only: leave a sticky comment with the install URLs pinned to this |
| 142 | + # commit. Uses pkg.ing (the canonical short host), which serves both |
| 143 | + # the pretty alias paths and the underlying /projects/:project/tags/:tag |
| 144 | + # API directly. |
| 145 | + - name: Generate bot token |
| 146 | + if: github.event_name == 'pull_request' |
| 147 | + id: bot-token |
| 148 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 149 | + with: |
| 150 | + app-id: ${{ secrets.ALCHEMY_VERSION_BOT_ID }} |
| 151 | + private-key: ${{ secrets.ALCHEMY_VERSION_BOT_PRIVATE_KEY }} |
| 152 | + |
| 153 | + - name: Comment on PR |
| 154 | + if: github.event_name == 'pull_request' |
| 155 | + env: |
| 156 | + GH_TOKEN: ${{ steps.bot-token.outputs.token }} |
| 157 | + REPO: ${{ github.repository }} |
| 158 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 159 | + SHORT_SHA: ${{ steps.tags.outputs.short }} |
| 160 | + run: | |
| 161 | + set -euo pipefail |
| 162 | + MARKER="<!-- pr-package-comment -->" |
| 163 | + # Use `bun add <name>@<url>` instead of `bun add <url>`. Without an |
| 164 | + # explicit name, bun's resolver hits a DependencyLoop bug when the |
| 165 | + # consumer already has the same package name resolved from npm |
| 166 | + # (oven-sh/bun#5789, oven-sh/bun#17946). The aliased form bypasses |
| 167 | + # the buggy reconciliation path and works on fresh and existing |
| 168 | + # projects alike. |
| 169 | + BODY=$(printf '%s\n\nInstall the packages built from this commit:\n\n**alchemy**\n```sh\nbun add alchemy@https://pkg.ing/alchemy/%s\n```\n\n**@alchemy.run/better-auth**\n```sh\nbun add @alchemy.run/better-auth@https://pkg.ing/@alchemy.run/better-auth/%s\n```\n\n**@alchemy.run/pr-package**\n```sh\nbun add @alchemy.run/pr-package@https://pkg.ing/@alchemy.run/pr-package/%s\n```\n' "$MARKER" "$SHORT_SHA" "$SHORT_SHA" "$SHORT_SHA") |
| 170 | +
|
| 171 | + existing=$(gh api --paginate \ |
| 172 | + "repos/${REPO}/issues/${PR_NUMBER}/comments" \ |
| 173 | + --jq ".[] | select(.user.login == \"alchemy-version-bot[bot]\") | select(.body | startswith(\"${MARKER}\")) | .id" \ |
| 174 | + | head -1) |
| 175 | +
|
| 176 | + if [ -n "$existing" ]; then |
| 177 | + gh api -X PATCH "repos/${REPO}/issues/comments/${existing}" -f body="$BODY" |
| 178 | + else |
| 179 | + gh api -X POST "repos/${REPO}/issues/${PR_NUMBER}/comments" -f body="$BODY" |
| 180 | + fi |
0 commit comments