Skip to content

Instantly share code, notes, and snippets.

@JonnyBurger
Created November 8, 2016 22:34
Show Gist options
  • Save JonnyBurger/208940ed9abcae569f1708d19c354cf0 to your computer and use it in GitHub Desktop.
Save JonnyBurger/208940ed9abcae569f1708d19c354cf0 to your computer and use it in GitHub Desktop.
Continuosly update alias for zeit.co's now

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
const 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment