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