下載go1.20.2.linux-arm64.tar.gz 并存放到jenkins home目錄:
寫一個golang demo程序:靜態文件服務器:https://gitee.com/jelex/jenkins_golang
package mainimport ("encoding/base64""flag""fmt""log""net/http""strings"
)const (uw = "username用戶名:你想要設置的密碼"
)type authFileSrvHandler struct {http.Handler
}func (f *authFileSrvHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {auth := r.Header.Get("Authorization")log.Println("got req:auth", auth)if auth == "" {w.Header().Set("WWW-Authenticate", `Basic realm="您必須輸入用戶名和密碼"`)w.WriteHeader(http.StatusUnauthorized)return}log.Println("auth->", auth)split := strings.Split(auth, " ")if len(split) == 2 && split[0] == "Basic" {bytes, err := base64.StdEncoding.DecodeString(split[1])if err == nil && string(bytes) == uw {f.Handler.ServeHTTP(w, r)return}}w.Write([]byte("請聯系相關人員獲取用戶名和密碼!"))
}/*
*通過這種方式修改文件服務器的根路徑及端口jelex@jelexxudeMacBook-Pro static-file-server % go run .\main.go -p 88888888exit status 2
jelex@jelexxudeMacBook-Pro static-file-server % go run .\main.go -r <absolute path>
jelex@jelexxudeMacBook-Pro static-file-server % go run main.go -r ~
*/
func main() {var rootPath, port stringflag.StringVar(&rootPath, "r", "", "文件根目錄")flag.StringVar(&port, "p", "6000", "文件服務器端口")flag.Parse()mux := http.NewServeMux()fs := http.FileServer(http.Dir(rootPath))mux.Handle("/", &authFileSrvHandler{fs})fmt.Println(rootPath, port)http.ListenAndServe(":"+port, mux)
}
bin/build.sh
#局部變量(執行文件名稱), 根據自己項目寫
project_name="file-srv"
#殺掉之前正在運行的程序
go_id=`ps -ef|grep "./${project_name}" |grep -v "grep" | awk '{print $2}'`
if [ -z "$go_id" ];
thenecho "[go pid not found]"
else#殺掉進程kill -9 $go_idecho "killed $go_id"
fiecho "clean old file"
rm -rf ${project_name}
#執行日志,根據自己項目情況可選
rm -rf ${project_name}.logif [ -f main ]; thenecho "start new process"mv main ${project_name}chmod -R 777 ${project_name}#這里要防止nohup不執行,添加了一個BUILD_ID, -r ~ 表示程序運行的參數(根目錄為~ home目錄)BUILD_ID=DONTKILLME nohup ./${project_name} -r ~ >${project_name}.log 2>&1 &
else
echo "executable file not found,quit"
fi
新配置一個服務器:事先在服務器上創建好目錄 /root/golang
構建步驟:
#source /etc/profile #寫了這個才會報錯!所以注釋掉
echo $WORKSPACE
cd $WORKSPACE
export GOPROXY=https://goproxy.cn,direct
#go mod tidy
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 /var/jenkins_home/go/bin/go build -o bin/main static-file-server/main.go #如果要部署到amd架構的服務器,如此處理。。。
構建后操作:
構建:
控制臺輸出:
Started by user rootRunning as SYSTEM
Building in workspace /var/jenkins_home/workspace/golang-proj
The recommended git tool is: NONE
using credential 53623f15-682e-456e-8a6e-72cd80c011c6> git rev-parse --resolve-git-dir /var/jenkins_home/workspace/golang-proj/.git # timeout=10
Fetching changes from the remote Git repository> git config remote.origin.url https://gitee.com/jelex/jenkins_golang.git # timeout=10
Fetching upstream changes from https://gitee.com/jelex/jenkins_golang.git> git --version # timeout=10> git --version # 'git version 2.30.2'
using GIT_ASKPASS to set credentials > git fetch --tags --force --progress -- https://gitee.com/jelex/jenkins_golang.git +refs/heads/*:refs/remotes/origin/* # timeout=10> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 718528cb0b88c6712a1b805c540f8765f7c1315a (refs/remotes/origin/master)> git config core.sparsecheckout # timeout=10> git checkout -f 718528cb0b88c6712a1b805c540f8765f7c1315a # timeout=10
Commit message: "chmod build.sh"> git rev-list --no-walk 718528cb0b88c6712a1b805c540f8765f7c1315a # timeout=10
[golang-proj] $ /bin/sh -xe /tmp/jenkins9821548014139851283.sh
+ echo /var/jenkins_home/workspace/golang-proj
/var/jenkins_home/workspace/golang-proj
+ cd /var/jenkins_home/workspace/golang-proj
+ export GOPROXY=https://goproxy.cn,direct
+ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 /var/jenkins_home/go/bin/go build -o bin/main static-file-server/main.go
SSH: Connecting from host [b88e4cd5f58a]
SSH: Connecting with configuration [tencent] ...
SSH: EXEC: completed after 401 ms
SSH: Disconnecting configuration [tencent] ...
SSH: Transferred 2 file(s)
Finished: SUCCESS
查看jenkins內部:
查看服務器:
訪問: