简述:gitlab ci ,依赖runner 来执行 Pipelines,Pipelines包含对多个阶段中job的定义。
第一步:安装runner
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bashsudo yum install gitlab-ci-multi-runner
第二步:注册runner
需要两个东西 :私服地址URL 和 TOKEN,可以在gitlab项目的设置->ci/cd中找到
sudo gitlab-ci-multi-runner register回车后按提示输出url 和 token注意:通过gitlab-ci-multi-runner register注册的Runner配置会存储在/etc/gitlab-runner/config.toml中,如果需要修改可直接编辑该文件
注意事项:这里我自己对git-runner service 进行了 工作空间修改,这里要对注意一下新目录的权限问题
git-runner
第三步:在 gitlab 私服配置ci/di配置文件 gitlab-ci.yml
gitlab-ci.yml
# 定义 stages,我暂时只定义了两个阶段 构建、发布stages: - build - deploy# 定义 jobbuild-job: stage: build script: - mvn clean compile# 定义 jobdeploy-job: stage: deploy script: - /data/script/deploy.sh
这里注意一下:我们的项目是依赖maven 构建的,所以需要在runner的服务器上需要
附加一下我测试用的构建部分代码:为了匹配历史项目,正规项目跟进自己的需求修改构建代码
freezing ${basedir}/src/main/java ${deploy.path}/WEB-INF/classes/com/ ${basedir}/src/main/webapp WEB-INF/**/*.* templates/default/z/**/*.* ${deploy.path}/ ${basedir}/src/main/resources **/*.* ${basedir}/src/main/config **/*.* maven-compiler-plugin 1.7 1.7 ${basedir}/src/main/webapp/WEB-INF/lib org.apache.maven.plugins maven-dependency-plugin copy-dependencies process-resources copy-dependencies ${basedir}/src/main/webapp/WEB-INF/lib true
附加重启tomcat脚本
#!/bin/bash# tomcat 目录p='/data/tomcat7'# tomcat 服务地址sname='/etc/init.d/tomcat7'work=${p}'/work/'`rm -rf ${work}`tomcatpath=${p}'/bin'echo 'operate restart tomcat: '$tomcatpathpid=`ps aux | grep $tomcatpath | grep -v grep | awk '{print $2}'`echo 'exist pid:'$pidif [ -n "$pid" ]then{ echo ===========shutdown================ $sname stop sleep 2 pid=`ps aux | grep $tomcatpath | grep -v grep | awk '{print $2}'` if [ -n "$pid" ] then { sleep 2 echo ========kill tomcat begin============== echo $pid kill -9 $pid echo ========kill tomcat end============== sleep 2 echo ===========startup.sh============== $sname start } else $sname start fi }elseecho ===========startup.sh==============$sname startfi
第四步:验证
通过push代码,触发工作流,然后查看运行日志
这篇文章是很基础的操作,让你快速感受它的简单易用,高级功能可以参考官方文档,或者后期可能会更新一下博客
补充几个小脚本:遍历更新文件的时间和参照文件做比较来判断是否需要重启tomcat、检测某个地址是否可用
#! /bin/bashdeploy_path="/data/script/deploy.sh"#遍历文件夹res=0 #默认类未更新不需要重启tomcatfunction read_dir(){ for file in `ls $1` #注意此处这是两个反引号,表示运行系统命令 do if [ -d $1"/"$file ] #注意此处之间一定要加上空格,否则会报错 then read_dir $1"/"$file else res=$(compareTime $1"/"$file $deploy_path) if [ $res == 1 ] then break fi fi done}#比较文件修改时间function compareTime(){ newer=`find $1 -newer $2` if [ "$newer" == "$1" ] then echo 1 return 1 #$1 大于 $2 else echo 0 return 0 # 相反 fi}#读取第一个参数read_dir $1echo $res
#!/bin/bashtestapi='http://www.cn-healthcare.com/freezing'urlstatus=$(curl -s -m 5 -IL $testapi|grep 200)if [ "$urlstatus" == "" ];then echo "testapi result is error" return errorelse echo "testapi result is right:"$urlstatusfi