go 编译npm 前端包 发版

package main


import (
	"bufio"
	"fmt"
	"golang.org/x/text/encoding/simplifiedchinese"
	"io"
	"os/exec"
	"time"
)
type jenkins struct{
	code string
	project string
	work string
	webWork string
	env  string
	branch string
	server string
	build string
	action string
}


type Charset string


const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
)


func main(){
	p1 := jenkins{
		code : "https://gitee.com/hequan2019/quan",
		project: "quan",
		work : "/data/workspace",
		webWork: "/data/wwwroot",
		env : "测试",
		branch: "main",
		server: "192.168.174.129",
		action: "构建",
		build: "npm",
	}
	num := time.Now().Unix()
	dir := fmt.Sprintf("/data/tmp/%s-%v",p1.project,num)


	mkdir :=[]string{
		"-c",
		fmt.Sprintf("mkdir -p %s ",dir),
	}
	execCommand("/bin/sh", mkdir)


	par:=[]string{
		"clone",
		"-b",
		fmt.Sprintf("%s",p1.branch),
		fmt.Sprintf("%s",p1.code),
		fmt.Sprintf("%s/%s",dir,p1.project),
	}
	execCommand("/usr/bin/git", par)


	par1:=[]string{
		"-c",
		fmt.Sprintf("cd %s/%s/web && npm install && npm run build",dir,p1.project),
	}
	execCommand("/bin/sh", par1)


	par2:=[]string{
		"-c",
		fmt.Sprintf("cd %s/%s/web && tar zcf %s.tar.gz dist/",dir,p1.project,p1.project),
	}
	execCommand("/bin/sh", par2)


	par3:=[]string{
		"-c",
		fmt.Sprintf("ssh  root@%s ‘mkdir -p %s/backup/%v‘",p1.server,p1.work,num),
	}
	execCommand("/bin/sh", par3)


	par4:=[]string{
		"-c",
		fmt.Sprintf("cd %s/%s/web && scp  %s.tar.gz  root@%s:%s",dir,p1.project,p1.project,p1.server,p1.work),
	}
	execCommand("/bin/sh", par4)


	par5:=[]string{
		"-c",
		fmt.Sprintf("ssh  root@%s ‘cd %s && tar xf %s.tar.gz && rm -rf %s.tar.gz ‘",p1.server,p1.work,p1.project,p1.project),
	}
	execCommand("/bin/sh", par5)


	par6:=[]string{
		"-c",
		fmt.Sprintf("ssh  root@%s ‘cd %s && mv *  %s/backup/%v/‘",p1.server,p1.webWork,p1.work,num),
	}
	execCommand("/bin/sh", par6)


	par7:=[]string{
		"-c",
		fmt.Sprintf("ssh  root@%s ‘cd %s && mv dist/*  %s‘",p1.server,p1.work,p1.webWork),
	}
	execCommand("/bin/sh", par7)


}


func execCommand(commandName string, params []string) bool {


	cmd := exec.Command(commandName,params...)


	fmt.Println(cmd.Args)
	stdout, err := cmd.StdoutPipe()
	errReader,err1 := cmd.StderrPipe()
	if err1 != nil{
		fmt.Println("err:"+err1.Error())
	}
	//开启错误处理
	go handlerErr(errReader)
	if err != nil {
		fmt.Println("输出",err)
		return false
	}


	err = cmd.Start()
	if err != nil {
		return false
	}
	in := bufio.NewScanner(stdout)
	for in.Scan() {
		cmdRe:=ConvertByte2String(in.Bytes(),"UTF8")
		fmt.Println("输出",cmdRe)
	}
	err = cmd.Wait()
	if err != nil {
		fmt.Println("错误1",err)
		return false
	}
	return true
}


//开启一个协程来输出错误
func handlerErr(errReader io.ReadCloser){
	in := bufio.NewScanner(errReader)
	for in.Scan() {
		cmdRe:=ConvertByte2String(in.Bytes(),"UTF8")
		fmt.Println("输出",cmdRe)
	}
}


func ConvertByte2String(byte []byte, charset Charset) string {
	var str string
	switch charset {
	case GB18030:
		var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
		str = string(decodeBytes)
	case UTF8:
		fallthrough
	default:
		str = string(byte)
	}
	return str
}

go 编译npm 前端包 发版

上一篇:数据分组


下一篇:defer实现原理