Skip to content

Commit a0df3c5

Browse files
authored
Merge pull request #479 from Kaggle/publish-kaggle-environments-core-to-npm
Publish @kaggle-environments/core to npm based on the version in the pythion TOML
2 parents a476af5 + 0aa8ff9 commit a0df3c5

File tree

4 files changed

+123
-6
lines changed

4 files changed

+123
-6
lines changed

cicd/cloudbuild.yaml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
steps:
2-
# Step 1: Install dependencies and build all static assets.
2+
# Step 1: Install dependencies, sync versions, and build all static assets.
33
# This runs our 'build-ci' script which builds all visualizers and
44
# collects the artifacts into a single './build' directory.
5-
# If this gets much more complicated, we should make a bash or docker file.
65
- name: "gcr.io/cloud-builders/npm"
76
id: "build-all-visualizers"
87
entrypoint: "bash"
@@ -13,9 +12,19 @@ steps:
1312
apt-get update && apt-get install -y jq
1413
npm install -g pnpm
1514
pnpm install
15+
node web/scripts/sync-version.js
1616
pnpm build-ci
1717
18-
# Step 2: Deploy the collected artifacts to Google Cloud Storage.
18+
# Step 2: Publish the core package to NPM.
19+
# This runs after the build to ensure the package is up-to-date.
20+
- name: "gcr.io/cloud-builders/npm"
21+
id: "publish-to-npm"
22+
entrypoint: "bash"
23+
args: ["web/scripts/publish-core.sh"]
24+
waitFor: ["build-all-visualizers"]
25+
secretEnv: ["NPM_TOKEN"]
26+
27+
# Step 3: Deploy the collected artifacts to Google Cloud Storage.
1928
# 'rsync -d' syncs the directory and deletes any files in the destination
2029
# that are not in the source, We probably want to change this to keep versions
2130
# once we figure out a decent versioning scheme.
@@ -29,9 +38,14 @@ steps:
2938
- "-r"
3039
- "./build"
3140
- "gs://$_BUCKET_NAME/episode-visualizers"
32-
waitFor: ["build-all-visualizers"]
41+
waitFor: ["publish-to-npm"]
3342

3443
substitutions:
3544
_BUCKET_NAME: "kaggle-static"
3645

37-
tags: ["kaggle-environments", "visualizer-deployment"]
46+
availableSecrets:
47+
secretManager:
48+
- versionName: projects/kaggle-cicd/secrets/npm-token-kaggle-environments/versions/latest
49+
env: "NPM_TOKEN"
50+
51+
tags: ["kaggle-environments", "visualizer-deployment", "npm-publish"]

web/core/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "@kaggle-environments/core",
3-
"private": true,
43
"version": "0.0.0",
4+
"publishConfig": {
5+
"access": "public"
6+
},
57
"type": "module",
68
"exports": {
79
".": {

web/scripts/publish-core.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "Installing dependencies..."
5+
npm install -g pnpm
6+
pnpm install
7+
8+
echo "Building core library..."
9+
pnpm --filter @kaggle-environments/core build
10+
11+
PACKAGE_NAME="@kaggle-environments/core"
12+
CORE_PACKAGE_JSON_PATH="web/core/package.json"
13+
VERSION_FROM_PACKAGE_JSON=$(grep -E '"version":\s*".*"' "$CORE_PACKAGE_JSON_PATH" | sed -E 's/.*"version":\s*"(.*)".*/\1/')
14+
15+
if [ -z "$VERSION_FROM_PACKAGE_JSON" ]; then
16+
echo "❌ Could not find version in $CORE_PACKAGE_JSON_PATH. Exiting."
17+
exit 1
18+
fi
19+
20+
echo "Found version $VERSION_FROM_PACKAGE_JSON. Proceeding to publish/verify loop."
21+
22+
23+
MAX_RETRIES=3
24+
RETRY_DELAY_SECONDS=20
25+
SUCCESS=false
26+
27+
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > web/core/.npmrc
28+
29+
set +e
30+
31+
for i in $(seq 1 $MAX_RETRIES)
32+
do
33+
echo "--- Attempt $i/$MAX_RETRIES ---"
34+
35+
# 1. Check if the version now exists on the registry.
36+
echo "Checking NPM registry for $PACKAGE_NAME@$VERSION_FROM_PACKAGE_JSON..."
37+
if npm view "$PACKAGE_NAME@$VERSION_FROM_PACKAGE_JSON" version > /dev/null; then
38+
echo "✅ Version found on NPM - Skipping Publish"
39+
SUCCESS=true
40+
break
41+
fi
42+
43+
# 2. If not found, attempt to publish.
44+
echo "Version not found. Attempting to publish..."
45+
if pnpm --filter @kaggle-environments/core publish --no-git-checks; then
46+
echo "Publish command succeeded. Will verify on the next attempt after a delay."
47+
else
48+
echo "Publish command failed."
49+
fi
50+
51+
# 3. If this wasn't the last attempt, wait before retrying.
52+
if [ "$i" -lt "$MAX_RETRIES" ]; then
53+
echo "Waiting ${RETRY_DELAY_SECONDS}s before next attempt..."
54+
sleep $RETRY_DELAY_SECONDS
55+
fi
56+
done
57+
58+
# Cleanup the .npmrc file regardless of outcome
59+
rm web/core/.npmrc
60+
61+
# Re-enable failing on error
62+
set -e
63+
64+
if [ "$SUCCESS" != "true" ]; then
65+
echo "❌ Failed to publish or verify package after $MAX_RETRIES attempts. Aborting."
66+
exit 1
67+
fi
68+
69+
echo "🚀 Process complete."

web/scripts/sync-version.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const pyprojectPath = path.resolve(__dirname, '../../pyproject.toml');
5+
const packageJsonPath = path.resolve(__dirname, '../core/package.json');
6+
7+
try {
8+
// Read pyproject.toml
9+
const pyprojectContent = fs.readFileSync(pyprojectPath, 'utf8');
10+
const versionMatch = pyprojectContent.match(/^version\s*=\s*"(.+?)"/m);
11+
12+
if (!versionMatch) {
13+
throw new Error('Could not find version in pyproject.toml');
14+
}
15+
const version = versionMatch[1];
16+
console.log(`Found version ${version} in pyproject.toml`);
17+
18+
// Read package.json
19+
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
20+
const packageJson = JSON.parse(packageJsonContent);
21+
22+
// Update version
23+
packageJson.version = version;
24+
25+
// Write back to package.json
26+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
27+
console.log(`Successfully updated version in web/core/package.json to ${version}`);
28+
29+
} catch (error) {
30+
console.error('Error syncing version:', error.message);
31+
process.exit(1);
32+
}

0 commit comments

Comments
 (0)