Skip to content

Blog

Auto deploy from Bitbucket or any other git repository

Update aptitude first

$ sudo apt-get update

Install expect

$ sudo apt-get install expect

Create a directory named trigger in your home directory

$ cd
$ mkdir trigger
$ cd trigger

Create following files inside the trigger directory. trigger.js

$ cat trigger.js
var server_port = <new_port_for_trigger>;

var sys = require('sys');
var exec = require('child_process').exec;
var child;

var http = require('http');
var express = require('express');
var app = express();
var server_get = require('http').Server(app);

app.post('/update', function(req, res) {
    child = exec("./update_repo.sh", function(error, stdout, stderr) {
                console.log('stdout: ' + stdout);
                console.log('stderr: ' + stderr);
                if (error !== null) {
                    console.log('exec error: ' + error);
                }
    });

    res.send("SUCCESS");
});

app.listen(server_port, function() {
    console.log('Example app listening on port ' + server_port);
}

package.json

$ cat package.json
{
    "name": "Trigger",
    "version": "0.0.1",
    "scripts": {
        "start": "node server"
    },
    "dependencies": {
        "express": "^4.14.0",
        "http": "0.0.0",
        "https": "^1.0.0"
    }
}

update_repo.sh

$ cat update_repo.sh
#!/bin/bash
cd <your_git_repo>
~/trigger/git_pull_helper.sh
pm2 restart server

git_pull_helper.sh

$ cat git_pull_helper.sh
#!/usr/bin/expect -f
spawn git pull
expect "ass"
send "<your_ssh_key_pass_phrase>\r"
interact

Update permission for update_repo.sh and git_pull_helper.sh

$ chmod +x update_repo.sh
$ chmod +x git_pull_helper.sh

Start your trigger NodeJS server

$ pm2 start trigger.js

  • Configure you nginx if you are using one
  • Don't forget to enable firewall to allow the new port

Update in bitbucket

  • Got Settings
  • Select Webhooks in Workflow section
  • Click Add webhook button
  • Give name
  • Enter your url as *<your_domain_name>.<ext>:<your_port_for_trigger>/update*
  • Press save button

Or follow the steps provided by your git server.

Add swap space to Ubuntu

Truffle compilation was crashing with memory-overrun issues in my 512 MB basic droplet. So I added 2 GB of swap space with following commands.

Make sure you have no swap config previously. There should be no output.

$ sudo swapon --show
$

The simple way of doing this is by creating a swap file. Lets look at all the partitions to see how much free space in each of them.

$ df -h
kaba@ubuntu-512mb-blr1-01:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            238M     0  238M   0% /dev
tmpfs            49M  5.9M   43M  13% /run
/dev/vda1        20G  5.2G   15G  27% /
tmpfs           245M   24M  221M  10% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           245M     0  245M   0% /sys/fs/cgroup
/dev/vda15      105M  3.4M  102M   4% /boot/efi
tmpfs            49M     0   49M   0% /run/user/0
tmpfs            49M  8.0K   49M   1% /run/user/1000
$
* Other than the ones with /dev/ are virtual filesystem and are not usable. * I'm gonna create my swap file in /dev/vda1.

Use fallocate utility to create a preallocated file in root directory.

$ sudo fallocate -l 2G /swapfile
* As in previous command output, dev/vda1 is mounted on / * 2G specifies 2 GB of space. Use whatever size you want in that place.

Make the is file accessible to root user alone

sudo chmod 600 /swapfile

Mark the file as swap space

$ sudo mkswap /swapfile

Enable the swap file, so your system will start using it

$ sudo swapon /swapfile

Done!

Now swapon command shouldn't return an empty result

$ sudo swapon --show
NAME      TYPE SIZE   USED PRIO
/swapfile file   2G 771.6M   -1


Make swap settings permanent

You need to edit /etc/fstab file. So better take a backup of it.

$ sudo mv /etc/fstab /etc/fstab.orig

Update the file with swap information

$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Configure Vim

The above picture is from my favorite serial Mr.Robot. Elliot Alderson is writing a script to hack Evil Corp.

I have three development environments * One Cent-OS machine for C programming in office * Ubuntu-16.04 Digitalocean and AWS virtual machines to host some servers and Ethereum developemnt * And one Raspberry PI 3 at home as an SSH client

As the Cent-OS machine has older version of vim, Plugins that require latest version of vim and plugins that are only needed for Web/Ethereum development are kept under an if condition. So modify it based on your need.

Here I didn't give any explanation for any config or plugin. Copy pasting them in Google will give you the information.

Backup you vimrc file

$ mv ~/.vimrc ~/.vimrc.orig

Clone Vundle into your ~/.vim directory

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Copy this vimrc file into your home directory.

Install plugins using PluginInstall command. Open vim with no file. Ignore if any error shows up. Press Esc key then : and type the command PluginInstall followed by Enter key. A new window will open and install all plugins.

$ vim

:PluginInstall

Don't forget to install 'YouCompleteMe' in your machine.

$ cd ~/.vim/bundle/YouCompleteMe/
$ ./install.py --js-completer --clang-completer --system-libclang
Installing auto-complete feature for JavaScript and C Language. See YouCompleteMe GitHub page for more details.