Unverified Commit 87f95fcc authored by Hong Minhee's avatar Hong Minhee
Browse files

Add pull request pre-release builds and publishing

Enhance CI workflow to automatically build and publish pre-release versions
for pull requests with versioning scheme including PR number, build number,
and commit hash. Add automatic PR comments to notify about published
pre-releases and update documentation.
parent 67c26e13
Loading
Loading
Loading
Loading
+145 −42
Original line number Diff line number Diff line
name: build
on: [push, pull_request]
on: [push, pull_request_target]

jobs:
  test:
@@ -14,7 +14,13 @@ jobs:
      checks: write
      pull-requests: write
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -54,7 +60,13 @@ jobs:
      fail-fast: false
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -74,7 +86,13 @@ jobs:
      fail-fast: false
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -93,7 +111,13 @@ jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -105,7 +129,13 @@ jobs:
      id-token: write
      contents: read
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -134,8 +164,18 @@ jobs:
    permissions:
      id-token: write
      contents: write
      pull-requests: write
    outputs:
      version: ${{ steps.versioning.outputs.version }}
      short_version: ${{ steps.versioning.outputs.short_version }}
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -145,7 +185,7 @@ jobs:
    - uses: pnpm/action-setup@v4
      with:
        version: 10
    - if: github.ref_type == 'branch'
    - if: github.event_name == 'push' && github.ref_type == 'branch'
      run: |
        jq \
          --arg build "$GITHUB_RUN_NUMBER" \
@@ -153,13 +193,27 @@ jobs:
          '.version = .version + "-dev." + $build + "+" + $commit' \
          deno.json > deno.json.tmp
        mv deno.json.tmp deno.json
      working-directory: ${{ github.workspace }}/fedify/
    - if: github.event_name == 'pull_request_target'
      run: |
        jq \
          --arg pr_number "$PR_NUMBER" \
          --arg build "$GITHUB_RUN_NUMBER" \
          --arg commit "${GITHUB_SHA::8}" \
          '.version = .version + "-dev." + $build + "+" + $commit' \
          package.json > package.json.tmp
        mv package.json.tmp package.json
          --arg commit "${PR_SHA::8}" \
          '.version = .version + "-pr." + $pr_number + "." + $build + "+" + $commit' \
          deno.json > deno.json.tmp
        mv deno.json.tmp deno.json
      working-directory: ${{ github.workspace }}/fedify/
      env:
        PR_NUMBER: ${{ github.event.pull_request.number }}
        PR_SHA: ${{ github.event.pull_request.head.sha }}
    - id: versioning
      run: |
        set -ex
        echo version="$(jq -r .version deno.json)" >> $GITHUB_OUTPUT
        echo short_version="$(jq -r .version deno.json | sed 's/[+].*//')" >> $GITHUB_OUTPUT
      working-directory: ${{ github.workspace }}/fedify/
    - run: deno task -r sync-version
    - if: github.ref_type == 'tag'
      run: |
        set -ex
@@ -202,29 +256,27 @@ jobs:
          fedify/fedify-fedify-*.tgz
          cli/fedify-cli-*
        generate_release_notes: false
    - if: |
        github.event_name == 'push' &&
        github.ref_type == 'tag' || github.ref == 'refs/heads/main'
      run: deno task publish --allow-dirty
    - run: deno task publish --allow-dirty
      working-directory: ${{ github.workspace }}/fedify/
    - if: |
        github.event_name == 'push' &&
        github.ref_type == 'tag' || github.ref == 'refs/heads/main'
      run: deno task publish --allow-dirty
    - run: deno task publish --allow-dirty
      working-directory: ${{ github.workspace }}/cli/
    - if: |
        github.event_name == 'push' &&
        github.ref_type == 'tag' || github.ref == 'refs/heads/main'
      run: |
    - run: |
        set -ex
        npm config set //registry.npmjs.org/:_authToken "$NPM_AUTH_TOKEN"
        if [[ "$GITHUB_REF_TYPE" = "tag" ]]; then
          npm publish --provenance --access public fedify-fedify-*.tgz
        elif [[ "$GITHUB_EVENT_NAME" = "pull_request_target" ]]; then
          npm publish \
            --provenance \
            --access public \
            --tag "pr-$PR_NUMBER" \
            fedify-fedify-*.tgz
        else
          npm publish --provenance --access public --tag dev fedify-fedify-*.tgz
        fi
      env:
        NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
        PR_NUMBER: ${{ github.event.pull_request.number }}
      working-directory: ${{ github.workspace }}/fedify/
    - if: github.event_name == 'push' && github.ref_type == 'tag'
      run: |
