Skip to content

Instantly share code, notes, and snippets.

@mrstork
Last active December 4, 2024 08:37
Show Gist options
  • Save mrstork/dc852a8967a62a5e16a080ab6ad9f892 to your computer and use it in GitHub Desktop.
Save mrstork/dc852a8967a62a5e16a080ab6ad9f892 to your computer and use it in GitHub Desktop.
Update all your Netlify sites to use the new Noble build image
import { exec } from 'node:child_process'
import pLimit from 'p-limit'
const OLD_BUILD_IMAGE_NAME = 'focal'
const NEW_BUILD_IMAGE_NAME = 'noble'
// At most 20 update commands in parallel
const limitConcurrent = pLimit(20)
// Run a command with max buffer set to ~10 MB
const runCommand = (command) => new Promise((resolve, reject) => {
exec(command, { maxBuffer: 1000 * 1000 * 10 }, (err, stdout) => (err ? reject(err) : resolve(stdout)))
})
// Fetch an array of sites you have access to
const getSites = async () => {
const data = await runCommand(`netlify sites:list --json`);
return JSON.parse(data)
}
// Update the site to use the new image
const updateSite = async (site) => {
try {
await runCommand(`netlify api updateSite --data='{"site_id":"${site.id}", "body": {"build_image":"${NEW_BUILD_IMAGE_NAME}"}}'`)
console.log(`Updated build image on ${site.name} (${site.url})`);
} catch (error) {
console.error(`Error updating site ${site.name} (${site.url})`, error)
}
}
// Update Script
console.log('Fetching a list of your sites...')
const sites = await getSites();
const promises = sites
.filter(site => site.build_image === OLD_BUILD_IMAGE_NAME)
// .filter(site => site.account_name === [optional account name filter])
.map(site => ({
id: site.id,
name: site.name,
url: site.url,
account_name: site.account_name
}))
.map(site => limitConcurrent(() => updateSite(site)))
console.log('Updating your sites...')
if (promises.length > 0) {
await Promise.all(promises)
} else {
console.log('No sites to update.')
}
console.log('Complete!')
@mrstork
Copy link
Author

mrstork commented Nov 23, 2024

Requires netlify-cli and p-limit installed.
Execute the script with node netlify-noble-bulk-migrate.mjs 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment