Last active
August 3, 2018 13:52
-
-
Save wiledal/73cede8f0e0c014b0a554007818693e2 to your computer and use it in GitHub Desktop.
Deployment Tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| const spawn = require('child_process').spawn | |
| const argv = process.argv | |
| const servers = { | |
| internal: [ | |
| { | |
| user: 'root', | |
| host: 'your internal setup', | |
| path: '/var/www/project', | |
| port: 1234 | |
| } | |
| ] | |
| } | |
| async function pause(time) { | |
| return new Promise((resolve) => { | |
| setTimeout(resolve, time) | |
| }) | |
| } | |
| console.log(` | |
| PROJECT DEPLOYER | |
| `) | |
| if (argv[2] == 'help' || !argv[2]) { | |
| console.log(` | |
| Available commands: | |
| deploy:<environment> [<tag/branch>] | |
| Deploy to an environment | |
| Examples: | |
| Pull latest: ./tools deploy:staging | |
| Deploy 1.0.4: ./tools deploy:production 1.0.4 | |
| ssh:prepare | |
| Prepare local machine for deployment. | |
| renew-ssl:<environment> | |
| Updates the SSL and distributes it amongst all the servers. | |
| If you have issues deploying, try running | |
| ./tools ssh:prepare | |
| This will set up your first-run ssh settings to allow SSH Agent Forwarding | |
| (https://developer.github.com/v3/guides/using-ssh-agent-forwarding/). | |
| Only run this command ONCE. It adds settings to your ~/.ssh/config. | |
| `.trim()) | |
| return | |
| } | |
| if (argv[2] == 'ssh:prepare') { | |
| var serversKeysArray = Object.keys(servers) | |
| var serversValuesArray = Object.values(servers) | |
| spawn(` | |
| ${serversKeysArray.map((k, i) => ` | |
| echo "Adding SSH Agent Forwarding for '${k}'..." && | |
| ${serversValuesArray[i].map(s => ` | |
| echo "Host ${s.host}\n ForwardAgent yes" >> ~/.ssh/config && | |
| `).join('')} | |
| `).join('')} | |
| echo 'done!'; | |
| `, { | |
| shell: true, | |
| stdio: 'inherit' | |
| }) | |
| } | |
| if (argv[2].indexOf('deploy') > -1) { | |
| var target = argv[2].split(':') | |
| // If we are just pulling | |
| var gitMethod = ` | |
| git fetch --all && | |
| git pull && | |
| ` | |
| // If a tag or branch is supplied | |
| if (argv[3]) { | |
| var tag = argv[3] | |
| gitMethod = ` | |
| git fetch --all && | |
| echo 'Resetting to tag ${tag}'; | |
| git reset --hard ${tag} && | |
| ` | |
| } | |
| if (servers[target[1]]) { | |
| (async() => { | |
| console.log(`Deploying to`) | |
| console.log() | |
| console.log(` ${target[1].toUpperCase()} `) | |
| console.log() | |
| console.log() | |
| console.log(`To cancel, press CTRL+C now.`) | |
| console.log() | |
| await pause(500) | |
| console.log(`Deploying in: 3 seconds`) | |
| await pause(1000) | |
| console.log(`Deploying in: 2 seconds`) | |
| await pause(1000) | |
| console.log(`Deploying in: 1 second!`) | |
| await pause(1000) | |
| console.log() | |
| console.log(`Continuing...`) | |
| console.log() | |
| spawn(` | |
| ssh-add; | |
| ${servers[target[1]].map(s => ` | |
| echo 'Connecting to ${s.host}...'; | |
| echo ''; | |
| ssh ${s.user}@${s.host} -p ${s.port ? s.port : 22}" | |
| cd ${s.path}; | |
| echo ''; | |
| echo 'Running git commands...'; | |
| echo ''; | |
| ${gitMethod} | |
| docker-compose exec web bash -c ' | |
| npm install && | |
| gulp && | |
| pm2 reload all; | |
| ' | |
| "; | |
| `).join('')} | |
| `, { | |
| shell: true, | |
| stdio: 'inherit' | |
| }) | |
| })() | |
| } else { | |
| console.log('No target supplied, or deploy target not found.') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment