首页 > 其他分享 >62-CICD持续集成工具-Jenkins构建Golang的web项目

62-CICD持续集成工具-Jenkins构建Golang的web项目

时间:2023-02-18 11:35:26浏览次数:41  
标签:web http CICD APP Golang done demo go root

实现 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 源代码

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins

  • 生成ssh密钥并上传到GitLab

[root@jenkins ~]#ssh-keygen
[root@jenkins ~]#cat .ssh/id_rsa.pub
ssh-rsa
...

复制到这里

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_02

  • 验证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 任务

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_03

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_04

[root@jenkins data]#tree script/
script/
├── http_demo_go.sh
└── spring-boot-helloworld.sh

0 directories, 2 files

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_05

  • 构建并确认

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_06

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_07

  • 验证

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_08

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_09

  • 升级测试

[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

再次构建

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_10

确认升级成功

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_11


我是moore,大家一起加油!

标签:web,http,CICD,APP,Golang,done,demo,go,root
From: https://blog.51cto.com/mooreyxia/6065141

相关文章

  • WEB开发中的页面跳转方法总结
    页面跳转可能是由于用户单击链接、按钮等触发的,也可能是系统自动产生的。页面自动跳转在WEB开发中经常用到,而且根据需求可以采用不同的跳转方式,比如提示操作信息后延时跳转......
  • Webpack中Loader和Plugin
    1.首先两者都是为了扩展webpack的功能2.Loader:webpack视一切文件为模块,但webpack原生只能解析js文件,如果想将其他文件也打包的话,就会用到loader。它只专注于转化文件......
  • golang运算符
    1.算术运算符算术运算符有:+,-,*,/,%,++,--1.1加号(+)表示正数数字相加字符串拼接packagemainimport( "fmt")funcmain(){ num1:=+4 num2:=8 str1:="abc......
  • Golang 的 cobra 是如何实现使用链表实现命令树的
    Golang的cobra是如何实现使用链表实现命令树的在cobra中,每个命令都是独立的。通过parent.AddCommand(children)的形式进行串连。varroot=&cobra.Command{}......
  • 一次web系统的nginx配置恢复
    前言:组里有一个小伙子,为了升级nodejs,安装各种库,把系统给搞崩溃了,无法登录。找运维人员也不行,最后的解决办法换一台机器。幸好原来只有一块盘,所以数据还在。 $mva/b/......
  • Java Web(二)MyBatis
    MyBatis一.MyBatis简介1.什么是MyBatisMyBatis是一款优秀的持久层框架,用于简化JDBC开发MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundati......
  • Vite 与 Webpack的区别
    1.什么是Vite?Vite是尤雨溪在开发vue3的时候开发的一个web开发构建工具。极速的服务启动:使用原生ESM文件,无需打包!轻量快速的热重载:无论应用程序大小如何,都......
  • 交互式Web前端开发最有用的WebGL框架
    JavaScript是创建Web最有用的编程语言之一,尤其是在WebGL库的支持下。有了WebGL,可以很方便地使用HTML5Canvas元素动态生成图形。因此,设计师和开发人员很容易创建流畅的2D......
  • 黑猫web端signature参数逆向分析
    适合小白练手一、断点调试1.查找关键字2.分析nn是一个随机数16位u是固定参数u="$d6eb7ff91ee257475%"时间戳vard=(newDate).getTime()3.随机数生成o......
  • 使用SpringBoot简单实现WebRTC群聊会议室(Mesh方案)
    近期需要做一个类似会议室功能,但网络上大多数是一对一通信,故记录分享希望帮助到有用的人WebRTC一对一聊天原理关于WebRTC建立一对一聊天的模板网上很多,可参考以下博客:spr......