|
1 | 1 | name: Release |
| 2 | + |
2 | 3 | on: |
3 | 4 | pull_request: |
4 | | - types: [closed] |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + types: |
| 9 | + - closed |
| 10 | + |
5 | 11 | jobs: |
6 | 12 | release: |
| 13 | + if: | |
| 14 | + github.event.pull_request.merged == true && |
| 15 | + contains(github.event.pull_request.labels.*.name, 'Type: Release') |
7 | 16 | runs-on: ubuntu-latest |
8 | | - if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'Type: Release') |
| 17 | + environment: |
| 18 | + name: npm |
9 | 19 | permissions: |
10 | 20 | contents: write |
11 | | - id-token: write |
12 | | - pull-requests: write |
| 21 | + id-token: write # OIDC |
| 22 | + outputs: |
| 23 | + released: ${{ steps.tag-check.outputs.exists == 'false' }} |
| 24 | + version: ${{ steps.package.outputs.version }} |
| 25 | + package-name: ${{ steps.package.outputs.name }} |
| 26 | + release-url: ${{ steps.create-release.outputs.url }} |
13 | 27 | steps: |
14 | 28 | - name: Checkout |
15 | | - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 29 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 30 | + with: |
| 31 | + persist-credentials: false |
| 32 | + |
| 33 | + - name: Get package info |
| 34 | + id: package |
| 35 | + run: | |
| 36 | + VERSION=$(jq -r '.version' package.json) |
| 37 | + PACKAGE_NAME=$(jq -r '.name' package.json) |
| 38 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 39 | + echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT |
| 40 | + |
| 41 | + - name: Check if tag exists |
| 42 | + id: tag-check |
| 43 | + run: | |
| 44 | + if git rev-parse "v$VERSION" >/dev/null 2>&1; then |
| 45 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 46 | + else |
| 47 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 48 | + fi |
| 49 | + env: |
| 50 | + VERSION: ${{ steps.package.outputs.version }} |
| 51 | + |
16 | 52 | - name: Install pnpm |
| 53 | + if: steps.tag-check.outputs.exists == 'false' |
17 | 54 | uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0 |
| 55 | + |
18 | 56 | - name: Setup Node.js |
19 | | - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 57 | + if: steps.tag-check.outputs.exists == 'false' |
| 58 | + uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0 |
20 | 59 | with: |
21 | | - cache: "pnpm" |
22 | | - node-version: lts/* |
23 | | - registry-url: "https://registry.npmjs.org" |
| 60 | + node-version: 'lts/*' |
| 61 | + registry-url: 'https://registry.npmjs.org' |
| 62 | + |
| 63 | + - name: Install latest npm |
| 64 | + if: steps.tag-check.outputs.exists == 'false' |
| 65 | + run: | |
| 66 | + echo "Current npm version: $(npm -v)" |
| 67 | + npm install -g npm@latest |
| 68 | + echo "Updated npm version: $(npm -v)" |
| 69 | + |
24 | 70 | - name: Install dependencies |
25 | | - run: pnpm install |
26 | | - - name: Build |
| 71 | + if: steps.tag-check.outputs.exists == 'false' |
| 72 | + run: pnpm install --frozen-lockfile |
| 73 | + |
| 74 | + - name: Build package |
| 75 | + if: steps.tag-check.outputs.exists == 'false' |
27 | 76 | run: pnpm run build |
28 | | - - name: Publish to npm |
29 | | - run: npm publish --provenance --access public |
30 | | - env: |
31 | | - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
32 | | - - name: Get version |
33 | | - id: version |
34 | | - run: echo "version=$(node -p 'require(\"./package.json\").version')" >> "$GITHUB_OUTPUT" |
35 | | - - name: Create GitHub Release |
| 77 | + |
| 78 | + - name: Publish to npm with provenance |
| 79 | + if: steps.tag-check.outputs.exists == 'false' |
| 80 | + run: pnpm publish --access public |
| 81 | + |
| 82 | + - name: Create GitHub Release with tag |
| 83 | + id: create-release |
| 84 | + if: steps.tag-check.outputs.exists == 'false' |
| 85 | + run: | |
| 86 | + RELEASE_URL=$(gh release create "v$VERSION" \ |
| 87 | + --title "v$VERSION" \ |
| 88 | + --target "$SHA" \ |
| 89 | + --notes "$PR_BODY") |
| 90 | + echo "url=$RELEASE_URL" >> $GITHUB_OUTPUT |
36 | 91 | env: |
37 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
38 | | - TAG_NAME: v${{ steps.version.outputs.version }} |
| 92 | + GH_TOKEN: ${{ github.token }} |
| 93 | + VERSION: ${{ steps.package.outputs.version }} |
| 94 | + SHA: ${{ github.sha }} |
| 95 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 96 | + |
| 97 | + comment: |
| 98 | + needs: release |
| 99 | + if: | |
| 100 | + always() && |
| 101 | + github.event_name == 'pull_request' && |
| 102 | + needs.release.outputs.released == 'true' |
| 103 | + runs-on: ubuntu-latest |
| 104 | + permissions: |
| 105 | + pull-requests: write |
| 106 | + steps: |
| 107 | + - name: Comment on PR - Success |
| 108 | + if: needs.release.result == 'success' |
39 | 109 | run: | |
40 | | - gh release create "$TAG_NAME" --generate-notes |
41 | | - - name: Comment on PR (success) |
42 | | - if: success() |
| 110 | + gh pr comment "$PR_NUMBER" \ |
| 111 | + --repo "$REPOSITORY" \ |
| 112 | + --body "✅ **Release v$VERSION completed successfully!** |
| 113 | + |
| 114 | + - 📦 npm package: https://www.npmjs.com/package/$PACKAGE_NAME/v/$VERSION |
| 115 | + - 🏷️ GitHub Release: $RELEASE_URL |
| 116 | + - 🔗 Workflow run: $SERVER_URL/$REPOSITORY/actions/runs/$RUN_ID" |
43 | 117 | env: |
44 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + GH_TOKEN: ${{ github.token }} |
45 | 119 | PR_NUMBER: ${{ github.event.pull_request.number }} |
46 | | - VERSION: ${{ steps.version.outputs.version }} |
| 120 | + VERSION: ${{ needs.release.outputs.version }} |
| 121 | + PACKAGE_NAME: ${{ needs.release.outputs.package-name }} |
| 122 | + RELEASE_URL: ${{ needs.release.outputs.release-url }} |
| 123 | + SERVER_URL: ${{ github.server_url }} |
| 124 | + REPOSITORY: ${{ github.repository }} |
| 125 | + RUN_ID: ${{ github.run_id }} |
| 126 | + |
| 127 | + - name: Comment on PR - Failure |
| 128 | + if: needs.release.result == 'failure' |
47 | 129 | run: | |
48 | | - gh pr comment "$PR_NUMBER" --body "Released as v${VERSION}" |
49 | | - - name: Comment on PR (failure) |
50 | | - if: failure() |
| 130 | + gh pr comment "$PR_NUMBER" \ |
| 131 | + --repo "$REPOSITORY" \ |
| 132 | + --body "❌ **Release v$VERSION failed** |
| 133 | + |
| 134 | + Please check the workflow logs for details. |
| 135 | + 🔗 Workflow run: $SERVER_URL/$REPOSITORY/actions/runs/$RUN_ID" |
51 | 136 | env: |
52 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + GH_TOKEN: ${{ github.token }} |
53 | 138 | PR_NUMBER: ${{ github.event.pull_request.number }} |
54 | | - run: | |
55 | | - gh pr comment "$PR_NUMBER" --body "Release failed. Check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details." |
| 139 | + VERSION: ${{ needs.release.outputs.version }} |
| 140 | + SERVER_URL: ${{ github.server_url }} |
| 141 | + REPOSITORY: ${{ github.repository }} |
| 142 | + RUN_ID: ${{ github.run_id }} |
0 commit comments