From 728a7523b252c9a9d6b521f8dd84ce494fd5392d Mon Sep 17 00:00:00 2001 From: Rui Date: Fri, 4 Apr 2025 13:49:40 -0500 Subject: [PATCH 1/2] chore: introduces publish workflow on comment --- .github/workflows/publish-package.yml | 124 ++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 .github/workflows/publish-package.yml diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml new file mode 100644 index 0000000..1b6260c --- /dev/null +++ b/.github/workflows/publish-package.yml @@ -0,0 +1,124 @@ +name: Publish Package + +on: + issue_comment: + types: [created] + pull_request_review_comment: + types: [created] + +permissions: + contents: write + packages: write + issues: write + pull-requests: write + +jobs: + publish: + if: ${{ startsWith(github.event.comment.body, '/publish') }} + runs-on: ubuntu-latest + steps: + - name: Check if commenter has write access + id: check_permissions + uses: actions-cool/check-user-permission@v2 + with: + require: "write" + username: ${{ github.event.comment.user.login }} + + - name: Fail if user doesn't have permissions + if: steps.check_permissions.outputs.require-result != 'true' + run: | + echo "::error::Only users with write permissions can run this workflow." + exit 1 + + - name: Extract version type + id: version-type + run: | + COMMENT="${{ github.event.comment.body }}" + if [[ "$COMMENT" == "/publish major" ]]; then + echo "type=major" >> $GITHUB_OUTPUT + elif [[ "$COMMENT" == "/publish minor" ]]; then + echo "type=minor" >> $GITHUB_OUTPUT + elif [[ "$COMMENT" == "/publish patch" ]]; then + echo "type=patch" >> $GITHUB_OUTPUT + else + echo "Unknown version type. Use '/publish major', '/publish minor', or '/publish patch'" + exit 1 + fi + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: "18.15" + cache: "yarn" + registry-url: "https://registry.npmjs.org" + + - name: Install dependencies + run: yarn install --frozen-lockfile --non-interactive + + - name: Build + run: yarn build + + - name: Configure Git + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + + - name: Bump version + id: bump-version + run: | + VERSION_TYPE="${{ steps.version-type.outputs.type }}" + yarn version --"$VERSION_TYPE" --no-git-tag-version + NEW_VERSION=$(node -p "require('./package.json').version") + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT + git add package.json + git commit -m "chore: bump version to $NEW_VERSION" + git push + + - name: Create tag + run: | + NEW_VERSION=${{ steps.bump-version.outputs.new_version }} + git tag v$NEW_VERSION + git push --tags + + - name: Create GitHub Release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: v${{ steps.bump-version.outputs.new_version }} + release_name: v${{ steps.bump-version.outputs.new_version }} + draft: false + prerelease: false + + - name: Publish to NPM + run: yarn publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Comment on issue/PR + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const issueNumber = context.issue.number || context.payload.pull_request?.number; + if (!issueNumber) { + console.log("Could not determine issue/PR number"); + return; + } + + const newVersion = '${{ steps.bump-version.outputs.new_version }}'; + const releaseUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/releases/tag/v${newVersion}`; + const npmUrl = `https://www.npmjs.com/package/@jam.dev/sdk/v/${newVersion}`; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, + body: `🎉 Version v${newVersion} has been published!\n\n- [GitHub Release](${releaseUrl})\n- [NPM Package](${npmUrl})` + }); From c199e7fd92bfccb2e359f9e283aa23e82e33ae84 Mon Sep 17 00:00:00 2001 From: Rui Date: Mon, 7 Apr 2025 16:16:17 -0500 Subject: [PATCH 2/2] chore: changes trigger hook to be `pull_request` --- .github/workflows/publish-package.yml | 64 ++++++--------------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index 1b6260c..d32a206 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -1,53 +1,39 @@ name: Publish Package on: - issue_comment: - types: [created] - pull_request_review_comment: - types: [created] + pull_request: + types: [closed] + branches: + # runs on main PRs targeting main + - main permissions: - contents: write - packages: write - issues: write - pull-requests: write + contents: write # To push version bump commit and tag jobs: publish: - if: ${{ startsWith(github.event.comment.body, '/publish') }} + if: github.event.pull_request.merged == true && (contains(github.event.pull_request.labels.*.name, 'release-major') || contains(github.event.pull_request.labels.*.name, 'release-minor') || contains(github.event.pull_request.labels.*.name, 'release-patch')) runs-on: ubuntu-latest steps: - - name: Check if commenter has write access - id: check_permissions - uses: actions-cool/check-user-permission@v2 - with: - require: "write" - username: ${{ github.event.comment.user.login }} - - - name: Fail if user doesn't have permissions - if: steps.check_permissions.outputs.require-result != 'true' - run: | - echo "::error::Only users with write permissions can run this workflow." - exit 1 - - - name: Extract version type + - name: Determine version type from label id: version-type run: | - COMMENT="${{ github.event.comment.body }}" - if [[ "$COMMENT" == "/publish major" ]]; then + LABELS="${{ toJson(github.event.pull_request.labels.*.name) }}" + if echo "$LABELS" | grep -q '"release-major"'; then echo "type=major" >> $GITHUB_OUTPUT - elif [[ "$COMMENT" == "/publish minor" ]]; then + elif echo "$LABELS" | grep -q '"release-minor"'; then echo "type=minor" >> $GITHUB_OUTPUT - elif [[ "$COMMENT" == "/publish patch" ]]; then + elif echo "$LABELS" | grep -q '"release-patch"'; then echo "type=patch" >> $GITHUB_OUTPUT else - echo "Unknown version type. Use '/publish major', '/publish minor', or '/publish patch'" + echo "::error::No valid release label found (release-major, release-minor, release-patch)." exit 1 fi - name: Checkout uses: actions/checkout@v3 with: + ref: "main" fetch-depth: 0 token: ${{ secrets.GITHUB_TOKEN }} @@ -100,25 +86,3 @@ jobs: run: yarn publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - - - name: Comment on issue/PR - uses: actions/github-script@v6 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const issueNumber = context.issue.number || context.payload.pull_request?.number; - if (!issueNumber) { - console.log("Could not determine issue/PR number"); - return; - } - - const newVersion = '${{ steps.bump-version.outputs.new_version }}'; - const releaseUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/releases/tag/v${newVersion}`; - const npmUrl = `https://www.npmjs.com/package/@jam.dev/sdk/v/${newVersion}`; - - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issueNumber, - body: `🎉 Version v${newVersion} has been published!\n\n- [GitHub Release](${releaseUrl})\n- [NPM Package](${npmUrl})` - });