-
Notifications
You must be signed in to change notification settings - Fork 518
284 lines (239 loc) · 11.2 KB
/
npm-app-release-staging.yml
File metadata and controls
284 lines (239 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
name: Release Staging (Codecane)
on:
pull_request:
branches: ['main']
# Ensure only one staging release runs at a time
concurrency:
group: staging-release
cancel-in-progress: false
permissions:
contents: write
jobs:
# First job: Check PR title and prepare staging release
prepare-and-commit-staging:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.title, '[codecane]')
outputs:
new_version: ${{ steps.bump_version.outputs.new_version }}
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.pull_request.head.sha }}
- name: Set up Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: '1.2.16'
# Cache dependencies for speed
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
node_modules
*/node_modules
key: ${{ runner.os }}-deps-${{ hashFiles('**/bun.lockb', '**/package.json') }}
restore-keys: |
${{ runner.os }}-deps-${{ hashFiles('**/bun.lockb') }}
${{ runner.os }}-deps-
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Calculate and update staging version
id: bump_version
run: |
cd npm-app/release-staging
# Use the current package.json version as base
CURRENT_VERSION=$(node -e "console.log(require('./package.json').version)")
echo "Current package.json version: $CURRENT_VERSION"
# Get latest beta version from npm to check if we need to increment
echo "Fetching latest beta version from npm..."
LATEST_BETA=$(npm view codecane@latest version 2>/dev/null || echo "")
if [ -z "$LATEST_BETA" ]; then
echo "No beta version found on npm, using current version as base"
NEW_VERSION="$CURRENT_VERSION-beta.1"
else
echo "Latest beta version: $LATEST_BETA"
# Extract base version and beta number from npm
NPM_BASE_VERSION=$(echo "$LATEST_BETA" | sed 's/-beta\..*$//')
BETA_NUM=$(echo "$LATEST_BETA" | sed 's/.*-beta\.//')
# Compare base versions
if [ "$CURRENT_VERSION" = "$NPM_BASE_VERSION" ]; then
# Same base version, increment beta number
NEW_BETA_NUM=$((BETA_NUM + 1))
NEW_VERSION="$CURRENT_VERSION-beta.$NEW_BETA_NUM"
else
# Different base version, start with beta.1
NEW_VERSION="$CURRENT_VERSION-beta.1"
fi
fi
echo "New staging version: $NEW_VERSION"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
# Update package.json with new version
node -e "
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
pkg.version = '$NEW_VERSION';
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
"
- name: Configure git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Commit staging release state
run: |
# Add all changes (current state + version bump)
git add -A
git commit -m "Staging Release v${{ steps.bump_version.outputs.new_version }} (codecane)
This commit captures the complete state being released for staging,
including any uncommitted changes and the version bump.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>"
- name: Create and push staging tag
run: |
# Show current commit info for debugging
echo "Current HEAD commit:"
git log -1 --format="%H %ci %s"
# Create tag on current HEAD (the commit we just made)
git tag "v${{ steps.bump_version.outputs.new_version }}"
git push origin "v${{ steps.bump_version.outputs.new_version }}"
echo "Tag created on commit:"
git show "v${{ steps.bump_version.outputs.new_version }}" --format="%H %ci %s" -s
- name: Upload updated package
uses: actions/upload-artifact@v4
with:
name: updated-staging-package
path: npm-app/release-staging/
build-staging-binaries:
needs: prepare-and-commit-staging
uses: ./.github/workflows/npm-app-release-build.yml
with:
binary-name: codecane
new-version: ${{ needs.prepare-and-commit-staging.outputs.new_version }}
artifact-name: updated-staging-package
checkout-ref: ${{ github.event.pull_request.head.sha }}
env-overrides: '{"NEXT_PUBLIC_CB_ENVIRONMENT": "prod", "NEXT_PUBLIC_CODEBUFF_BACKEND_URL": "backend-pr-221-we0m.onrender.com"}'
secrets: inherit
# Create GitHub prerelease with all binaries
create-staging-release:
needs: [prepare-and-commit-staging, build-staging-binaries]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Clean up old prereleases
run: |
# Calculate date one week ago
ONE_WEEK_AGO=$(date -d '7 days ago' -u +%Y-%m-%dT%H:%M:%SZ)
echo "Current date: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Cleaning up prereleases older than: $ONE_WEEK_AGO"
# Get all prereleases
echo "Fetching releases from GitHub API..."
RELEASES=$(curl -s -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \
"https://api.github.com/repos/CodebuffAI/codebuff-community/releases?per_page=100")
# Check if we got a valid response
if echo "$RELEASES" | jq -e . >/dev/null 2>&1; then
echo "Successfully fetched releases JSON"
# Count total releases and prereleases
TOTAL_RELEASES=$(echo "$RELEASES" | jq '. | length')
PRERELEASE_COUNT=$(echo "$RELEASES" | jq '[.[] | select(.prerelease == true)] | length')
echo "Total releases: $TOTAL_RELEASES"
echo "Total prereleases: $PRERELEASE_COUNT"
# Show some example release dates for debugging
echo "Sample release dates:"
echo "$RELEASES" | jq -r '.[] | select(.prerelease == true) | "\(.tag_name): \(.created_at)"' | head -5
# Filter and show old prereleases before deleting
OLD_PRERELEASES=$(echo "$RELEASES" | jq -r '.[] | select(.prerelease == true and .created_at < "'$ONE_WEEK_AGO'") | "\(.id):\(.tag_name):\(.created_at)"')
if [ -z "$OLD_PRERELEASES" ]; then
echo "No old prereleases found to delete"
else
echo "Found old prereleases to delete:"
echo "$OLD_PRERELEASES"
# Delete old prereleases
echo "$RELEASES" | jq -r '.[] | select(.prerelease == true and .created_at < "'$ONE_WEEK_AGO'") | .id' | while read release_id; do
if [ -n "$release_id" ]; then
echo "Deleting prerelease with ID: $release_id"
DELETE_RESPONSE=$(curl -s -w "HTTP Status: %{http_code}" -X DELETE \
-H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \
"https://api.github.com/repos/CodebuffAI/codebuff-community/releases/$release_id")
echo "Delete response: $DELETE_RESPONSE"
fi
done
fi
else
echo "Failed to fetch releases or invalid JSON response:"
echo "$RELEASES" | head -10
fi
echo "Cleanup completed"
- name: Download all binary artifacts
uses: actions/download-artifact@v4
with:
path: binaries/
- name: Download updated package
uses: actions/download-artifact@v4
with:
name: updated-staging-package
path: npm-app/release-staging/
- name: Create GitHub Prerelease
run: |
# Get current timestamp in ISO format
CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "Publishing release at: $CURRENT_TIME"
# Create release with current timestamp
curl -s -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
https://api.github.com/repos/CodebuffAI/codebuff-community/releases \
-d "{
\"tag_name\": \"v${{ needs.prepare-and-commit-staging.outputs.new_version }}\",
\"name\": \"Staging Release v${{ needs.prepare-and-commit-staging.outputs.new_version }} (Codecane)\",
\"body\": \"## Codecane v${{ needs.prepare-and-commit-staging.outputs.new_version }} (Staging)\n\n**⚠️ This is a staging/beta release for testing purposes.**\n\nBinary releases for all supported platforms.\n\n### Installation\n\`\`\`bash\nnpm install -g codecane\n\`\`\`\n\n### Platform Binaries\n- \`codecane-linux-x64.tar.gz\` - Linux x64\n- \`codecane-linux-arm64.tar.gz\` - Linux ARM64\n- \`codecane-darwin-x64.tar.gz\` - macOS Intel\n- \`codecane-darwin-arm64.tar.gz\` - macOS Apple Silicon\n- \`codecane-win32-x64.tar.gz\` - Windows x64\",
\"prerelease\": true,
\"published_at\": \"$CURRENT_TIME\"
}"
- name: Upload release assets
run: |
# Get the release ID
RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \
"https://api.github.com/repos/CodebuffAI/codebuff-community/releases/tags/v${{ needs.prepare-and-commit-staging.outputs.new_version }}" | \
jq -r '.id')
echo "Release ID: $RELEASE_ID"
# Upload all binary assets
for file in binaries/*/codecane-*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
echo "Uploading $filename..."
curl -s -X POST \
-H "Authorization: token ${{ secrets.CODEBUFF_GITHUB_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$file" \
"https://uploads.github.com/repos/CodebuffAI/codebuff-community/releases/$RELEASE_ID/assets?name=$filename"
fi
done
# Publish npm package as prerelease
publish-staging-npm:
needs: [prepare-and-commit-staging, create-staging-release]
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Download updated package
uses: actions/download-artifact@v4
with:
name: updated-staging-package
path: npm-app/release-staging/
- name: Set up Node.js for npm publishing
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- name: Publish staging to npm
run: |
cd npm-app/release-staging
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}