|
| 1 | +name: Release SQL Parser |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + id-token: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + name: Publish to NPM |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - run: corepack enable |
| 21 | + |
| 22 | + - uses: actions/setup-node@v4 |
| 23 | + with: |
| 24 | + node-version: "20" |
| 25 | + cache: "yarn" |
| 26 | + registry-url: "https://registry.npmjs.org" |
| 27 | + |
| 28 | + - name: Install dependencies |
| 29 | + run: yarn install --immutable |
| 30 | + |
| 31 | + - name: Build |
| 32 | + run: yarn build |
| 33 | + |
| 34 | + - name: Get package version |
| 35 | + id: get-version |
| 36 | + run: | |
| 37 | + VERSION=$(node -p "require('./package.json').version") |
| 38 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 39 | + echo "Publishing version: $VERSION" |
| 40 | +
|
| 41 | + - name: Extract changelog for version |
| 42 | + id: extract-changelog |
| 43 | + env: |
| 44 | + VERSION: ${{ steps.get-version.outputs.version }} |
| 45 | + run: | |
| 46 | + node << 'SCRIPT' |
| 47 | + const fs = require('fs'); |
| 48 | + const version = process.env.VERSION; |
| 49 | +
|
| 50 | + const content = fs.readFileSync('CHANGELOG.md', 'utf8'); |
| 51 | + const lines = content.split('\n'); |
| 52 | +
|
| 53 | + let capturing = false; |
| 54 | + let result = []; |
| 55 | +
|
| 56 | + for (const line of lines) { |
| 57 | + if (line.trim().startsWith('## ')) { |
| 58 | + if (capturing) { |
| 59 | + break; |
| 60 | + } |
| 61 | + if (line.trim() === `## ${version}` || line.trim().startsWith(`## ${version} `)) { |
| 62 | + capturing = true; |
| 63 | + continue; |
| 64 | + } |
| 65 | + } else if (capturing) { |
| 66 | + result.push(line); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + const changelog = result.join('\n').trim(); |
| 71 | +
|
| 72 | + if (!changelog) { |
| 73 | + console.log(`Warning: No changelog found for version ${version}`); |
| 74 | + } else { |
| 75 | + console.log('Extracted changelog:'); |
| 76 | + console.log(changelog); |
| 77 | + } |
| 78 | +
|
| 79 | + const crypto = require('crypto'); |
| 80 | + const delimiter = `EOF_${crypto.randomBytes(16).toString('hex')}`; |
| 81 | + const outputFile = process.env.GITHUB_OUTPUT; |
| 82 | + fs.appendFileSync(outputFile, `changelog<<${delimiter}\n${changelog}\n${delimiter}\n`); |
| 83 | + SCRIPT |
| 84 | +
|
| 85 | + - name: Check if version exists on npm |
| 86 | + id: check-version |
| 87 | + run: | |
| 88 | + VERSION=$(node -p "require('./package.json').version") |
| 89 | + if npm view @questdb/sql-parser@$VERSION version 2>/dev/null; then |
| 90 | + echo "Version $VERSION already exists on npm" |
| 91 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 92 | + else |
| 93 | + echo "Version $VERSION does not exist on npm" |
| 94 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 95 | + fi |
| 96 | +
|
| 97 | + - name: Publish to npm |
| 98 | + if: steps.check-version.outputs.exists == 'false' |
| 99 | + run: npm publish --provenance --access public |
| 100 | + |
| 101 | + - name: Create GitHub Release |
| 102 | + if: steps.check-version.outputs.exists == 'false' |
| 103 | + uses: actions/github-script@v7 |
| 104 | + env: |
| 105 | + VERSION: ${{ steps.get-version.outputs.version }} |
| 106 | + CHANGELOG: ${{ steps.extract-changelog.outputs.changelog }} |
| 107 | + with: |
| 108 | + script: | |
| 109 | + const version = process.env.VERSION; |
| 110 | + const changelog = process.env.CHANGELOG || ''; |
| 111 | +
|
| 112 | + const body = [ |
| 113 | + `## @questdb/sql-parser v${version}`, |
| 114 | + '', |
| 115 | + changelog || '_No changelog found for this release._', |
| 116 | + '', |
| 117 | + `[npm](https://www.npmjs.com/package/@questdb/sql-parser/v/${version})` |
| 118 | + ].join('\n'); |
| 119 | +
|
| 120 | + await github.rest.repos.createRelease({ |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + tag_name: `v${version}`, |
| 124 | + name: `v${version}`, |
| 125 | + body, |
| 126 | + draft: false, |
| 127 | + prerelease: false |
| 128 | + }); |
| 129 | +
|
| 130 | + console.log(`Created release v${version}`); |
0 commit comments