|
| 1 | +name: Release Preview |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + |
| 6 | +jobs: |
| 7 | + preview: |
| 8 | + name: Preview Release |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }} |
| 20 | + |
| 21 | + - name: Setup Node.js |
| 22 | + uses: actions/setup-node@v4 |
| 23 | + with: |
| 24 | + node-version: 'lts/*' |
| 25 | + |
| 26 | + - name: Install semantic-release |
| 27 | + run: | |
| 28 | + npm install -g semantic-release@18.0.0 \ |
| 29 | + @semantic-release/commit-analyzer@9.0.2 \ |
| 30 | + @semantic-release/release-notes-generator@10.0.3 \ |
| 31 | + @semantic-release/github@8.0.7 \ |
| 32 | + conventional-changelog-conventionalcommits@4.6.3 |
| 33 | +
|
| 34 | + - name: Run semantic-release (dry-run) |
| 35 | + id: semantic |
| 36 | + run: | |
| 37 | + # Run semantic-release in dry-run mode and capture output |
| 38 | + OUTPUT=$(npx semantic-release --dry-run --no-ci 2>&1 || true) |
| 39 | + echo "$OUTPUT" |
| 40 | +
|
| 41 | + # Extract version information |
| 42 | + NEW_VERSION=$(echo "$OUTPUT" | grep -oP "The next release version is \K[0-9]+\.[0-9]+\.[0-9]+" || echo "") |
| 43 | + RELEASE_TYPE=$(echo "$OUTPUT" | grep -oP "Analysis of \d+ commits complete: \K\w+ release" | sed 's/ release//' || echo "") |
| 44 | +
|
| 45 | + # Save to outputs |
| 46 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 47 | + echo "release_type=$RELEASE_TYPE" >> $GITHUB_OUTPUT |
| 48 | +
|
| 49 | + # Save full output for comment |
| 50 | + echo "full_output<<EOF" >> $GITHUB_OUTPUT |
| 51 | + echo "$OUTPUT" >> $GITHUB_OUTPUT |
| 52 | + echo "EOF" >> $GITHUB_OUTPUT |
| 53 | + env: |
| 54 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + - name: Display Preview |
| 57 | + run: | |
| 58 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 59 | + echo " RELEASE PREVIEW" |
| 60 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 61 | + echo "" |
| 62 | + if [ -n "${{ steps.semantic.outputs.new_version }}" ]; then |
| 63 | + echo "Version: v${{ steps.semantic.outputs.new_version }}" |
| 64 | + echo "Release Type: ${{ steps.semantic.outputs.release_type }}" |
| 65 | + echo "Status: Release will be published" |
| 66 | + else |
| 67 | + echo "Status: No release will be published" |
| 68 | + echo "Reason: No relevant changes detected" |
| 69 | + fi |
| 70 | + echo "" |
| 71 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 72 | +
|
| 73 | + - name: Comment on PR |
| 74 | + if: github.event_name == 'pull_request' |
| 75 | + uses: actions/github-script@v7 |
| 76 | + with: |
| 77 | + script: | |
| 78 | + const newVersion = '${{ steps.semantic.outputs.new_version }}'; |
| 79 | + const releaseType = '${{ steps.semantic.outputs.release_type }}'; |
| 80 | +
|
| 81 | + let body; |
| 82 | + if (newVersion) { |
| 83 | + body = `## Release Preview |
| 84 | +
|
| 85 | + **Version:** \`v${newVersion}\` |
| 86 | + **Release Type:** \`${releaseType}\` |
| 87 | + **Status:** Release will be published when merged to main |
| 88 | +
|
| 89 | + <details> |
| 90 | + <summary> View semantic-release analysis</summary> |
| 91 | +
|
| 92 | + \`\`\` |
| 93 | + ${{ steps.semantic.outputs.full_output }} |
| 94 | + \`\`\` |
| 95 | + </details> |
| 96 | +
|
| 97 | + --- |
| 98 | + *This preview is generated by semantic-release dry-run mode*`; |
| 99 | + } else { |
| 100 | + body = `## Release Preview |
| 101 | +
|
| 102 | + **Status:** No release will be published |
| 103 | + **Reason:** No relevant changes detected |
| 104 | +
|
| 105 | + <details> |
| 106 | + <summary> View semantic-release analysis</summary> |
| 107 | +
|
| 108 | + \`\`\` |
| 109 | + ${{ steps.semantic.outputs.full_output }} |
| 110 | + \`\`\` |
| 111 | + </details> |
| 112 | +
|
| 113 | + --- |
| 114 | + *This preview is generated by semantic-release dry-run mode*`; |
| 115 | + } |
| 116 | +
|
| 117 | + // Find existing comment |
| 118 | + const { data: comments } = await github.rest.issues.listComments({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + issue_number: context.issue.number, |
| 122 | + }); |
| 123 | +
|
| 124 | + const botComment = comments.find(comment => |
| 125 | + comment.user.type === 'Bot' && |
| 126 | + comment.body.includes('Release Preview') |
| 127 | + ); |
| 128 | +
|
| 129 | + // Update or create comment |
| 130 | + if (botComment) { |
| 131 | + await github.rest.issues.updateComment({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + comment_id: botComment.id, |
| 135 | + body: body |
| 136 | + }); |
| 137 | + } else { |
| 138 | + await github.rest.issues.createComment({ |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + issue_number: context.issue.number, |
| 142 | + body: body |
| 143 | + }); |
| 144 | + } |
0 commit comments