@@ -234,6 +286,27 @@ jobs:
      env:
        NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
      working-directory: ${{ github.workspace }}/cli/
    - if: github.event_name == 'pull_request_target'
      uses: thollander/actions-comment-pull-request@v3
      with:
        pr-number: ${{ github.event.pull_request.number }}
        comment-tag: publish
        mode: delete
    - if: github.event_name == 'pull_request_target'
      uses: thollander/actions-comment-pull-request@v3
      with:
        message: |
          The latest push to this pull request has been published to JSR and npm as a pre-release:

          - [`jsr:@fedify/fedify@${{ steps.versioning.outputs.version }}`][1]
          - [`jsr:@fedify/cli@${{ steps.versioning.outputs.version }}`][2]
          - [`npm:@fedify/fedify@${{ steps.versioning.outputs.short_version }}`][3]

          [1]: https://jsr.io/@fedify/fedify@${{ steps.versioning.outputs.version }}
          [2]: https://jsr.io/@fedify/cli@${{ steps.versioning.outputs.version }}
          [3]: https://www.npmjs.com/package/@fedify/fedify/v/${{ steps.versioning.outputs.short_version }}
        pr-number: ${{ github.event.pull_request.number }}
        comment-tag: publish

  publish-examples-blog:
    if: github.event_name == 'push'
@@ -243,7 +316,13 @@ jobs:
      id-token: write
      contents: read
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: denoland/setup-deno@v2
      with:
        deno-version: v2.x
@@ -256,33 +335,39 @@ jobs:
        root: .

  publish-docs:
    if: github.event_name == 'push'
    needs: [publish]
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      pages: write
      deployments: write
      pull-requests: write
      statuses: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
    - uses: actions/checkout@v4
    - if: github.event_name == 'push'
      uses: actions/checkout@v4
    - if: github.event_name == 'pull_request_target'
      uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: pnpm/action-setup@v4
      with:
        version: 10
    - run: |
        set -ex
        pnpm install
        pnpm add -D "@fedify/fedify@$SHORT_VERSION"
        if [[ "$GITHUB_EVENT_NAME" = "push" && "$GITHUB_REF_TYPE" = "tag" ]]; then
          pnpm add -D "@fedify/fedify@$GITHUB_REF_NAME"
          EXTRA_NAV_TEXT=Unstable \
          EXTRA_NAV_LINK="$UNSTABLE_DOCS_URL" \
          SITEMAP_HOSTNAME="$STABLE_DOCS_URL" \
          JSR_REF_VERSION=stable \
          pnpm run build
        else
          pnpm add -D @fedify/fedify@dev
          EXTRA_NAV_TEXT=Stable \
          EXTRA_NAV_LINK="$STABLE_DOCS_URL" \
          SITEMAP_HOSTNAME="$UNSTABLE_DOCS_URL" \
@@ -290,6 +375,7 @@ jobs:
          pnpm run build
        fi
      env:
        SHORT_VERSION: ${{ needs.publish.outputs.short_version }}
        PLAUSIBLE_DOMAIN: ${{ secrets.PLAUSIBLE_DOMAIN }}
        STABLE_DOCS_URL: ${{ vars.STABLE_DOCS_URL }}
        UNSTABLE_DOCS_URL: ${{ vars.UNSTABLE_DOCS_URL }}
@@ -300,17 +386,34 @@ jobs:
    - id: deployment
      if: github.event_name == 'push' && github.ref_type == 'tag'
      uses: actions/deploy-pages@v4
    - if: github.event_name == 'pull_request' || github.ref_type == 'branch'
      uses: nwtgck/actions-netlify@v3.0
      with:
        publish-dir: docs/.vitepress/dist
        production-branch: main
        github-token: ${{ github.token }}
        enable-pull-request-comment: false
        enable-commit-comment: false
      env:
        NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
        NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
      timeout-minutes: 2
    - if: github.event_name == 'push' && github.ref == 'refs/heads/main'
      uses: cloudflare/wrangler-action@v3
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        gitHubToken: ${{ github.token }}
        command: >-
          pages deploy docs/.vitepress/dist
          --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }}
    - if: github.event_name == 'pull_request_target'
      id: wrangler
      uses: cloudflare/wrangler-action@v3
      with:
        apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        gitHubToken: ${{ github.token }}
        command: >-
          pages deploy docs/.vitepress/dist
          --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }}
          --branch=pr-${{ github.event.pull_request.number }}
    - if: github.event_name == 'pull_request_target'
      uses: thollander/actions-comment-pull-request@v3
      with:
        message: |
          The docs for this pull request have been published:

          <${{ steps.wrangler.outputs.deployment-url }}>
        pr-number: ${{ github.event.pull_request.number }}
        comment-tag: docs

# cSpell: ignore submark softprops npmjs deployctl nwtgck
# cSpell: ignore submark softprops npmjs deployctl nwtgck thollander elif
+10 −0
Original line number Diff line number Diff line
@@ -131,6 +131,16 @@ A patch set should include the following:

Feature pull requests should target the *main* branch.

### Pull request builds

Each pull request is automatically built and published to the JSR and npm
registries as a pre-release.  You can test the pull request by installing
the pre-release version of the Fedify library.  The version number of
the pre-release version consists of the base version number, the pull request
number, the build number, and the commit hash, which looks like
`1.2.3-pr.456.789+abcdef01`.  You can find the exact version number in
the comment left by the build process in the pull request.


Build
-----