I have two projects on Github for automatic deployment of Gitlab code, one for the PHP version of Gitlab Webhooks and the other for the NodeJS version of Gitlab Webhooks. I have previously shared the PHP version of Gitlab Webhooks in the community, and most of you will probably follow this tutorial and try to install and deploy them yourself. In this article I recommend using the NodeJS version of Gitlab Webhooks and explain why I recommend using them.

Gitlab Webhooks check the current commit Git version branch and execute different Linux Shell scripts to achieve automatic deployment of project code. It is easy to understand the logic of PHP.

First, after cloning https://github.com/bravist/gitlab-webhook-php, need many times to modify the project configuration information and rely on running the host directory

# see gitlab. PHP $access_token = 'MmNmMGRmMGI2Y2EyYjY3NzFjMzRjODkzZTQ3NjY5M2Y ='; $access_IP = array('{server}'); $access_ip = array('{server}'); Exec ("/ MNT/WWW /{project}/public/gitlab-webhook-php/master_deploy.sh"); Develop_deploy. sh CD/MNT/WWW /{project} #Copy the code

For those of you who are hands-on, you should know that configuration information depends on other project environments and is very inflexible.

Second, running PHP Webhooks requires the Linux server to provide Web and PHP services, and when the server is built with the Docker runtime, PHP Webhooks cannot continue to use them

In recent years, Docker has become very popular in the DevOps space. We have programmed a set of Running resources of Docker by ourselves. Interested students can complete a complete PHP Web environment in 5 minutes. It is recommended that individual users purchase a VPS from DigitalOcean to install CentOS7 and try Docker. Here is a brief description of choreography resources:

Lnmpdocker_nginx_1 ## Nginx: lnmpdocker_php-fpm_1 ## PHP-FPM: Lnmpdocker_www_1 ## MariaDB: lnmpdocker_www_1 ## WWW: Lnmpdocker_redis_1 ## Redis: Redis cache service containerCopy the code

Each container here is a separate virtual server that provides a different service. We do not have the PHP script interpreter installed on the CentOS 7 host (as opposed to the Docker container) and cannot run the PHP version of Gitlab Webhooks.

To improve the flexibility and availability of automatic deployment of Gitlab source code, the NodeJS version of Gitlab Webhooks is recommended. The following describes automatic deployment of source code using the NodeJS version of Gitlab Webhooks in CentOS 7.

1. Install the Node environment

The Node version management tool NVM is recommended.

# # use curl tool to download and install the $wget - qO - https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash using NVM $# # in the current window $NVM install v7.8.0 ## $NVM install v7.8.0 ##Copy the code

The latest version of Node automatically installs the package management tool NPM. Chinese users are advised to use the image tool CNPM of Taobao.

# # use CNPM replace NPM $NPM install - g CNPM - registry=https://registry.npm.taobao.orgCopy the code

2. Install the NodeJS Gitlabhook extension package

Access NPMJS, the online repository for NodeJS package management, and search for Gitlabhook to view related configuration information.

Gitlabhook $NPM install --global gitlabhook $NPM installCopy the code

Configuration gitlabhook. Conf

# # into the ~ /. NVM/versions/node/v7.8.0 / lib/node_modules $CD ~ /. NVM/versions/node/v7.8.0 / lib/node_modules/gitlabhook Conf $cp gitlabhook. Conf $cp gitlabhook. Conf $cp gitlabhook /etc/gitlabhook.conf { "tasks": { "*": [ "echo 'GitLab Server: %s' > /tmp/gitlabhook.tmp", "echo 'Repository: %r' >> /tmp/gitlabhook.tmp", "echo 'Repository branch: %b' >> /tmp/gitlabhook.tmp", "echo 'Repository remote url: %g' >> /tmp/gitlabhook.tmp", "echo $(date) >> /tmp/gitlabhook.tmp", "cd /mnt/www/%r >> /tmp/gitlabhook.tmp", "sudo -u www git checkout develop >> /tmp/gitlabhook.tmp", "sudo -u www git pull origin develop >> /tmp/gitlabhook.tmp" ] }, "keep":false }Copy the code

Here is a special reminder:

  • The Git version management tool must be installed on the CentOS hostyum install -y git
  • By default, you need to run the git command to clone the remote repository on the CentOS host. You need to configure the ssh-key on the CentOS host and configure it in gitlab. Otherwise, an error message will be displayed: “Clone repository without permission”
  • The owner of the virtual host directory on the CentOS host is required to use git commands, such as in configuration fileswwwThe user went to clone the repository
  • Check the clone repository version on your CentOS host. Use it in test environmentsdevelopBranch, recommended for production environmentsmasterbranch

3. Install the NodeJS process management tool PM2

The NodeJS process management tool PM2 is used to monitor and manage NodeJS processes.

## Install PM2 $NPM install --global PM2 ## Install PM2 $NPMCopy the code

4. Run NodeJS Gitlabhook

# # to perform gitlab - server. Js $pm2 start ~ /. NVM/versions/node/v7.8.0 / lib/node_modules/gitlabhook/gitlabhook - server. Js # # See if gitlab - server port open $netstat - tunlp | grep # # 3420 in local confirm whether can access the remote port 3420 $Telnet 138.68.24.82 3420 Trying 138.68.24.82... '> < span style = "max-width: 100%; clear: bothCopy the code

5. Configure Webhooks in the Gitlab repository

Log in to your Gitlab project management background, search for Webhooks and add Push Events (Webhooks may vary between versions of Gitlab configurations).

6. Commit and test

In your local repository, execute git push and go to the CentOS server to check whether the code is automatically synchronized.

NodeJS version of Gitlab Webhooks removes strong dependencies with the Web server, while the server runtime environment source code remains clean and flexible, suitable for the Docker runtime environment. In addition, this project can run not only in Gitlab version repository, but also in Github, Coding and other operating environments.