Skip to content

Instantly share code, notes, and snippets.

@identor
Last active February 3, 2017 04:34
Show Gist options
  • Save identor/e4ece3a6b326e7fc0b32 to your computer and use it in GitHub Desktop.
Save identor/e4ece3a6b326e7fc0b32 to your computer and use it in GitHub Desktop.
Deployment Test with simple auto deployment server update by git push.
#!/bin/sh
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/opt/node/bin
mkdir -p $HOME/opt/katydid
git --work-tree=$HOME/opt/katydid --git-dir=$HOME/katydid.git checkout -f
npm i --prefix $HOME/opt/katydid --production
pm2 stop katydid
pm2 start katydid

Deployment Summary

Deployment summary, all commands are tested in debian 8.1 x64. The new user to be created will be deploy under the host yourhost-here.

Create new user for deployment

Access the root user's shell. Note that the adduser command will prompt for a password, enter a new password and remember it.

apt-get install sudo -y
adduser deploy
gpasswd -a deploy sudo

Access the new user and secure it

Access the new user's shell and initiate needed dirs and files. On ssh-kegen press enter three times to use the default values.

su - deploy
mkdir -p opt .tmp
ssh-keygen

Install new key for the user

Access your local machine and issue the following command:

cat ~/.ssh/id_rsa.pub | ssh deploy@yourhost-here 'cat >> ~/.ssh/authorized_keys'

Once the command deploy's password should be entered (for the last time).

Secure the remote host (yourhost-here)

Disable password logins (ssh). Access the remote host and issue the following command/s.

[sudo ]vi /etc/ssh/sshd_config

Replace this content

 
# Change to no to disable tunnelled clear text passwords 
#PasswordAuthentication yes 

with this:


# Change to no to disable tunnelled clear text passwords 
PasswordAuthentication no

then restart the ssh daemon

[sudo ]service ssh restart

Install dependencies

Access the remote host using the deployment user. Install git and install nodejs from linux tarfile, obtain the latest node binaries throuh node's website, in these example we will be installing nodejs v4.1.0. To install needed dependencies issue the following commands:

ssh deploy@yourhost-here
sudo apt-get install git -y
wget https://nodejs.org/download/release/v4.1.0/node-v4.1.0-linux-x64.tar.gz
tar xzf node-v4.1.0-linux-x64.tar.gz
mv node-v4.1.0-linux-x64 ~/opt/node
rm node-v4.1.0-linux-x64.tar.gz

Add node to the user's PATH

To add node's binaries to deploy's PATH environment variable. Edit ~/.profile and add the following:

# set PATH so it includes node's private bin if it exists
if [ -d "$HOME/opt/node/bin" ] ; then
    PATH="$HOME/opt/node/bin:$PATH"
fi

Once you've added node's binaries to deploy's PATH environment variable either exit the current shell and open it again, or issue source ~/.profile.

Install pm2

PM2 is a node process manager, google pm2 for more information.

npm i -g pm2

Deploy a sample application

In this guide we will be installing indexzero's http-server. Issue the following command/s to clone the repository into a bare git repo (to enable githooks this is required).

git clone --bare https://github.com/indexzero/http-server.git

Start the application

To start the application we need to checkout the source files into ~/opt/http-server, and then start the application using pm2. To do these issue the following commands:

mkdir -p $HOME/opt/http-server
git --work-tree=$HOME/opt/http-server --git-dir=$HOME/http-server.git checkout -f
npm i --prefix $HOME/opt/http-server --production
cd ~/opt/http-server
pm2 start bin/http-server

Test if the application is running by accessing the server through a browser, it should be viewable in http://yourhost-here:8080/.

Add git hooks for post receive to auto update the application

To enable auto updates by pushing to the server add a post-receive hook. Create the new file by editing ~/http-server.git/hooks/post-receive add add these:

#!/bin/sh
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/opt/node/bin
mkdir -p $HOME/opt/http-server
git --work-tree=$HOME/opt/http-server --git-dir=$HOME/http-server.git checkout -f
npm i --prefix $HOME/opt/http-server --production
pm2 stop http-server
pm2 start http-server

Add execute flag to the script

chmod u+x ~/http-server.git/hooks/post-receive

Check if auto update through push is enabled

To check if auto update is enabled we will be cloning the source in your local machine. First you need to open a shell where deploy@yourhost-here is accessible without password authentication (key-based login).

git clone deploy@yourhost-here:http-server.git
cd http-server.git

Or add a remote to your existing http-server repo

git remote add http-server deploy@yourhost-here:http-server.git

Edit the index file vi public/index.html change the following content:

<html>
  <head>
    <title>node.js http server</title>
  </head>
  <body>

    <h1>Serving up static files like they were turtles strapped to rockets.</h1>

To this:

<html>
  <head>
    <title>BlaBla | This is just a test</title>
  </head>
  <body>

    <h1>All right!!!</h1>

Then commit the changes and push it to the server

git add .
git commit -m 'Test commit'
git push origin master

This will effectively push the commit to the server and execute the post-receive script. An output similar to this would appear:

Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 360 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: hooks/post-receive: 1: hooks/post-receive: !/bin/sh: not found
remote: npm WARN package.json [email protected] No license field.
remote: [PM2] Stopping http-server
remote: [PM2] stopProcessId process id 0
remote: ┌─────────────┬────┬──────┬─────┬─────────┬─────────┬────────┬────────┬──────────┐
remote: │ App name    │ id │ mode │ pid │ status  │ restart │ uptime │ memory │ watching │
remote: ├─────────────┼────┼──────┼─────┼─────────┼─────────┼────────┼────────┼──────────┤
remote: │ http-server │ 0  │ fork │ 0   │ stopped │ 0       │ 0      │ 0 B    │ disabled │
remote: └─────────────┴────┴──────┴─────┴─────────┴─────────┴────────┴────────┴──────────┘
remote:  Use `pm2 show <id|name>` to get more details about an app
remote: [PM2] restartProcessId process id 0
remote: [PM2] Process successfully started
remote: ┌─────────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
remote: │ App name    │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │
remote: ├─────────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
remote: │ http-server │ 0  │ fork │ 3350 │ online │ 0       │ 0s     │ 14.613 MB   │ disabled │
remote: └─────────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
remote:  Use `pm2 show <id|name>` to get more details about an app
To [email protected]:http-server.git
   a93ca12..9d8e959  master -> master

END

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