|
| 1 | +name: Reusable - Create Release from Changelog |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + description: Tag name to create a release for (e.g. v2.88.0 or v2.88.0-prerelease) |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + create-release: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout tag |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + ref: refs/tags/${{ inputs.tag }} |
| 22 | + |
| 23 | + - name: Create release from CHANGELOG |
| 24 | + uses: actions/github-script@v7 |
| 25 | + env: |
| 26 | + TAG: ${{ inputs.tag }} |
| 27 | + with: |
| 28 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + script: | |
| 30 | + const { readFile } = require('fs/promises'); |
| 31 | +
|
| 32 | + const tag = process.env.TAG; |
| 33 | + core.info(`Creating release for tag: ${tag}`); |
| 34 | +
|
| 35 | + if (!tag || !tag.startsWith('v2.')) { |
| 36 | + core.setFailed('Invalid tag name. Tag name must start with "v2."'); |
| 37 | + return; |
| 38 | + } |
| 39 | +
|
| 40 | + // Read CHANGELOG.md and extract the latest section (first single '#' header) |
| 41 | + const changelog = await readFile('CHANGELOG.md', 'utf8'); |
| 42 | + const headerMatch = changelog.match(/^# .+$/m); |
| 43 | + if (!headerMatch) { |
| 44 | + core.setFailed('Could not find a top-level # header in CHANGELOG.md'); |
| 45 | + return; |
| 46 | + } |
| 47 | + const startIdx = changelog.indexOf(headerMatch[0]); |
| 48 | + let endIdx = changelog.indexOf('\n# ', startIdx + headerMatch[0].length); |
| 49 | + if (endIdx === -1) { |
| 50 | + endIdx = changelog.length; |
| 51 | + } |
| 52 | + const releaseNotes = changelog.substring(startIdx, endIdx).trim(); |
| 53 | +
|
| 54 | + const isPrerelease = tag.includes('-prerelease'); |
| 55 | + core.info(`Prerelease: ${isPrerelease}`); |
| 56 | +
|
| 57 | + try { |
| 58 | + const response = await github.rest.repos.createRelease({ |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + tag_name: tag, |
| 62 | + name: tag, |
| 63 | + body: releaseNotes, |
| 64 | + prerelease: isPrerelease, |
| 65 | + }); |
| 66 | + core.info(`Release created: ${response.data.html_url}`); |
| 67 | + } catch (err) { |
| 68 | + if (err && err.status === 422 && err.message && String(err.message).includes('already_exists')) { |
| 69 | + core.warning(`Release for tag '${tag}' already exists.`); |
| 70 | + } else { |
| 71 | + core.setFailed(`Error creating release: ${err?.message ?? err}`); |
| 72 | + } |
| 73 | + } |
0 commit comments