服务器版本
# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.8.2003 (Core)
Release: 7.8.2003
Codename: Core
1、gitlab runner
#下载最新的Runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
#安装
sudo yum install gitlab-runner
#修改Runner权限为root 否则在同步代码事会报错
sudo gitlab-runner uninstall # 删除gitlab-runner用户
gitlab-runner install --working-directory /home/gitlab-runner --user root # 安装并设置--user(设置为root)
sudo service gitlab-runner restart # 重启gitlab-runner
ps aux|grep gitlab-runner # 查看当前runner用户
2、编译工具(视开发语言而定),本文为Java工程,编译打包工具为maven
配置Gitlab Runner
#注册
sudo gitlab-runner register
#输入gitlab url
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
#输入您获得的注册Runner的token
Please enter the gitlab-ci token for this runner
xxx
#输入Runner的描述
Please enter the gitlab-ci description for this runner
[hostame] my-runner
#输入与Runner关联的标签,
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag
#选择执行程序
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
输入参数说明:
url:打开gitlab上Project页面,点击Settings》CI/CD》Runners》Expand(见下图)
token:同url
runner描述:简要说明该runnner用途方便记忆
tags: 关联任务标签(任务名称)
配置CI/CD
在工程根目录下创建.gitlab-ci.yml文件或在gitlab上工程打开"CI/CD configuration"进入.gitlab-ci.yml 。
OwnerApiServiceDev:
script:
- chmod 777 test.sh
- ./test.sh
stage: deploy
only:
- master
tags:
- OwnerApiService
test.sh也放在工程根目录中
#linux上maven打包后的项目war包地址
war_path="./target/Owner_service.war"
#linux上tomcat的webapps的项目地址
pro_path="/usr/local/my_tomcat/tomcat/webapps"
#linux上tomcat的bin执行地址
tom_bin="/usr/local/my_tomcat/tomcat/bin"
#3.停止tomcat
echo "shutdown...tomcat...begin..."
sh $tom_bin"/shutdown.sh"
echo "shutdown...tomcat...end..."
#2.maven清除并且重新打包项目
echo "build...war...begin..."
mvn clean package -P dev
echo "build...war...end..."
#4.替换原有项目包
rm -rf $pro_path"/Owner_service"
mv $pro_path"/Owner_service.war" $pro_path"/Owner_service"$(date +%Y%m%d%H%M%S)".war.bak"
mv $war_path $pro_path
#5.启动tomcat
echo "startup...tomcat...begin..."
sh $tom_bin"/startup.sh"
echo "startup...tomcat...end..."
提交工程代码到 master分支则会完成自动部署。
以上讲解已Java工程为例,其他工程修改test.sh脚本流程即可。