Automatic deployment tool for golang

Time:2020-6-3

An example of automatic deployment in golang

Why do I need to write this file when Jenkins is deployed automatically? It’s because the company’s server is too expensive. It’s fried when Jenkins is installed.
So I want to use golnag to develop an automatic deployment tool. Every time I update the program, SCP is too painful

Practice completed Automated Deployment Tool

https://github.com/dollarkill…

The realization of automatic deployment

Automatic deployment tool for golang
After reading this picture, you should find that it is not difficult to practice
Just call git pull after receiving the request

Here is the program analysis

#Please fill in the file generated by Devops and run it after checking and confirming (each run will rewrite the SH module)


#This Devops system configuration
app:
  host: "0.0.0.0:8083"
  debug: true
  max_request: 1000
  task_num: 10

#To automate the configuration of deployment applications
devops:
  node:
    -Port: "8081" - program running port
      full_ Name: "dollarkillerx / easyutils" ා name e.g. dollarkillerx / easyutils
      Branch: "master" branch
      giturl: " https://github "Git pull URL address (you need to configure the secret key first!)
      Runname: "API" - the name of the running program
      Dirpath: "/ home / s" ා absolute path
      Secondarydirectory: "" (fill in here if there is a secondary directory)
    - port: "8082"                              
      full_name: ""      
      branch: "es"      
      giturl: ""    
      runname: ""    
      dirpath: ""    
      secondarydirectory: ""

The node part of the yaml file filled in by the user is a [] slice
When the system is initialized, I get to read this file

There’s a problem here. Let’s run the program in steps, but this two-level program may not be in the outermost directory of this project, so we need a secondary directory to guide it

var sh1 = `
#! /bin/sh

lsof -i :%s | awk '{print $2}'> tmp
pid=$(awk 'NR==2{print}' tmp);
kill -9 $pid

#If git clone does not exist in the file 
if [ ! -d "%s" ];then
    cd %s
    git clone -b %s  %s
    cd %s
    ./%s &    
else
#If the file exists
    cd %s
    git pull
    ./%s &

fi
`

var sh2 = `
#! /bin/sh

lsof -i :%s | awk '{print $2}'> tmp
pid=$(awk 'NR==2{print}' tmp);
kill -9 $pid

#If git clone does not exist in the file 
if [ ! -d "%s" ];then
    cd %s
    git clone -b %s  %s
    cd %s
    cd %s
    ./%s &    
else
#If the file exists
    cd %s
    git pull
    cd %s
    ./%s &

fi

Here we write two sh templates to read when initializing the system config.yml Then generate the corresponding sh

Analysis of data sent by GitHub when pull

{
  "ref": "refs/heads/master",
  "before": "b61cd27c0bfe30cbac7a731a6ce22790d9e1f6f7",
  "after": "05300bfb10912f370ac74a5cbc2ed2095a60466c",
  "repository": {
    "id": 208684536,
    "node_id": "MDEwOlJlcG9zaXRvcnkyMDg2ODQ1MzY=",
    "name": "Cartoon5",
    "full_name": "dollarkillerx/Cartoon5",

Data returned when pull is triggered
Let’s see
“Ref”: “refs / heads / Master”, this is the branch
“full_ Name “:” dollarkillerx / Cartoon5 “, this is the project address

When more than one website is deployed on the server, the two websites can be judged to distinguish which script to execute now

Is this system very simple!