shell脚本:一键编译go上传到服务器更新

#!/bin/bash

# 设置变量
localPath="/path/to/local/project"     # 本地项目路径
buildPath="/path/to/build"             # 构建输出路径
serverAddress="username@server"        # 服务器地址
remotePath="/path/to/destination"       # 远程目标路径
remoteProgram="your_program"           # 远程程序名称

# 进入项目目录
cd "$localPath"

# 构建 Go 程序
GOOS=linux GOARCH=amd64 go build -o "$buildPath/$remoteProgram" main.go

# 检查构建是否成功
if [ $? -eq 0 ]; then
    echo "Go程序构建成功"
else
    echo "Go程序构建失败"
    exit 1
fi

# 通过 SCP 上传到服务器
scp "$buildPath/$remoteProgram" "$serverAddress:$remotePath"

# 检查上传是否成功
if [ $? -eq 0 ]; then
    echo "Go程序上传成功"
else
    echo "Go程序上传失败"
    exit 1
fi

# 在服务器上启动程序
ssh "$serverAddress" "cd $remotePath && ./$remoteProgram &"

# 检查远程启动命令是否成功
if [ $? -eq 0 ]; then
    echo "Go程序在服务器上启动成功"
else
    echo "Go程序在服务器上启动失败"
    exit 1
fi

echo "完成"

转自:https://www.jianshu.com/p/b033e585b1af