Run this script after deployment to update the alias.
got
is the only dev dependency- Change the file and include your domain, and app name
- Set an environment variable called
NOW_TOKEN
. You take the token from https://zeit.co/account#api-tokens
Run this script after deployment to update the alias.
got
is the only dev dependencyNOW_TOKEN
. You take the token from https://zeit.co/account#api-tokensconst got = require('got'); | |
const DOMAIN = 'example.com'; | |
const NOW_API_DOMAIN = 'https://api.zeit.co/now'; | |
const DEPLOYMENT_NAME = 'myapp'; | |
const OPTIONS = { | |
headers: { | |
Authorization: `Bearer ${process.env.NOW_TOKEN}` | |
}, | |
json: true | |
}; | |
console.log(`Getting old aliases for ${DOMAIN}`); | |
got(`${NOW_API_DOMAIN}/aliases`, OPTIONS) | |
.then(response => { | |
const {aliases} = response.body; | |
let aliasesForDomain = aliases.filter(a => a.alias === DOMAIN); | |
console.log(`Deleting old aliases (${aliasesForDomain.length})...`); | |
return Promise.all(aliasesForDomain.map(alias => { | |
return got(`${NOW_API_DOMAIN}/aliases/${alias.uid}`, Object.assign({}, OPTIONS, { | |
method: 'DELETE' | |
})); | |
})); | |
}) | |
.then(() => { | |
console.log(`Getting newest deployment with name ${DEPLOYMENT_NAME}...`); | |
return got(`${NOW_API_DOMAIN}/deployments`, OPTIONS); | |
}) | |
.then(response => { | |
const {deployments} = response.body; | |
const deploymentsForName = deployments.filter(d => d.name === DEPLOYMENT_NAME); | |
const latestDeployment = deploymentsForName.sort((a, b) => parseInt(b.created, 10) - parseInt(a.created, 10))[0]; | |
console.log('Adding alias to newest deployment...'); | |
return got(`${NOW_API_DOMAIN}/deployments/${latestDeployment.uid}/aliases`, Object.assign({}, OPTIONS, { | |
body: JSON.stringify({ | |
alias: DOMAIN | |
}), | |
headers: Object.assign({}, OPTIONS.headers, { | |
'Content-Type': 'application/json' | |
}) | |
})); | |
}) | |
.then(() => { | |
console.log('Added or replaced alias.'); | |
}) | |
.catch(err => { | |
console.error(err); | |
}); |