实现 Golang 应用源码编译并部署
- 安装 Golang 环境
#编译安装
[root@jenkins ~]#cat install_go.sh
#!/bin/bash
GO_VERSION=1.18.4
URL=https://studygolang.com/dl/golang/go${GO_VERSION}.linux-amd64.tar.gz
#URL=https://golang.google.cn/dl/go${GO_VERSION}.linux-amd64.tar.gz
INSTALL_DIR=/usr/local
GOPATH_DIR=/opt/go
. /etc/os-release
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
install_go () {
if ! [[ $ID_LIKE =~ debian|rhel ]];then
color "不支持此操作系统,退出!" 1
exit
fi
if [ ! -f go${GO_VERSION}.linux-amd64.tar.gz ] ;then
wget $URL
fi
[ ! -d ${INSTALL_DIR} ] && mkdir -pv ${INSTALL_DIR}
tar xf go${GO_VERSION}.linux-amd64.tar.gz -C ${INSTALL_DIR} || { color "Go 解压缩失败!" 1;exit; }
cat > /etc/profile.d/go.sh <<EOF
export GOROOT=${INSTALL_DIR}/go
export PATH=$PATH:\$GOROOT/bin
EOF
. /etc/profile.d/go.sh
ln -s ${INSTALL_DIR}/go/bin/* /usr/local/bin/
go version
if [ $? -eq 0 ] ;then
color "Golang 安装成功!" 0
else
color "Golang 安装失败!" 1
exit 1
fi
}
config_go() {
cat >> /etc/profile.d/go.sh <<EOF
export GOPATH=${GOPATH_DIR}
EOF
[ ! -d ${GOPATH_DIR} ] && mkdir -pv ${GOPATH_DIR}/src
. /etc/profile.d/go.sh
go env -w GOPROXY=https://goproxy.cn,direct
}
hello_go () {
mkdir -pv ${GOPATH_DIR}/src/hello
cd ${GOPATH_DIR}/src/hello
cat > hello.go <<EOF
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
EOF
go mod init
go build
./hello
}
install_go
config_go
hello_go
exec bash
[root@jenkins ~]#go version
go version go1.18.4 linux/amd64
[root@web01 hello]#go version
go version go1.18.4 linux/amd64
[root@web02 hello]#go version
go version go1.18.4 linux/amd64
- 准备 Golang 源代码
- 生成ssh密钥并上传到GitLab
[root@jenkins ~]#ssh-keygen
[root@jenkins ~]#cat .ssh/id_rsa.pub
ssh-rsa
...
复制到这里
- 验证jenkins是否可以基于SSH密钥拉取代码
[root@jenkins data]#git clone [email protected]:myprj/http_demo_go.git
Cloning into 'http_demo_go'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.
[root@jenkins data]#tree http_demo_go/
http_demo_go/
├── go.mod
├── hello.html
└── main.go
0 directories, 3 files
- 编写自动化部署Shell脚本
#部署对象服务器提前建立好数据文件夹
[root@web01 ~]#mkdir -p /data
[root@web02 ~]# mkdir -p /data
#Jenkins服务器上编写自动化脚本
[root@jenkins script]#vim http_demo_go.sh
[root@jenkins script]#cat http_demo_go.sh
#!/bin/bash
APP=http_server_demo
APP_PATH=/data
DATE=`date +%F_%H-%M-%S`
HOST_LIST="
192.168.11.202
192.168.11.203
"
#最新版Go编译依赖环境变量GOCACHE,如果缺失就手动加入
export GOCACHE="/root/.cache/go-build"
build () {
CGO_ENABLED=0 go build -o $APP
}
deloy () {
for host in $HOST_LIST;do
ssh $host "mkdir -p $APP_PATH/${APP}-${DATE}"
scp -r hello.html ${APP} $host:$APP_PATH/${APP}-${DATE}/
ssh $host "killall -0 ${APP} &>/dev/null && killall -9 $APP;rm -f ${APP_PATH}/${APP} && \
ln -s ${APP_PATH}/${APP}-${DATE} ${APP_PATH}/${APP}; \
cd ${APP_PATH}/${APP}/ && ./${APP}" &
done
}
build
deloy
- 创建 Jenkins 任务
[root@jenkins data]#tree script/
script/
├── http_demo_go.sh
└── spring-boot-helloworld.sh
0 directories, 2 files
- 构建并确认
- 验证
- 升级测试
[root@gitlab data]#git clone [email protected]:myprj/http_demo_go.git
Cloning into 'http_demo_go'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.
[root@gitlab data]#cd http_demo_go/
[root@gitlab http_demo_go]#ls
go.mod hello.html main.go
[root@gitlab http_demo_go]#vim main.go
[root@gitlab http_demo_go]#git branch
* master
[root@gitlab http_demo_go]#git commit -am 'v1.0'
[master 07c8b7a] v1.0
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab http_demo_go]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/http_demo_go.git
99131f2..07c8b7a master -> master
[root@gitlab http_demo_go]#git log
commit 07c8b7a596899a5114a49a9eb9ee432334b4c7de (HEAD -> master, origin/master, origin/HEAD)
Author: root <[email protected]>
Date: Sat Feb 18 03:06:07 2023 +0000
v1.0
commit 99131f252ad74aee49aaffbe4fae7e5beea5fee8
Author: 南山 <[email protected]>
Date: Sun Jul 24 11:52:46 2022 +0800
v1.0
再次构建
确认升级成功
我是moore,大家一起加油!
标签:web,http,CICD,APP,Golang,done,demo,go,root From: https://blog.51cto.com/mooreyxia/6065141