Skip to content

PR Tarball (Publish) #7

PR Tarball (Publish)

PR Tarball (Publish) #7

name: PR Tarball (Publish)
# Runs in the context of the BASE branch after PR Tarball (Build) completes.
# This workflow has write permissions but never checks out or executes PR code.
on:
workflow_run:
workflows: ['PR Tarball (Build)']
types: [completed]
permissions:
contents: write
pull-requests: write
jobs:
publish-tarball:
runs-on: ubuntu-latest
if: >-
github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
steps:
- name: Get PR number
id: pr
env:
GH_TOKEN: ${{ github.token }}
RUN_ID: ${{ github.event.workflow_run.id }}
REPO: ${{ github.repository }}
run: |
# The triggering workflow_run carries the PR info in its pull_requests array
PR_NUMBER=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}" \
--jq '.pull_requests[0].number')
if [[ -z "$PR_NUMBER" || "$PR_NUMBER" == "null" ]]; then
echo "No PR associated with this workflow run (likely a push to main). Skipping."
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
- name: Download tarball artifact
if: steps.pr.outputs.skip != 'true'
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: pr-tarball
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Get tarball info
if: steps.pr.outputs.skip != 'true'
id: tarball
run: |
TARBALL_NAME=$(ls *.tgz | head -1 | xargs basename)
echo "name=$TARBALL_NAME" >> "$GITHUB_OUTPUT"
- name: Create or update PR release
if: steps.pr.outputs.skip != 'true'
id: release
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
TARBALL_NAME: ${{ steps.tarball.outputs.name }}
PR_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
REPO: ${{ github.repository }}
run: |
TAG="pr-${PR_NUMBER}-tarball"
# Delete existing release if it exists (to update the tarball)
gh release delete "$TAG" --yes --cleanup-tag 2>/dev/null || true
# Create a new pre-release with the tarball
gh release create "$TAG" \
"${TARBALL_NAME}" \
--title "PR #${PR_NUMBER} Tarball" \
--notes "Auto-generated tarball for PR #${PR_NUMBER}." \
--draft \
--target "$PR_HEAD_SHA"
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${TARBALL_NAME}"
echo "url=$DOWNLOAD_URL" >> "$GITHUB_OUTPUT"
- name: Comment on PR
if: steps.pr.outputs.skip != 'true'
uses: marocchino/sticky-pull-request-comment@0ea0beb66eb9baf113663a64ec522f60e49231c0 # v3.0.4
with:
number: ${{ steps.pr.outputs.number }}
header: tarball
message: |
## Package Tarball
**[${{ steps.tarball.outputs.name }}](${{ steps.release.outputs.url }})**
### How to install
```bash
npm install ${{ steps.release.outputs.url }}
```