The general process is as follows:

Installation services (Ubuntu in my case)

Installing the Node Environment

#Create a new nodejs folder under /root (you can follow your own path here)
mkdir nodejs

#Official website to downloadWget HTTP: / / https://nodejs.org/dist/v12.10.0/node-v12.10.0-linux-x64.tar.xz
#Wget: Unable to resolv host address
#You can try the following
vi /etc/resolv.conf
#Add the following codeNameserver 8.8.8.8 # Google https://nodejs.org

#Unpack theThe tar xf node - v12.10.0 - Linux - x64. Tar. Xz
#/root/tool/nodejs /nodejs /nodejs /nodejs /nodejs /nodejsLn -s /root/tool/nodejs/node-v12.10.0-linux-x64/bin/node /usr/local/bin/node ln -s / root/tool/nodejs/node - v12.10.0 - Linux - x64 / bin/NPM/usr/local/bin/NPM

#testNode -v Output: v12.10.0 NPM -v Output: 6.10.3 It indicates that nodeJS is successfully installed
#Install CNPM
npm install -g cnpm -registry=https://registry.npm.taobao.org
#/ root/tool/nodejs/node - v12.10.0 - Linux - x64 / bin/CNPM - > / root/tool/nodejs/node - v12.10.0 - Linux - x64 / lib/node_modules/CNPM/bin/CNPM
#Configure the environmentLn -s/root/tool/nodejs/node - v12.10.0 - Linux - x64 / bin/CNPM/usr/local/bin/CNPMCopy the code

Vue – CLI server installation

Install CNPM: sudo NPM install – g CNPM – registry=https://registry.npm.taobao.org

Run NPM install -g@vue /cli

Output after installation:

/ root/tool/nodejs/node - v12.10.0 - Linux - x64 / bin/vue - > / root/tool/nodejs/node - v12.10.0 - Linux - x64 / lib/node_modules / @ vue/cli/bin/vue. JsCopy the code

Configuring environment variables (soft connection package) :

Ln -s/root/tool/nodejs/node - v12.10.0 - Linux - x64 / bin/vue/usr/local/bin /Copy the code

Vue -v takes effect!

Install the docker – compose

apt install docker-compose

#Test to see if the installation is successful
mkdir helloworld

vi docker-compose.yml

#Put the following code inVersion: '3.1' Services: hello-world: image: hello-world
#Start the
docker-compose up
#If logs are displayed, the installation is complete
Copy the code

The Compose project is Docker’s official open source project, which is responsible for the rapid choreography of Docker container clusters.

In a word: If several Dockers want to work together, use Compose

Code configuration

First, you need to configure githook on Github: Git project => Settings => Webhooks

Configure VSCode (upload code use)

// .vscode/sftp.json
{
    "name": "AliyunServer"."host": "xx.xx.xx.xx"."port": 22."username": "root"."password": "xxxxxxxxxxx"."protocol": "sftp"."passive": false."interactiveAuth": false."remotePath": "/root/for-study-test/git-for-study"."uploadOnSave": false."syncMode": "update"."ignore": [            
        "**/.vscode/**"."**/node_modules/**"."**/.DS_Store"]}Copy the code

Create an Autobuild folder in the project root directory

Install Github – webhook-Handler and PM2

npm i github-webhook-handler pm2 -g
Copy the code

Write webhook services

// autobuild/webhook.js
const http = require('http');
// Github -webhook-handler absolute path
var createHandler = require('/ root/tool/nodejs/node - v12.10.0 - Linux - x64 / lib/node_modules/lot - webhook - handler')
// keep secret consistent with GitHub background Settings
var handler = createHandler({ path: '/'.secret: 'dylanlv2021' })

function run_cmd(cmd, args, callback) {
  var spawn = require('child_process').spawn;
  var child = spawn(cmd, args);
  var resp = "";

  child.stdout.on('data'.function(buffer) { resp += buffer.toString(); });
  child.stdout.on('end'.function() { callback (resp) });
}

http.createServer(function (req, res) {
  handler(req, res, function (err) {
    res.statusCode = 404
    res.end('no such location')
  })
}).listen(7777) // The security group needs to be opened for the port to start the service

handler.on('push'.function (event) {
  console.log('Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref);
    run_cmd('sh'['./webhooks.sh',event.payload.repository.name], function(text){ console.log(text) });
})
Copy the code

Note: The webhook service listens on the same port that Webhooks open on Git. Consistency is required.

Writing shell scripts

autobuild/webhook.sh

#! /bin/bashWEB_PATH='/root/for-study-test/git-for-study/deploy_server/pm2-test/server' echo "starts executing shell" CD $WEB_PATH echo "pulling source code..." git pull && echo "changing permissions..."#chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATHEcho "git pull complete. Docker-compose "docker-compose down && docker-compose up -d echo Docker-compose restart completedCopy the code

Start the PM2 service

#In this folder: autobuild/ 
pm2 start webhooks.js -o ./webhooks.log
Copy the code

Githook’s functionality is now complete!!

Here is the code to configure Docker-compose:

// autobuild/docker-compose.yml
version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    ports:
      - 81: 81
    volumes:
      - ./conf.d/:/etc/nginx/conf.d
      - /root/for-study-test/git-for-study/deploy_server/pm2-test/dist:/usr/share/nginx/test/
Copy the code
// autobuild/conf.d/docker.conf
server {
  listen  81; location / { root /usr/share/nginx/test; index index.html index.htm; try_files $uri $uri/ /index.html; }}Copy the code