Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ricvillagrana/372009fab0c4584cc0e7bd273f721220 to your computer and use it in GitHub Desktop.
Save ricvillagrana/372009fab0c4584cc0e7bd273f721220 to your computer and use it in GitHub Desktop.
How to deploy files to Digital Ocean when pushing to Gitlab

How to get auto-deploy working from Gitlab to your Digital Ocean (DO) server.

Install https://github.com/olipo186/Git-Auto-Deploy (via apt-get):

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:olipo186/git-auto-deploy
sudo apt-get update
sudo apt-get install git-auto-deploy

Change config file to match your setup:

nano /etc/git-auto-deploy.conf.json

Here's my config file:

{
  "logfilepath": "/var/log/git-auto-deploy.log",
  "host": "0.0.0.0",
  "port": 8016,
  "global_deploy": [
    "echo Deploy started!",
    "echo Deploy completed!"
  ],
  "repositories": [
    {
      "url": "https://gitlab.com/username/domain.com.git",
      "branch": "master",
      "remote": "origin",
      "path": "/site",
      "deploy": "echo deploying",
      "secret-token": "secretstuff"
    }
  ]
}

You need to make sure you have the port you specify - in my case 8016 - allowed by the firewall:

sudo ufw allow 8016/tcp

Time to start the git-auto-deploy service and test the port.

service git-auto-deploy start
service git-auto-deploy status // * /usr/bin/git-auto-deploy is running

From another machine, check if the port is receiving connections:

$ telnet domain.com 8016
Trying XXX.XXX.XX.XX...
Connected to domain.com.
Escape character is '^]'.

Now you can add it as a hook in Gitlab. As per git-auto-deploy docs, 'Go to your repository -> Settings -> Web hooks. In "URL", enter your hostname and port (http://domain.com:8016). Hit "Add Web Hook"''. The secret token must be the same as in git-auto-deploy.conf.json above. I left "Enable SSL verification" unchecked. After you add it, click the 'test' button to make sure it returns HTTP 200.

On DO server, there will be a new user, git-auto-deploy and group with the same name. You need to generate an SSH key for this user on the server in their home directory, under .ssh(cd ~/.ssh):

ssh-keygen -t rsa -C "git_auto_deploy_on_digital_ocean" -b 4096

Add the new public key (contents of the ~/.ssh/id_rsa.pub) to Gitlab, click on user icon in the top right, then user settings -> SSH keys

Test it on the server:

git init
git remote add origin https://gitlab.com/username/domain.com.git
git fetch

If the fetch completes without error then your SSH key works.

Here's the directory structure to keep all the service stuff (.git directory, package.json, etc.) outside of the publicly-accessible folder:

|--site
   |--.git
   |--package.json
   |--public_html
      |--assets
      |--index.html

You're ready to test it out. Put a tail on the log file:

tail -f /var/log/git-auto-deploy.log

On your machine, commit a test file and push:

touch test.html
git add test.html
git commit -m 'test'
git push

Inside the logfile, you should see something like this:

[INFO ]  Updating repository /site
[INFO ]  HEAD is now at 7d1153a updated gulp to reflect directory change
[INFO ]  Repository /site successfully updated
[INFO ]  Executing 3 deploy commands
[INFO ]  Deploy started!
[INFO ]  deploying
[INFO ]  Deploy completed!
[INFO ]  3 commands executed with status; [0, 0, 0]
[DEBUG]  Successfully released lock: /site/status_running
[INFO ]  Done

If you see an error like below, it means your folder/file permissions are incorrect. Make sure the git-auto-deploy user can write to the folder. I just made this user owner of the public folder.

[ERROR] error: cannot open .git/FETCH_HEAD: Permission denied

If you see the below, try to run a git pull inside the repo on the server before you test pushing to Gitlab from your machine.

[ERROR] fatal: Not a git repository (or any of the parent directories): .git

On the server, all the files from repo should now be present. Now every time you push to Gitlab, these changes will also be pushed to your DO server. Rejoice!

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