Skip to content

Commit d98ba57

Browse files
committed
add release script for sdk
1 parent 0aa14e7 commit d98ba57

File tree

7 files changed

+272
-31
lines changed

7 files changed

+272
-31
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Build and Publish SDK
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
new-version:
7+
required: true
8+
type: string
9+
description: 'Version to build'
10+
artifact-name:
11+
required: true
12+
type: string
13+
description: 'Name for the artifact (updated-sdk-package)'
14+
checkout-ref:
15+
required: false
16+
type: string
17+
description: 'Git ref to checkout'
18+
default: ''
19+
20+
jobs:
21+
build-and-publish:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
25+
id-token: write
26+
steps:
27+
- uses: actions/checkout@v4
28+
with:
29+
ref: ${{ inputs.checkout-ref || github.sha }}
30+
31+
- uses: ./.github/actions/setup-project
32+
33+
- name: Download updated package
34+
uses: actions/download-artifact@v4
35+
with:
36+
name: ${{ inputs.artifact-name }}
37+
path: sdk/
38+
39+
- name: Build SDK
40+
run: |
41+
cd sdk
42+
bun run build:verify
43+
env:
44+
BUILD_VERSION: ${{ inputs.new-version }}
45+
46+
- name: Set up Node.js for npm publishing
47+
uses: actions/setup-node@v4
48+
with:
49+
node-version: 20
50+
registry-url: https://registry.npmjs.org/
51+
52+
- name: Publish to npm
53+
run: |
54+
cd sdk
55+
bun run publish-sdk
56+
env:
57+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: Release Production (SDK)
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version_type:
7+
description: 'Version bump type'
8+
required: true
9+
default: 'patch'
10+
type: choice
11+
options:
12+
- patch
13+
- minor
14+
- major
15+
16+
permissions:
17+
contents: write
18+
19+
jobs:
20+
# First job: Prepare production release
21+
prepare-and-commit-prod:
22+
runs-on: ubuntu-latest
23+
outputs:
24+
new_version: ${{ steps.bump_version.outputs.new_version }}
25+
steps:
26+
- uses: actions/checkout@v4
27+
with:
28+
token: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- uses: ./.github/actions/setup-project
31+
32+
- name: Calculate and update production version
33+
id: bump_version
34+
run: |
35+
cd sdk
36+
37+
# Get current version and bump it
38+
CURRENT_VERSION=$(bun -e "console.log(require('./package.json').version)")
39+
echo "Current version: $CURRENT_VERSION"
40+
41+
# Bump version based on input
42+
npm version ${{ inputs.version_type }} --no-git-tag-version
43+
NEW_VERSION=$(bun -e "console.log(require('./package.json').version)")
44+
45+
echo "New production version: $NEW_VERSION"
46+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
47+
48+
- name: Configure git
49+
run: |
50+
git config --global user.name "github-actions[bot]"
51+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
52+
53+
- name: Commit and push version bump
54+
run: |
55+
git stash
56+
git pull --rebase origin main
57+
git stash pop
58+
git add sdk/package.json
59+
git commit -m "Bump SDK version to ${{ steps.bump_version.outputs.new_version }}"
60+
git push
61+
62+
- name: Create and push production tag
63+
run: |
64+
git tag "sdk-v${{ steps.bump_version.outputs.new_version }}"
65+
git push origin "sdk-v${{ steps.bump_version.outputs.new_version }}"
66+
67+
- name: Upload updated package
68+
uses: actions/upload-artifact@v4
69+
with:
70+
name: updated-sdk-package
71+
path: sdk/
72+
73+
build-and-publish-sdk:
74+
needs: prepare-and-commit-prod
75+
uses: ./.github/workflows/sdk-release-build.yml
76+
with:
77+
new-version: ${{ needs.prepare-and-commit-prod.outputs.new_version }}
78+
artifact-name: updated-sdk-package
79+
checkout-ref: ${{ github.sha }}
80+
secrets: inherit

sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"prepare-dist": "bun run scripts/publish.ts --dry-run",
3636
"publish-sdk": "bun run scripts/publish.ts --public",
3737
"publish-dry-run": "bun run build:verify && bun run scripts/publish.ts --dry-run",
38+
"release": "bun run scripts/release.js",
3839
"fetch-ripgrep": "bun scripts/fetch-ripgrep.ts",
3940
"prepack": "bun run build",
4041
"typecheck": "tsc --noEmit -p .",

sdk/scripts/build.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,6 @@ async function build() {
5252
'events',
5353
]
5454

55-
// Environment variable overrides
56-
const envOverrides: Record<string, string> = {
57-
OPEN_ROUTER_API_KEY: '',
58-
RELACE_API_KEY: '',
59-
LINKUP_API_KEY: '',
60-
GOOGLE_CLOUD_PROJECT_ID: '',
61-
PORT: '0',
62-
DATABASE_URL: '',
63-
CODEBUFF_GITHUB_ID: '',
64-
CODEBUFF_GITHUB_SECRET: '',
65-
NEXTAUTH_SECRET: '',
66-
STRIPE_SECRET_KEY: '',
67-
STRIPE_WEBHOOK_SECRET_KEY: '',
68-
STRIPE_USAGE_PRICE_ID: '',
69-
STRIPE_TEAM_FEE_PRICE_ID: '',
70-
LOOPS_API_KEY: '',
71-
DISCORD_PUBLIC_KEY: '',
72-
DISCORD_BOT_TOKEN: '',
73-
DISCORD_APPLICATION_ID: '',
74-
API_KEY_ENCRYPTION_SECRET: '',
75-
}
76-
const envOverridesDefine: Parameters<typeof Bun.build>[0]['define'] =
77-
Object.fromEntries(
78-
Object.entries(envOverrides).map(([key, value]) => [
79-
`process.env.${key}`,
80-
value,
81-
]),
82-
)
83-
8455
console.log('📦 Building ESM format...')
8556
await Bun.build({
8657
entrypoints: ['src/index.ts'],
@@ -91,7 +62,6 @@ async function build() {
9162
sourcemap: 'linked',
9263
external,
9364
naming: '[dir]/index.mjs',
94-
define: envOverridesDefine,
9565
env: 'NEXT_PUBLIC_*',
9666
loader: {
9767
'.scm': 'text',
@@ -110,7 +80,6 @@ async function build() {
11080
external,
11181
naming: '[dir]/index.cjs',
11282
define: {
113-
...envOverridesDefine,
11483
'import.meta.url': 'undefined',
11584
'import.meta': 'undefined',
11685
},

sdk/scripts/release.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env node
2+
3+
const { execSync } = require('child_process')
4+
5+
// Parse command line arguments
6+
const args = process.argv.slice(2)
7+
const versionType = args[0] || 'patch' // patch, minor, major, or specific version like 1.2.3
8+
9+
function log(message) {
10+
console.log(`${message}`)
11+
}
12+
13+
function error(message) {
14+
console.error(`❌ ${message}`)
15+
process.exit(1)
16+
}
17+
18+
function formatTimestamp() {
19+
const now = new Date()
20+
const options = {
21+
month: 'long',
22+
day: 'numeric',
23+
hour: '2-digit',
24+
minute: '2-digit',
25+
second: '2-digit',
26+
timeZoneName: 'short',
27+
}
28+
return now.toLocaleDateString('en-US', options)
29+
}
30+
31+
function checkGitHubToken() {
32+
const token = process.env.CODEBUFF_GITHUB_TOKEN
33+
if (!token) {
34+
error(
35+
'CODEBUFF_GITHUB_TOKEN environment variable is required but not set.\n' +
36+
'Please set it with your GitHub personal access token or use the infisical setup.'
37+
)
38+
}
39+
40+
// Set GITHUB_TOKEN for compatibility with existing curl commands
41+
process.env.GITHUB_TOKEN = token
42+
return token
43+
}
44+
45+
async function triggerWorkflow(versionType) {
46+
if (!process.env.GITHUB_TOKEN) {
47+
error('GITHUB_TOKEN environment variable is required but not set')
48+
}
49+
50+
try {
51+
// Use workflow filename instead of ID
52+
const triggerCmd = `curl -s -w "HTTP Status: %{http_code}" -X POST \
53+
-H "Accept: application/vnd.github.v3+json" \
54+
-H "Authorization: token ${process.env.GITHUB_TOKEN}" \
55+
-H "Content-Type: application/json" \
56+
https://api.github.com/repos/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml/dispatches \
57+
-d '{"ref":"main","inputs":{"version_type":"${versionType}"}}'`
58+
59+
const response = execSync(triggerCmd, { encoding: 'utf8' })
60+
61+
// Check if response contains error message
62+
if (response.includes('workflow_dispatch')) {
63+
log(`⚠️ Workflow dispatch failed: ${response}`)
64+
log('The workflow may need to be updated on GitHub. Continuing anyway...')
65+
log(
66+
'Please manually trigger the workflow at: https://github.com/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml',
67+
)
68+
} else {
69+
log('🎉 SDK release workflow triggered!')
70+
}
71+
} catch (err) {
72+
log(`⚠️ Failed to trigger workflow automatically: ${err.message}`)
73+
log(
74+
'You may need to trigger it manually at: https://github.com/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml',
75+
)
76+
}
77+
}
78+
79+
async function main() {
80+
log('🚀 Initiating SDK release...')
81+
log(`Date: ${formatTimestamp()}`)
82+
83+
// Check for local GitHub token
84+
checkGitHubToken()
85+
log('✅ Using local CODEBUFF_GITHUB_TOKEN')
86+
87+
log(`Version bump type: ${versionType}`)
88+
89+
// Trigger the workflow
90+
await triggerWorkflow(versionType)
91+
92+
log('')
93+
log('Monitor progress at: https://github.com/CodebuffAI/codebuff/actions')
94+
}
95+
96+
main().catch((err) => {
97+
error(`SDK release failed: ${err.message}`)
98+
})

sdk/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { setEnv } from './set-env'
2+
setEnv()
3+
14
export type * from '../../common/src/types/json'
25
export type * from '../../common/src/types/messages/codebuff-message'
36
export type * from '../../common/src/types/messages/data-content'

sdk/src/set-env.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const overrides = {
2+
OPEN_ROUTER_API_KEY: '',
3+
RELACE_API_KEY: '',
4+
LINKUP_API_KEY: '',
5+
GOOGLE_CLOUD_PROJECT_ID: '',
6+
PORT: '0',
7+
DATABASE_URL: '',
8+
CODEBUFF_GITHUB_ID: '',
9+
CODEBUFF_GITHUB_SECRET: '',
10+
NEXTAUTH_SECRET: '',
11+
STRIPE_SECRET_KEY: '',
12+
STRIPE_WEBHOOK_SECRET_KEY: '',
13+
STRIPE_USAGE_PRICE_ID: '',
14+
STRIPE_TEAM_FEE_PRICE_ID: '',
15+
LOOPS_API_KEY: '',
16+
DISCORD_PUBLIC_KEY: '',
17+
DISCORD_BOT_TOKEN: '',
18+
DISCORD_APPLICATION_ID: '',
19+
API_KEY_ENCRYPTION_SECRET: '',
20+
}
21+
22+
export function setEnv(): void {
23+
if (
24+
process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev' ||
25+
process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'test'
26+
) {
27+
return
28+
}
29+
30+
for (const [key, value] of Object.entries(overrides)) {
31+
process.env[key] = value
32+
}
33+
}

0 commit comments

Comments
 (0)