Golang Echo,React Umijs 项目部署

nginx 配置文件

server {

        #强制https
        listen 80;
        listen [::]:80;
        server_name project.com;
        return 301 https://project.com$request_uri;
        
}	
map $http_upgrade $connection_upgrade {
	default upgrade;
	'' close;
}
server {

	listen 443 ssl;
	ssl_certificate /etc/letsencrypt/live/project.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/project.com/privkey.pem;
	
	server_name project.com;
	
	#默认为umijs 构建产物
	location / {
		root   /data/project/project/public/dist;
    	index  index.html index.htm;
    	try_files $uri $uri/ /index.html;
	}
	location /api {
			
	    # golang后端api监听18081端口
		proxy_pass http://127.0.0.1:18081/api;
		proxy_set_header Upgrade $http_upgrade; #websocket 链接
		proxy_set_header Connection $connection_upgrade;	
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;

	}

	error_log /var/log/nginx/project_error.log;
	access_log /var/log/nginx/project_access.log;

}

k8s服务器在国内,这个项目服务器在香港,ci/cd的话直接上传构建产物了,git只作为代码追踪使用

golang echo 部署脚本

#! /bin/bash
#打包压缩
GOOS=linux GOARCH=amd64 go build -o project && zip project.zip project 
#scp上传
scp -r ./project.zip root@hk1:/root/project/project
#登录到hk1服务器
# kill旧进程,这里也可以tmux直接停掉
# attach old session
ssh root@hk1 "
ps -ef | grep mqpay | grep -v grep | awk '{print \$2}' | xargs kill #tmux send-keys -t hk:0 C-c Enter
tmux a
tmux send-keys -t hk:0 'unzip -o project.zip && chmod +x project && ./project' Enter
"
echo 'deployed'

tmux send-keys -t $session:$window 'command1' keystroke

tmux发送指令,-t指定session/window 单引号接命令 回车键Enter C-c 这里是linux的停止命令

tmux allows a command to be bound to most keys, with or without a prefix key.
When specifying keys, most represent themselves (for example ‘A’ to ‘Z’).
Ctrl keys may be prefixed with ‘C-’ or ‘^’, Shift keys with ‘S-’ and Alt (meta) with ‘M-’.
In addition, the following special key names are accepted: Up, Down, Left, Right, BSpace, BTab, DC (Delete), End, Enter, Escape, F1 to F12, Home, IC (Insert), NPage/PageDown/PgDn, PPage/PageUp/PgUp, Space, and Tab.
Note that to bind the ‘"’ or ‘’’ keys, quotation marks are necessary, for example: bind-key ‘"’ split-window bind-key “’” new-window A command bound to the Any key will execute for all keys which do not have a more specific binding.

unzip -o project.zip && chmod +x project && ./project 解压缩并覆盖旧文件 然后赋予执行权限并启动项目

golang后端使用了logrus记录日志,控制台只是输出请求日志方便查看

react umijs 部署脚本

#! /bin/bash

yarn build
ssh root@hk1 "cd /data/project/project && rm -rf public/*"
scp -r ./dist root@hk1:/data/project/project/public

比较简单,就是本地构建然后删除旧的,scp将新的上传到服务器