How to implement a deployment from GitHub to a production server using Webhook

Original author: Jonathan Hsu
  • Transfer

I have long been in the habit of creating repositories on GitHub. This is much more effective than keeping everything on Google Drive or, worse, on your hard drive. But here the question immediately arises: how to deploy to a working server?

Most searches brought me to Jenkins and other continuous deployment tools. But I wanted to find a different solution. So I went to the free Webhook service.

Skillbox recommends: Practical course "Mobile Developer PRO" .

We remind you: for all readers of “Habr” - a discount of 10,000 rubles when registering for any Skillbox course using the “Habr” promo code.

The technical basis of Webhook is the fresh Digital Ocean droplet with Ubuntu 16.04 as a production server. To reduce the number of steps required to implement the plan, all actions are performed by the root user.

Let's start with github

If you have a repository and want to use it, you can skip this step - just use the SSH URI, and that’s it. If not, create one and use the SSH URI as well.

Install Go and Webhook (Digital Ocean droplet).

Before you begin, it’s worth a quick update and upgrade with the fresh Ubuntu 16.04 installer.

sudo apt update -y && sudo apt upgrade -y

Now to install WebHook you need to install the Go programming language. At the time of this writing, version 1.11.4 was relevant, so if you have a different version, you need to make changes.

sudo tar -C /usr/local -xzf go1.11.4.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

It's time to download the latest version of Webhook.

go get

There is no progress bar during the download, so you just have to wait. After the process completes, execute ~ / go / bin / webhook.

Webhook is installed, but you need to create a directory and file structure so that everything works as it should.

mkdir ~/webhooks
mkdir ~/webhooks/deployment-tutorial
touch ~/webhooks/hooks.json
touch ~/webhooks/deployment-tutorial/
chmod +x ~/webhooks/deployment-tutorial/

The hooks.json file is responsible for configuration and routing, and serves as a tool for executing the commands necessary for upgrading from GitHub.

The first step is to configure hooks.json by opening it in a text editor. The file contains the configuration for the endpoints that will be created after the launch of Webhook, and is an array of objects, each of which is a unique endpoint.

    "id": "deployment-tutorial",
    "execute-command": "/root/webhooks/deployment-tutorial/",
    "command-working-directory": "/root/deployed-site/",
    "response-message": "Executing deploy script...",
    "trigger-rule": {
        "match": {
            "type": "payload-hash-sha1",
            "secret": "The Returners",
            "parameter": {
                "source": "header",
                "name": "X-Hub-Signature"

Id is the unique name that will be used for the endpoint URL;
execute-command - a script that will be executed when the endpoint is activated;
command-working-directory - the directory that is used during the execution of execute-command;
trigger-rule - an option that will be used for information security purposes, this is a secret phrase for endpoints.

Let me remind you that I perform all the actions from root, so the starting address is / root. If you log in as a regular user, you need to register / home / username, where username, which is logical, is the name of this user. No need to use ~ /, the path must be absolute, otherwise you will get an error.

You may have noticed that we set up a working folder that does not yet exist. Before you create it, you need to finish with

The script should always start with “shebang”. Before opening the file, you should run which bash. Well, then the following commands are executed in the script:

git fetch --all
git checkout --force "origin/master"

Now both Go and Webhook are installed, so you need to configure the configuration in hooks.json and write a script responsible for the deployment. He will change the destination directory, working with the main branch of the GitHub repository.

Finally, it's time to bring Webhook to active mode, replacing 000,000,000,000 with the working IP of Doplet.

/root/go/bin/webhook -hooks /root/webhooks/hooks.json -ip "" -verbose

At runtime, you may notice the output of the URL with {id} at the end. This will be the id of the object that has already been created in the hooks.json: deployment-tutorial file.

Setting up Git (Digital Ocean Droplet)

Server setup is not finished yet. To complete it, you need to open a new terminal window and authenticate again on the server, while the first window is running Webhook. At the very beginning, we set the repository URI, now we need to use it.

To do this, go to the working directory specified in hooks.json and write the following:

git init
git remote add origin

In this case, you need to replace the URI with your own instead of what I indicated.

The final step will be to generate SSH keys in order to connect to GitHub with their help. In the terminal window, type ssh-keygen and confirm by pressing Enter until the keys are generated. Then you need to display the public keys by typing cat ~ / .ssh / You get something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyzJrPVOJqsTqD2R3xirTp3VNMwpmJMyLklzJg4sRQyslTUmbNNmDVO573EbXQQf2PqPQljqKDDlSaELdav4OTi1gPCoDary300yUqC/efLGHflZ6pMNuGsP2zTzerD/TMjzl1FXF1wOGTXqcC4TvGBS1bFyUY5n8wSOJ8ntZ6bBNv0zA2t7X1vH8ahIBJLKCayq9ipobKlHPYqxBt6zAoeh/ILQ0PWhGkmbGqqzqN1jcVWOefLgj4Dl8bZWORS1nkqrVg2wFC2nnibH97kZLsNrdQaeK8jUrkUWkJcUELI02mkkqh2RtBx9EwQEvsm9YuDBD9xF+HyuWoAeqcKerb root@github-webhook-tutorial

Well, now all that remains is to set up the GitHub repository and test the deployment.

Configure Deployment Key and Webhook (GitHub)

In the browser, go to the settings for the repository. First you need to add the Deployment Key.

When adding a key, you need to specify its name and insert it into the key output above. GitHub allows you to work only with public keys, so if necessary, you need to specify several keys. Once everything is finished, the Webhook section is the turn.

The three fields in this section need special attention. These are payload URL, content type and secret. Payload URL is the endpoint that Webhook listens to, content type is the data format, secret is the custom line from the hooks.json file. In our case, it is “The Returners”.

After creating Webhook, a corresponding mark in the settings should appear, which indicates that the server is available. If you check the terminal with a working Webhook, you can see that the repository is already in operation.

To check the workflow, copy the repository to the local machine, if not already done, and make changes to the main branch. After that, they should be displayed on the server. Done.

The developer, who has already done all this several times, spends about 10 minutes on configuration. But this is after hours of trial and error. Remember these four important customization features:

  • Be sure to use the absolute path for hooks.json.
  • Do not make executable.
  • Confirm SSH connections during the first “contact” of GitHub and the production server.
  • Do not use the erroneous “shebang” in the bash script.

Skillbox recommends:

Also popular now: