|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + |
| 8 | +jobs: |
| 9 | + create-release: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + concurrency: |
| 12 | + group: release-${{ github.ref }} |
| 13 | + cancel-in-progress: false |
| 14 | + permissions: |
| 15 | + actions: read |
| 16 | + contents: write |
| 17 | + steps: |
| 18 | + - name: Wait for CI workflow success |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const owner = context.repo.owner; |
| 23 | + const repo = context.repo.repo; |
| 24 | + const targetSha = context.sha; |
| 25 | + const targetRef = context.ref; |
| 26 | + const targetTag = targetRef.replace('refs/tags/', ''); |
| 27 | + const workflowId = 'ci.yml'; |
| 28 | + const pollMs = 20000; |
| 29 | + const timeoutMs = 45 * 60 * 1000; |
| 30 | + const startedAt = Date.now(); |
| 31 | + const failingConclusions = new Set(['failure', 'cancelled', 'timed_out', 'action_required']); |
| 32 | +
|
| 33 | + const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 34 | +
|
| 35 | + while (true) { |
| 36 | + const { data } = await github.rest.actions.listWorkflowRuns({ |
| 37 | + owner, |
| 38 | + repo, |
| 39 | + workflow_id: workflowId, |
| 40 | + event: 'push', |
| 41 | + head_sha: targetSha, |
| 42 | + per_page: 100, |
| 43 | + }); |
| 44 | +
|
| 45 | + const matchingRuns = data.workflow_runs |
| 46 | + .filter((run) => run.ref === targetRef) |
| 47 | + .sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); |
| 48 | +
|
| 49 | + if (matchingRuns.length > 0) { |
| 50 | + const run = matchingRuns[0]; |
| 51 | + if (run.status === 'completed') { |
| 52 | + if (run.conclusion === 'success') { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (failingConclusions.has(run.conclusion)) { |
| 56 | + core.setFailed(`CI did not succeed for ${targetTag}. Conclusion: ${run.conclusion}.`); |
| 57 | + return; |
| 58 | + } |
| 59 | + core.setFailed(`CI completed without success for ${targetTag}. Conclusion: ${run.conclusion ?? 'unknown'}.`); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | +
|
| 64 | + if (Date.now() - startedAt >= timeoutMs) { |
| 65 | + core.setFailed(`Timed out after ${timeoutMs / 60000} minutes waiting for successful CI on ${targetTag}.`); |
| 66 | + return; |
| 67 | + } |
| 68 | +
|
| 69 | + await sleep(pollMs); |
| 70 | + } |
| 71 | +
|
| 72 | + - name: Checkout |
| 73 | + uses: actions/checkout@v4 |
| 74 | + |
| 75 | + - name: Extract changelog entry |
| 76 | + id: changelog |
| 77 | + uses: actions/github-script@v7 |
| 78 | + with: |
| 79 | + result-encoding: string |
| 80 | + script: | |
| 81 | + const tag = process.env.GITHUB_REF_NAME || ''; |
| 82 | + const version = tag.startsWith('v') ? tag.slice(1) : tag; |
| 83 | + const fs = require('node:fs'); |
| 84 | + const changelog = fs.readFileSync('CHANGELOG.md', 'utf8'); |
| 85 | + const heading = `## [${version}]`; |
| 86 | + const start = changelog.indexOf(heading); |
| 87 | + let entry; |
| 88 | + if (start === -1) { |
| 89 | + entry = `No changelog entry found for version ${version}.`; |
| 90 | + } else { |
| 91 | + const afterHeading = start + heading.length; |
| 92 | + const nextHeader = changelog.indexOf('\n## [', afterHeading); |
| 93 | + const sliceEnd = nextHeader === -1 ? changelog.length : nextHeader; |
| 94 | + entry = changelog.slice(start, sliceEnd).trim(); |
| 95 | + } |
| 96 | + return entry; |
| 97 | +
|
| 98 | + - name: Create GitHub Release |
| 99 | + uses: softprops/action-gh-release@v2 |
| 100 | + with: |
| 101 | + draft: false |
| 102 | + prerelease: false |
| 103 | + generate_release_notes: false |
| 104 | + body: ${{ steps.changelog.outputs.result }} |
| 105 | + env: |
| 106 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments