In my last article, I documented how to use Docker🐳+Nginx+WebHook+Node with one limitation: you can only deploy packaged static websites. What about projects that need to be ordered to start? I spent my dinner time, finally in the instant noodles 🍜+ chives scrambled eggs 🥚, let me want to come out!

Imagine how much easier it would be to blog if your code could be built automatically as you push it. What if I told you that this could be done with a single command? ❤ ️

The high earners in this article:

  • Cloud Server Constructionblog, but not configured yetAutomated continuous deployment
  • Want to knowdocker + NginxThe deployment of
  • rightwebhookInterested in the usage
  • Want to learn to go outPack to forcePeople 🤪

Your praise 👍+ collection 🌟 is my power forever!

advantage

Just a line

Pm2 nohup is fine, just start oneLine
sh oneLine.sh
Copy the code

High portability

With the help of Docker, this automation is continuously deployed and can be ported to various platforms. If you want to switch cloud servers, switch systems. So congratulations, learn this article, one line of code is done!

Automated continuous deployment

Webhook help code can be sensitive to monitor github hook such as Git push, when your project changes, can get the latest changes in the first time, and automatically redeploy. 😆

In actual combat

Docker +Nginx+ Webhook +Node 👉🏻Docker🐳+Nginx+ Webhook +Node

Nginx configuration

🛑 Note aliases

# nginx/conf.d/docker.conf
Reverse proxy service port 4000 to Nginx service port 80
server {
    listen 80;
    location / {
        # Note that ML-blog is an alias for another image in docker, not a real URL!
        proxy_pass http://ml-blog:4000;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

Docker configuration

There are two Docker configurations:

  • Dockfile + process.yml
  • docker-compose.yml

Dockerfile content:

Pull the PM2 image to start your entire project
FROM keymetrics/pm2:latest-alpine
WORKDIR /usr/src/app
ADD . /usr/src/app
# Mirror acceleration
RUN npm config set registry https://registry.npm.taobao.org/ && \
    npm i
# pm2 in docker use the command pm2-docker
Fill in the command configuration using process.yml
CMD ["pm2-runtime"."start"."process.yml"]
Copy the code

Process. Yml content:

apps:
  Write your project startup command
  - script : npm run server
      If I try 1, I will have a problem, so I write 2
      instances: 2
      # Enable listening, if set to true, command exceptions can constantly try to reconnect
      watch  : true
Copy the code

Docker – compose. Yml content:

Note that the docker-compose version needs to match your docker-compose version
version: '2'
services:
  ml-blog:
    Nginx alias: http://ml-blog:4000
    container_name: ml-blog
    restart: always
    # build container
    build: .
    ports:
    - "4000:4000"
  nginx:
    # restart Restart the connection until an exception occurs
    restart: always
    image: nginx
    Map Nginx 80 to port 80 on the server
    ports:
      - 80: 80
    The Nginx configuration file takes effect only after being copied to the specified directory
    volumes:
      - ./nginx/conf.d/:/etc/nginx/conf.d
Copy the code

At this point, automated deployment is actually complete, but if you want to implement push-and-rebuild, you need webhook configuration

Webhook configuration

I will not explain how to configure Webhook in detail. Please refer to my last article

const http = require('http')
const createHandler = require('github-webhook-handler')

const handler = createHandler({
    path:'/XXXXX'.secret:'XXXXXXX'
})

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((req,res) = > {
  handler(req,res,err => { res.statusCode = 404
    res.end('no such location')
  })
}).listen(8000, () = > {console.log('Webhook listen at 8000')
})

handler.on('error',err => {
  console.error('Error',err.message)
})

handler.on('push'.function (event) {
  console.log('Received a push event for %s to %s', event.payload.repository.name, event.payload.ref);
  // Branch judgment
  if(event.payload.ref === 'refs/heads/master') {console.log('deploy master.. ')
    run_cmd('sh'['./pull.sh'].function(text){ console.log(text) }); }})Copy the code

Pull. Sh content

#! /bin/bash
git pull

# force a recompile of the container
docker-compose down
docker-compose up -d --force-recreate --build
Copy the code

The last of the shell

OneLine. Sh content

It’s really two things:

  • Pm2 start webhook. Js
  • Docker – compose start
# to monitor
pm2 start --name ml-blog webhooks.js

# start docker - compose
docker-compose up
Copy the code

Come and see the results!

Pm2 listening webhook

Nginx container, the first two are our current, the bottom is my other Nginx service

conclusion

This learning let me know Docker🐳 and Nginx powerful!

Docker yes, Nginx Yes!

Welcome to comment, learn together, progress together! 🎉 🎉 🎉