|
| 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: write |
| 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.pull_request.head.ref }} |
| 20 | + |
| 21 | + - name: Setup branch for semantic-release |
| 22 | + run: | |
| 23 | + # Explicitly checkout to the PR branch by name |
| 24 | + git checkout -B ${{ github.event.pull_request.head.ref }} |
| 25 | +
|
| 26 | + - name: Setup Node.js |
| 27 | + uses: actions/setup-node@v4 |
| 28 | + with: |
| 29 | + node-version: 'lts/*' |
| 30 | + |
| 31 | + - name: Setup preview config |
| 32 | + run: | |
| 33 | + # Update preview config with the PR branch name |
| 34 | + sed -i.bak "s/BRANCH_PLACEHOLDER/${{ github.event.pull_request.head.ref }}/g" .preview-releaserc.json |
| 35 | + rm .preview-releaserc.json.bak |
| 36 | +
|
| 37 | + - name: Run semantic-release (dry-run) |
| 38 | + id: semantic |
| 39 | + env: |
| 40 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + GIT_COMMITTER_NAME: "github-actions[bot]" |
| 42 | + GIT_COMMITTER_EMAIL: "github-actions[bot]@users.noreply.github.com" |
| 43 | + GIT_AUTHOR_NAME: "github-actions[bot]" |
| 44 | + GIT_AUTHOR_EMAIL: "github-actions[bot]@users.noreply.github.com" |
| 45 | + run: | |
| 46 | + # Unset GitHub Actions environment variables that interfere with semantic-release |
| 47 | + unset GITHUB_REF |
| 48 | + unset GITHUB_REF_NAME |
| 49 | + unset GITHUB_HEAD_REF |
| 50 | + unset GITHUB_BASE_REF |
| 51 | +
|
| 52 | + # Set them to what we want |
| 53 | + export GITHUB_REF="refs/heads/${{ github.event.pull_request.head.ref }}" |
| 54 | + export GITHUB_REF_NAME="${{ github.event.pull_request.head.ref }}" |
| 55 | +
|
| 56 | + # Temporarily use preview config |
| 57 | + mv .releaserc.json .releaserc.json.main |
| 58 | + cp .preview-releaserc.json .releaserc.json |
| 59 | +
|
| 60 | + # Run semantic-release with inline package installation (same as your local command) |
| 61 | + OUTPUT=$(npx --package semantic-release --package @semantic-release/exec --package conventional-changelog-conventionalcommits semantic-release 2>&1 || true) |
| 62 | + echo "$OUTPUT" |
| 63 | +
|
| 64 | + # Restore original config |
| 65 | + rm .releaserc.json |
| 66 | + mv .releaserc.json.main .releaserc.json |
| 67 | +
|
| 68 | + # Extract version information |
| 69 | + NEW_VERSION=$(echo "$OUTPUT" | grep -Eo "The next release version is [0-9]+\.[0-9]+\.[0-9]+" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+" || echo "") |
| 70 | + RELEASE_TYPE=$(echo "$OUTPUT" | grep -Eo "Analysis of [0-9]+ commits complete: [a-z]+ release" | grep -Eo "(major|minor|patch) release" | sed 's/ release//' || echo "") |
| 71 | +
|
| 72 | + # Extract release notes (everything after "Release note for version") |
| 73 | + RELEASE_NOTES=$(echo "$OUTPUT" | sed -n '/Release note for version/,$p' | tail -n +2 || echo "") |
| 74 | +
|
| 75 | + # Save to outputs |
| 76 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 77 | + echo "release_type=$RELEASE_TYPE" >> $GITHUB_OUTPUT |
| 78 | +
|
| 79 | + # Save release notes for comment |
| 80 | + echo "release_notes<<EOF" >> $GITHUB_OUTPUT |
| 81 | + echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT |
| 82 | + echo "EOF" >> $GITHUB_OUTPUT |
| 83 | +
|
| 84 | + - name: Display Preview |
| 85 | + run: | |
| 86 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 87 | + echo " RELEASE PREVIEW" |
| 88 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 89 | + echo "" |
| 90 | + if [ -n "${{ steps.semantic.outputs.new_version }}" ]; then |
| 91 | + echo "Version: v${{ steps.semantic.outputs.new_version }}" |
| 92 | + echo "Release Type: ${{ steps.semantic.outputs.release_type }}" |
| 93 | + echo "Status: Release will be published" |
| 94 | + else |
| 95 | + echo "Status: No release will be published" |
| 96 | + echo "Reason: No relevant changes detected" |
| 97 | + fi |
| 98 | + echo "" |
| 99 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 100 | +
|
| 101 | + - name: Comment on PR |
| 102 | + if: github.event_name == 'pull_request' |
| 103 | + uses: actions/github-script@v7 |
| 104 | + with: |
| 105 | + script: | |
| 106 | + const newVersion = '${{ steps.semantic.outputs.new_version }}'; |
| 107 | + const releaseType = '${{ steps.semantic.outputs.release_type }}'; |
| 108 | +
|
| 109 | + const releaseNotes = `${{ steps.semantic.outputs.release_notes }}`; |
| 110 | +
|
| 111 | + let body; |
| 112 | + if (newVersion) { |
| 113 | + body = `## Release Preview |
| 114 | +
|
| 115 | + **Version:** \`v${newVersion}\` |
| 116 | + **Release Type:** \`${releaseType}\` |
| 117 | + **Status:** Release will be published when merged to main |
| 118 | +
|
| 119 | + --- |
| 120 | +
|
| 121 | + ### Release Notes |
| 122 | +
|
| 123 | + ${releaseNotes} |
| 124 | +
|
| 125 | + --- |
| 126 | +
|
| 127 | + *This preview is generated by semantic-release dry-run mode*`; |
| 128 | + } |
| 129 | + else { |
| 130 | + body = `## Release Preview |
| 131 | +
|
| 132 | + **Status:** No release will be published |
| 133 | + **Reason:** No relevant changes detected |
| 134 | +
|
| 135 | + --- |
| 136 | +
|
| 137 | + <details> |
| 138 | + <summary> View full semantic-release log</summary> |
| 139 | +
|
| 140 | + \`\`\` |
| 141 | + ${{ steps.semantic.outputs.full_output }} |
| 142 | + \`\`\` |
| 143 | + </details> |
| 144 | +
|
| 145 | + --- |
| 146 | +
|
| 147 | + *This preview is generated by semantic-release dry-run mode*`; |
| 148 | + } |
| 149 | +
|
| 150 | + // Find existing comment |
| 151 | + const { data: comments } = await github.rest.issues.listComments({ |
| 152 | + owner: context.repo.owner, |
| 153 | + repo: context.repo.repo, |
| 154 | + issue_number: context.issue.number, |
| 155 | + }); |
| 156 | +
|
| 157 | + const botComment = comments.find(comment => |
| 158 | + comment.user.type === 'Bot' && |
| 159 | + comment.body.includes('Release Preview') |
| 160 | + ); |
| 161 | +
|
| 162 | + // Update or create comment |
| 163 | + if (botComment) { |
| 164 | + await github.rest.issues.updateComment({ |
| 165 | + owner: context.repo.owner, |
| 166 | + repo: context.repo.repo, |
| 167 | + comment_id: botComment.id, |
| 168 | + body: body |
| 169 | + }); |
| 170 | + } else { |
| 171 | + await github.rest.issues.createComment({ |
| 172 | + owner: context.repo.owner, |
| 173 | + repo: context.repo.repo, |
| 174 | + issue_number: context.issue.number, |
| 175 | + body: body |
| 176 | + }); |
| 177 | + } |
0 commit comments