1.创建Makefile
.PHONY: all build run gotool install clean help?
all: gotool build?
build:?
? make clean?
? @if [ ! -f go.mod ];then go mod init video_api;fi?
? @if [ ! -d ./deploy/api/vapi ];then mkdir -p ./deploy/api/vapi;fi?
? #@go env -w GOPROXY=https://goproxy.cn,direct?
? @go mod tidy?
? CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ./deploy/api/vapi? ./?
? # make config #加载配置文件?
config:?
? cp ./config.docker.yaml deploy/api/vapi/?
install:?
? make build?
clean:?
? @if [ -f deploy/vapi/ ] ; then rm deploy/vapi/ ; fi
?2.编写docker-compose.yaml
version: '3'?
services:?
? vapi:?
? ? image: alpine:latest?
? ? container_name: vapi?
? ? privileged: true?
? ? restart: always?
? ? volumes:?
? ? ? - /etc/timezone:/etc/timezone?
? ? ? - /etc/localtime:/etc/localtime?
? ? ? - ./deploy/api/vapi:/go/src?
? ? ? - type: bind?
? ? ? ? source: /usr/share/zoneinfo/PRC?
? ? ? ? target: /usr/share/zoneinfo/PRC?
? ? command: /go/src/video_api?
#? ? command: /go/src/video_api -c? -c /go/src/config.docker.yaml //自定义配置文件?
? ? ports:?
? ? ? - "8333:8333"?
? ? extra_hosts:?
? ? ? - "host.docker.internal:host-gateway"
3.编写启动脚本 upload.sh
#!/bin/bash?
source="deploy/api docker-compose.yaml"?
zipfile="deploy/api.zip"?
host="43.136.xxx.xxx"?
remotedir="/data/video" #需要保证服务器的该目录存在?
make="install"?
if [ "$1" != "" ]; then?
? make=$1?
fi?
# build code?
set -x?
# shellcheck disable=SC2046?
make "$make"?
set +x?
res=$??
if [ $res -ne 0 ]; then?
? echo "use docker build go binary file fail"?
? echo?
? exit 1?
fi?
# clear old zip file?
if [ -f "$zipfile" ]; then?
? rm -f $zipfile?
fi?
# zip deploy files?
zip -q -r $zipfile $source?
echo "info:zip finished"?
scp -r $zipfile root@$host:$remotedir?
filez=$(basename $zipfile)?
# shellcheck disable=SC2087?
ssh root@$host <<eeooff?
cd $remotedir?
rm -rf $source __MACOSX?
unzip $filez?
rm -f $filez?
# backup docker logs?
#if [ ! -d "../logs" ]; then?
#? mkdir ../logs?
#fi?
#tar zcvf ../logs/docker-logs-$(date +%F%H%M%S).tar.gz /var/lib/docker/containers/*/*.log?
docker-compose down?
docker-compose up -d?
eeooff
4.启动脚本
sh upload.sh
5.参考项目
https://gitee.com/idcsc/godemo