1313 CARGO_TERM_COLOR : always
1414
1515jobs :
16- # Build native modules for all platforms
16+ # Check if native binaries are cached, build only if needed
17+ prepare-native :
18+ runs-on : ubuntu-latest
19+ outputs :
20+ native-hash : ${{ steps.hash.outputs.hash }}
21+ cache-hit : ${{ steps.cache-check.outputs.cache-hit }}
22+ steps :
23+ - uses : actions/checkout@v4
24+
25+ - name : Calculate native source hash
26+ id : hash
27+ run : |
28+ HASH=$(find native/src native/Cargo.toml native/Cargo.lock native/build.rs -type f -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | cut -d' ' -f1)
29+ echo "hash=$HASH" >> $GITHUB_OUTPUT
30+ echo "Native source hash: $HASH"
31+
32+ - name : Check cache for pre-built binaries
33+ id : cache-check
34+ uses : actions/cache/restore@v4
35+ with :
36+ path : native-binaries-flat
37+ key : native-${{ steps.hash.outputs.hash }}
38+ lookup-only : true
39+
40+ # Build native modules only if not cached
1741 build-native :
42+ needs : prepare-native
43+ if : needs.prepare-native.outputs.cache-hit != 'true'
1844 strategy :
1945 fail-fast : false
2046 matrix :
6894 path : native/*.node
6995 if-no-files-found : error
7096
71- # Publish platform-specific npm packages
97+ # Collect freshly built binaries and cache them
98+ cache-new-binaries :
99+ needs : [prepare-native, build-native]
100+ if : needs.prepare-native.outputs.cache-hit != 'true'
101+ runs-on : ubuntu-latest
102+ steps :
103+ - name : Download all artifacts
104+ uses : actions/download-artifact@v4
105+ with :
106+ path : native-binaries
107+
108+ - name : Flatten directory structure
109+ run : |
110+ mkdir -p native-binaries-flat
111+ find native-binaries -name "*.node" -exec cp {} native-binaries-flat/ \;
112+ ls -la native-binaries-flat/
113+
114+ - name : Cache native binaries for future releases
115+ uses : actions/cache/save@v4
116+ with :
117+ path : native-binaries-flat
118+ key : native-${{ needs.prepare-native.outputs.native-hash }}
119+
120+ # Publish platform-specific npm packages (only if native code changed)
72121 publish-native :
73- needs : build-native
122+ needs : [prepare-native, build-native, cache-new-binaries]
123+ # Only run if native code changed (cache miss means we built new binaries)
124+ if : always() && !failure() && !cancelled() && needs.prepare-native.outputs.cache-hit != 'true'
74125 runs-on : ubuntu-latest
75126 steps :
76127 - uses : actions/checkout@v4
@@ -85,12 +136,30 @@ jobs:
85136
86137 - run : pnpm install
87138
88- - name : Download all artifacts
139+ # Try to restore from cache first
140+ - name : Restore cached binaries
141+ id : cache-restore
142+ if : needs.prepare-native.outputs.cache-hit == 'true'
143+ uses : actions/cache/restore@v4
144+ with :
145+ path : native-binaries-flat
146+ key : native-${{ needs.prepare-native.outputs.native-hash }}
147+
148+ - name : Copy cached binaries to native/
149+ if : steps.cache-restore.outputs.cache-hit == 'true'
150+ run : |
151+ cp native-binaries-flat/*.node native/
152+ ls -la native/*.node
153+
154+ # Or download freshly built artifacts
155+ - name : Download build artifacts
156+ if : needs.prepare-native.outputs.cache-hit != 'true'
89157 uses : actions/download-artifact@v4
90158 with :
91159 path : native/artifacts
92160
93- - name : Move artifacts
161+ - name : Move artifacts to native/
162+ if : needs.prepare-native.outputs.cache-hit != 'true'
94163 run : |
95164 cd native
96165 for dir in artifacts/bindings-*; do
@@ -101,7 +170,7 @@ jobs:
101170 - name : Update native package version
102171 run : |
103172 VERSION="${{ github.ref_name }}"
104- VERSION="${VERSION#v}" # Remove 'v' prefix
173+ VERSION="${VERSION#v}"
105174 cd native
106175 node -e "
107176 const fs = require('fs');
@@ -114,21 +183,17 @@ jobs:
114183 run : |
115184 cd native
116185 pnpm napi create-npm-dirs
117- # List created dirs
118186 ls -la npm/
119187
120188 - name : Copy binaries to npm packages
121189 run : |
122190 cd native
123- # Copy each .node file to its corresponding npm package directory
124191 for node_file in *.node; do
125- # Extract platform from filename (e.g., graphql-lint.darwin-arm64.node -> darwin-arm64)
126192 platform=$(echo "$node_file" | sed 's/graphql-lint\.//' | sed 's/\.node//')
127193 target_dir="npm/$platform"
128194 if [ -d "$target_dir" ]; then
129195 cp "$node_file" "$target_dir/"
130196 echo "Copied $node_file to $target_dir/"
131- ls -la "$target_dir/"
132197 else
133198 echo "Warning: $target_dir not found for $node_file"
134199 fi
@@ -165,14 +230,18 @@ jobs:
165230 if [ -f "$pkg/package.json" ]; then
166231 echo "Publishing $pkg"
167232 cd "$pkg"
168- npm publish --provenance --access public --tag ${{ steps.npm-tag.outputs.tag }}
233+ npm publish --provenance --access public --tag ${{ steps.npm-tag.outputs.tag }} || true
169234 cd ../..
170235 fi
171236 done
237+ env :
238+ NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
172239
173- # Publish main package (after native packages)
240+ # Publish main package
174241 release :
175- needs : publish-native
242+ needs : [prepare-native, publish-native]
243+ # Always run (publish-native might be skipped if native code unchanged)
244+ if : always() && !failure() && !cancelled()
176245 runs-on : ubuntu-latest
177246 steps :
178247 - uses : actions/checkout@v4
@@ -189,26 +258,43 @@ jobs:
189258
190259 - run : pnpm install
191260
192- # Add optionalDependencies for platform-specific native packages
261+ - name : Determine native package version
262+ id : native-version
263+ run : |
264+ RELEASE_VERSION="${{ github.ref_name }}"
265+ RELEASE_VERSION="${RELEASE_VERSION#v}"
266+
267+ # If native code changed, use current release version
268+ # If not changed, get latest published version from npm
269+ if [ "${{ needs.prepare-native.outputs.cache-hit }}" == "true" ]; then
270+ echo "Native code unchanged, fetching latest published version..."
271+ NATIVE_VERSION=$(npm view nitro-graphql-linux-x64-gnu version 2>/dev/null || echo "$RELEASE_VERSION")
272+ echo "Using existing native version: $NATIVE_VERSION"
273+ else
274+ echo "Native code changed, using release version: $RELEASE_VERSION"
275+ NATIVE_VERSION="$RELEASE_VERSION"
276+ fi
277+
278+ echo "version=$NATIVE_VERSION" >> $GITHUB_OUTPUT
279+
193280 - name : Add native package dependencies
194281 run : |
195- VERSION="${{ github.ref_name }}"
196- VERSION="${VERSION#v}" # Remove 'v' prefix
282+ NATIVE_VERSION="${{ steps.native-version.outputs.version }}"
197283 node -e "
198284 const fs = require('fs');
199285 const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
200286 pkg.optionalDependencies = {
201- 'nitro-graphql-darwin-arm64': '$VERSION ',
202- 'nitro-graphql-darwin-x64': '$VERSION ',
203- 'nitro-graphql-linux-x64-gnu': '$VERSION ',
204- 'nitro-graphql-linux-arm64-gnu': '$VERSION ',
205- 'nitro-graphql-win32-x64-msvc': '$VERSION '
287+ 'nitro-graphql-darwin-arm64': '$NATIVE_VERSION ',
288+ 'nitro-graphql-darwin-x64': '$NATIVE_VERSION ',
289+ 'nitro-graphql-linux-x64-gnu': '$NATIVE_VERSION ',
290+ 'nitro-graphql-linux-arm64-gnu': '$NATIVE_VERSION ',
291+ 'nitro-graphql-win32-x64-msvc': '$NATIVE_VERSION '
206292 };
207293 fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
208294 "
209- cat package.json | grep -A 10 optionalDependencies
295+ echo "optionalDependencies set to version: $NATIVE_VERSION"
296+ cat package.json | grep -A 6 optionalDependencies
210297
211- # Skip native build - use JS-only build since native packages are separate
212298 - run : pnpm tsdown
213299
214300 - name : Determine npm tag
@@ -240,3 +326,16 @@ jobs:
240326 fi
241327 env :
242328 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
329+
330+ - name : Summary
331+ run : |
332+ echo "## Release Complete 🚀" >> $GITHUB_STEP_SUMMARY
333+ echo "" >> $GITHUB_STEP_SUMMARY
334+ echo "**Version:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
335+ echo "**Native Version:** ${{ steps.native-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
336+ if [ "${{ needs.prepare-native.outputs.cache-hit }}" == "true" ]; then
337+ echo "**Native Build:** ⏭️ Skipped (unchanged, using cached)" >> $GITHUB_STEP_SUMMARY
338+ else
339+ echo "**Native Build:** ✅ Built & Published" >> $GITHUB_STEP_SUMMARY
340+ fi
341+ echo "**Cache Key:** \`native-${{ needs.prepare-native.outputs.native-hash }}\`" >> $GITHUB_STEP_SUMMARY
0 commit comments