If you're here, I probably don't need to espouse the benefits of using Travis.
To deploy to DigitalOcean, we'll use some bash scripts that are executed after the travis build.
Here's what will happen when you commit changes to your github repo.
cd my-project
touch .travis.yml # if you don't already have one
Here's a sample .travis.yml:
language: nodeCreate public and private SSH keysjs
nodejs:
- 6.9.1
notifications:
email:
onfailure: change
onsuccess: change
addons:
sshknownhosts:
- 123.45.56.78 # your droplet IP goes here
branches:
only:
- master
env:
global:
- REMOTEAPPDIR=/var/www/MYDOMAIN.COM/
- REMOTEUSER=deploy
- REMOTEHOST=123.45.56.78 # your droplet IP goes here
- PORT=8080
beforeinstall:
- npm install -g npm@^2
install:
- npm install --only=dev
beforescript:
- chmod 600 deploy && mv deploy ~/.ssh/idrsa
script:
- npm run build
after_success:
- "./scripts/deploy.sh"
On your local machine, open terminal and create keys.
ssh-keygen -f ~/.ssh/deploy
ssh-add -K ~/.ssh/deploy # add your SSH private key and store password in keychain
Resource: new ubuntu user.
ssh user@123.45.56.78
su - root # switch to root
adduser deploy # add a new user
usermod -aG sudo deploy
su - deploy ## switch to newly created user 'deploy'
cat ~/.ssh/deploy.pub | ssh user@123.45.56.78 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Locally, we'll do the following.
cd my-project
cp ~/.ssh/deploy > ./deploy ## copy private keys to project directory
travis encrypt-file deploy --add
echo 'deploy' >> .gitignore ## make sure your private key is .gitignored!
rm deploy ## even better
mkdir scripts
#!/usr/bin/env sh
set -x
tar -czf package.tgz build && \
scp package.tgz $REMOTE_USER@$REMOTE_HOST:$REMOTE_APP_DIR && \
ssh $REMOTE_USER@$REMOTE_HOST 'bash -s' < ./scripts/untar.sh
#!/usr/bin/env sh
set -x
export NODE_ENV=production
export NVM_BIN=$HOME/.nvm/versions/node/v6.9.0/bin
cd /var/www/YOUR-DOMAIN.com && \
tar zxvf package.tgz -C . && \
mv build/package.json . && \
npm install && \
npm run start