|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + types: |
| 9 | + - closed |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + if: | |
| 14 | + github.event.pull_request.merged == true && |
| 15 | + contains(github.event.pull_request.labels.*.name, 'Type: Release') |
| 16 | + runs-on: ubuntu-latest |
| 17 | + environment: |
| 18 | + name: npm |
| 19 | + permissions: |
| 20 | + contents: 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 }} |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 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 | + |
| 52 | + |
| 53 | + - name: Setup Node.js |
| 54 | + if: steps.tag-check.outputs.exists == 'false' |
| 55 | + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 |
| 56 | + with: |
| 57 | + node-version: 'lts/*' |
| 58 | + registry-url: 'https://registry.npmjs.org' |
| 59 | + |
| 60 | + - name: Install latest npm |
| 61 | + if: steps.tag-check.outputs.exists == 'false' |
| 62 | + run: | |
| 63 | + echo "Current npm version: $(npm -v)" |
| 64 | + npm install -g npm@latest |
| 65 | + echo "Updated npm version: $(npm -v)" |
| 66 | + |
| 67 | + - name: Install dependencies |
| 68 | + if: steps.tag-check.outputs.exists == 'false' |
| 69 | + run: yarn install --frozen-lockfile |
| 70 | + |
| 71 | + - name: Build package |
| 72 | + if: steps.tag-check.outputs.exists == 'false' |
| 73 | + run: yarn run build |
| 74 | + |
| 75 | + - name: Publish to npm with provenance |
| 76 | + if: steps.tag-check.outputs.exists == 'false' |
| 77 | + run: npm publish --access public |
| 78 | + |
| 79 | + - name: Create GitHub Release with tag |
| 80 | + id: create-release |
| 81 | + if: steps.tag-check.outputs.exists == 'false' |
| 82 | + run: | |
| 83 | + RELEASE_URL=$(gh release create "v$VERSION" \ |
| 84 | + --title "v$VERSION" \ |
| 85 | + --target "$SHA" \ |
| 86 | + --notes "$PR_BODY") |
| 87 | + echo "url=$RELEASE_URL" >> $GITHUB_OUTPUT |
| 88 | + env: |
| 89 | + GH_TOKEN: ${{ github.token }} |
| 90 | + VERSION: ${{ steps.package.outputs.version }} |
| 91 | + SHA: ${{ github.sha }} |
| 92 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 93 | + |
| 94 | + comment: |
| 95 | + needs: release |
| 96 | + if: | |
| 97 | + always() && |
| 98 | + github.event_name == 'pull_request' && |
| 99 | + needs.release.outputs.released == 'true' |
| 100 | + runs-on: ubuntu-latest |
| 101 | + permissions: |
| 102 | + pull-requests: write |
| 103 | + steps: |
| 104 | + - name: Comment on PR - Success |
| 105 | + if: needs.release.result == 'success' |
| 106 | + run: | |
| 107 | + gh pr comment "$PR_NUMBER" \ |
| 108 | + --repo "$REPOSITORY" \ |
| 109 | + --body "✅ **Release v$VERSION completed successfully!** |
| 110 | + |
| 111 | + - 📦 npm package: https://www.npmjs.com/package/$PACKAGE_NAME/v/$VERSION |
| 112 | + - 🏷️ GitHub Release: $RELEASE_URL |
| 113 | + - 🔗 Workflow run: $SERVER_URL/$REPOSITORY/actions/runs/$RUN_ID" |
| 114 | + env: |
| 115 | + GH_TOKEN: ${{ github.token }} |
| 116 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 117 | + VERSION: ${{ needs.release.outputs.version }} |
| 118 | + PACKAGE_NAME: ${{ needs.release.outputs.package-name }} |
| 119 | + RELEASE_URL: ${{ needs.release.outputs.release-url }} |
| 120 | + SERVER_URL: ${{ github.server_url }} |
| 121 | + REPOSITORY: ${{ github.repository }} |
| 122 | + RUN_ID: ${{ github.run_id }} |
| 123 | + |
| 124 | + - name: Comment on PR - Failure |
| 125 | + if: needs.release.result == 'failure' |
| 126 | + run: | |
| 127 | + gh pr comment "$PR_NUMBER" \ |
| 128 | + --repo "$REPOSITORY" \ |
| 129 | + --body "❌ **Release v$VERSION failed** |
| 130 | + |
| 131 | + Please check the workflow logs for details. |
| 132 | + 🔗 Workflow run: $SERVER_URL/$REPOSITORY/actions/runs/$RUN_ID" |
| 133 | + env: |
| 134 | + GH_TOKEN: ${{ github.token }} |
| 135 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 136 | + VERSION: ${{ needs.release.outputs.version }} |
| 137 | + SERVER_URL: ${{ github.server_url }} |
| 138 | + REPOSITORY: ${{ github.repository }} |
| 139 | + RUN_ID: ${{ github.run_id }} |
0 commit comments