部署Go项目简介
方案一
• 开发者本地环境需要将环境变量文件改为正式环境配置
• 编译成可执行文件
• 发送给运维
• (运维)将文件覆盖为线上
• (运维)重启进程
(可谓“又臭又长”)
方案二
开发者将代码commit到gitlab上交给运维同学
(运维)pull代码
(运维)编译成可执行文件
(运维)覆盖线上文件
(运维)重启进程
部署GitLab环境
部署gitlab请看我前面写的文章
https://www.cnblogs.com/you-men/p/13126530.html
部署Jenkins环境
环境清单
* CentOS 7.3
* git-2.9.5.tar.gz
* Jenkins 2.203.war
* apache-tomcat-9.0.12.tar.gz
* jdk-8u151-linux-x64.tar.gz
* apache-maven-3.5.4-bin.tar.gz
初始化
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/backup
curl -o /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo &>/dev/null
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install ntpdate
ntpdate -b ntp1.aliyun.com
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config
sed -i '/^#UseDNS/ {s/^#//;s/yes/no/}' /etc/ssh/sshd_config
下载git.tar包安装
# Jenkins支持的git最低版本是1.8,使用rpm安装版本是1.8,这里采用源码安装使用2.9版本
# git官网下载最新版本GIT:
# https://mirrors.edge.kernel.org/pub/software/scm/git/
# 安装git 依赖包
yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
rpm -e git --nodeps # 卸载掉老版本git
tar xvf git-2.9.5.tar.gz -C /usr/local/src/
cd /usr/local/src/git-2.9.5/
make prefix=/usr/local/git all
make prefix=/usr/local/git install
# 修改/etc/bashrc配置文件末尾加入下面一行环境变量(建议针对用户生效,否则使用其他用户时候容易出问题)
tail -1 /etc/bashrc
export PATH=$PATH:$HOME/bin:/usr/local/git/bin
source /etc/bashrc
# 测试是否安装成功
git --version
git version 2.9.5
JDK环境部署
# jenkins对jdk版本有要求,不能用jdk9和10,且需要将系统自带的java环境清楚掉
# jdk下载地址
# http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html
# 卸载系统自带的java环境
[root@jenkins-8 git-2.9.5]# rpm -qa |grep jdk
[root@jenkins-8 git-2.9.5]# java
bash: java: 未找到命令
# 解压安装jdk
tar xvf jdk-8u151-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
mv jdk1.8.0_151/ jdk
# 修改/etc/bashrc配置文件,末尾加入下面一行环境变量.
tail -2 /etc/bashrc
JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
source /etc/bashrc
java -version # 尽量不要用openjdk
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
部署jenkins
# war包部署
# cp jenkins.war /usr/local/tomcat/webapps/
# 停掉tomcat在启动一遍
# 将Jenkins的war放到tomcat的webapps下面,重启一下tomcat自己就解压了,但是访问可能有点慢
# 也可以通过jar包直接启动jenkins
java \ -Dcom.sun.management.jmxremote \ -Dcom.sun.management.jmxremote.port=12345 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ -Djava.rmi.server.hostname="192.168.8.2 " \ -jar jenkins-2.138.3.war &
# 也可以用jenkins的rpm包安装
wget https://pkg.jenkins.io/redhat-stable/jenkins-2.222.1-1.1.noarch.rpm
rpm -ivh jenkins-2.150.1-1.1.noarch.rpm
# Jenkins配置
grep -v "#" /etc/sysconfig/jenkins | grep -v "^$"
JENKINS_HOME="/var/lib/jenkins"
JENKINS_JAVA_CMD=""
JENKINS_USER="root"
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname="149.129.38.117" "
JENKINS_PORT="8080"
JENKINS_LISTEN_ADDRESS=""
JENKINS_HTTPS_PORT=""
JENKINS_HTTPS_KEYSTORE=""
JENKINS_HTTPS_KEYSTORE_PASSWORD=""
JENKINS_HTTPS_LISTEN_ADDRESS=""
JENKINS_DEBUG_LEVEL="5"
JENKINS_ENABLE_ACCESS_LOG="no"
JENKINS_HANDLER_MAX="100"
JENKINS_HANDLER_IDLE="20"
JENKINS_ARGS=""
# 如果启动报错一般是你java不是openjdk的,修改下java路径即可
vim /etc/init.d/jenkins
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
/usr/local/jdk/bin/java # 加上你的Java路径即可
"
cat /root/.jenkins/secrets/initialAdminPassword # 这个密码以后可能会用到最后记下来,这个文件往后初始化就没有了
2775b0adf72e42a5bdea22297b81dd82
# 2> 复制密码上面进入下一步会出现一个页面安装插件图2,
# 我们不弄太复杂,就安装推荐的插件,
# 安装时间较长,请准备良好的网络环境,可能需要翻墙
/# 可能因为网络波动+jenkins源是国外的,会安装失败,重试几次,
# 如果还不行换服务器,比如云服务器,或者换源,图3
至此,就可以开始使用Jenkins了
**
**
创建配置Gitlab项目
新建project
Gitlab仓库添加jenkins主机的公钥
关掉Gitlab默认的Devops管道
创建配置jenkins项目
创建jenkins项目
填写gitlab的git地址
添加jenkins凭据
此处凭据写jenkins的私钥
保存测试jenkins项目拉取
jenkins配置go环境和插件
安装go环境
tar xf go1.15.6.linux-amd64.tar.gz -C /usr/local/
mkdir /home/app
tail -6 /etc/profile
export GOROOT=/usr/local/go
export GOPATH=/home/app/GoProject
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
export GOPROXY=https://goproxy.cn,direct
source /etc/profile
golang1.13可以直接进行
# 七牛云
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
# 阿里云
go env -w GO111MODULE=on
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
验证Go结果
[root@jenkins src]# go version
go version go1.15.6 linux/amd64
创建go测试代码
[root@jenkins ~]# mkdir -p /home/app/GoProject/src
[root@jenkins src]# git clone [email protected]:root/go_test1.git
[root@jenkins src]# cd /home/app/GoProject/src/go_test1
[root@jenkins test1]# go mod init go_test1
[root@jenkins test1]# cat main.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200,gin.H{
"message": "Hello World",
})
})
r.Run(":10080")
}
[root@jenkins go_test1]# go run main.go
[root@jenkins ~]# curl localhost:10080
{"message":"Hello World"}
上传代码到gitlab项目
[root@jenkins go_test1]# git add .
[root@jenkins go_test1]# git config --global user.email "[email protected]"
[root@jenkins go_test1]# git config --global user.name "admin"
[root@jenkins go_test1]# git commit -m "add main.go v0.1"
[master 8492f92] add main.go v0.1
3 files changed, 57 insertions(+)
create mode 100644 go.mod
create mode 100644 go.sum
create mode 100644 main.go
[root@jenkins go_test1]# git push
Counting objects: 5, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 2.36 KiB | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To 192.168.43.235:root/go_test1.git
4dbaa3a..8492f92 master -> master
配置jenkins的Go全局工具
配置jenkins构建参数
构建Go项目并运行测试
配置Nginx反代访问服务
安装配置Nginx
yum -y install nginx
vim /etc/nginx/nginx.conf
location / {
proxy_pass http://localhost:10080;
}
nginx