jenkins系列-05-jenkins构建golang程序

下载go1.20.2.linux-arm64.tar.gz 并存放到jenkins home目录:

写一个golang demo程序:静态文件服务器:https://gitee.com/jelex/jenkins_golang

go 复制代码
package main

import (
	"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 8888

	8888

exit 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 string

	flag.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

bash 复制代码
#局部变量(执行文件名称), 根据自己项目写
project_name="file-srv"
#杀掉之前正在运行的程序
go_id=`ps -ef|grep "./${project_name}" |grep -v "grep" | awk '{print $2}'`
if [ -z "$go_id" ];
then
    echo "[go pid not found]"
else
	#杀掉进程
    kill -9 $go_id
    echo "killed $go_id"
fi

echo "clean old file"
rm -rf ${project_name}
#执行日志,根据自己项目情况可选
rm -rf ${project_name}.log

if [ -f main ]; then
    echo "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

构建步骤:

bash 复制代码
#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架构的服务器,如此处理。。。

构建后操作:

构建:

控制台输出:

bash 复制代码
Started by user root

Running 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内部:

查看服务器:

访问:

相关推荐
sun00770030 分钟前
ubuntu dpkg 删除安装包
运维·服务器·ubuntu
吃肉不能购2 小时前
Label-studio-ml-backend 和YOLOV8 YOLO11自动化标注,目标检测,实例分割,图像分类,关键点估计,视频跟踪
运维·yolo·自动化
学Linux的语莫2 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible
qq_312920113 小时前
docker 部署 kvm 图形化管理工具 WebVirtMgr
运维·docker·容器
学Linux的语莫3 小时前
搭建服务器VPN,Linux客户端连接WireGuard,Windows客户端连接WireGuard
linux·运维·服务器
黑牛先生3 小时前
【Linux】进程-PCB
linux·运维·服务器
Karoku0663 小时前
【企业级分布式系统】ELK优化
运维·服务器·数据库·elk·elasticsearch
安迁岚4 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
打码人的日常分享4 小时前
商用密码应用安全性评估,密评整体方案,密评管理测评要求和指南,运维文档,软件项目安全设计相关文档合集(Word原件)
运维·安全·web安全·系统安全·规格说明书