简述:gitlab ci ,依赖runner 来执行 Pipelines,Pipelines包含对多个阶段中job的定义。html
第一步:安装runnerjava
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash sudo yum install gitlab-ci-multi-runner
第二步:注册runnergit
须要两个东西 :私服地址URL 和 TOKEN,能够在gitlab项目的设置->ci/cd中找到web
sudo gitlab-ci-multi-runner register 回车后按提示输出url 和 token 注意:经过gitlab-ci-multi-runner register注册的Runner配置会存储在/etc/gitlab-runner/config.toml中,若是须要修改可直接编辑该文件
注意事项:这里我本身对git-runner service 进行了 工做空间修改,这里要对注意一下新目录的权限问题apache
git-runner 配置详解api
第三步:在 gitlab 私服配置ci/di配置文件 gitlab-ci.ymltomcat
gitlab-ci.yml 官方详解bash
# 定义 stages,我暂时只定义了两个阶段 构建、发布 stages: - build - deploy # 定义 job build-job: stage: build script: - mvn clean compile # 定义 job deploy-job: stage: deploy script: - /data/script/deploy.sh
这里注意一下:咱们的项目是依赖maven 构建的,因此须要在runner的服务器上须要安装 maven服务器
附加一下我测试用的构建部分代码:为了匹配历史项目,正规项目跟进本身的需求修改构建代码app
<build> <finalName>freezing</finalName> <sourceDirectory>${basedir}/src/main/java</sourceDirectory> <outputDirectory>${deploy.path}/WEB-INF/classes/com/</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/webapp</directory> <excludes> <exclude>WEB-INF/**/*.*</exclude> <exclude>templates/default/z/**/*.*</exclude> </excludes> <targetPath>${deploy.path}/</targetPath> </resource> <resource> <directory>${basedir}/src/main/resources</directory> <excludes> <exclude>**/*.*</exclude> </excludes> </resource> <resource> <directory>${basedir}/src/main/config</directory> <excludes> <exclude>**/*.*</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <verbose /> <extdirs>${basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>process-resources</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${basedir}/src/main/webapp/WEB-INF/lib</outputDirectory> <overWriteSnapshots>true</overWriteSnapshots> </configuration> </execution> </executions> </plugin> </plugins> </build>
附加剧启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: '$tomcatpath pid=`ps aux | grep $tomcatpath | grep -v grep | awk '{print $2}'` echo 'exist pid:'$pid if [ -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 } else echo ===========startup.sh============== $sname start fi
第四步:验证
经过push代码,触发工做流,而后查看运行日志
这篇文章是很基础的操做,让你快速感觉它的简单易用,高级功能能够参考官方文档,或者后期可能会更新一下博客
补充几个小脚本:遍历更新文件的时间和参照文件作比较来判断是否须要重启tomcat、检测某个地址是否可用
#! /bin/bash deploy_path="/data/script/deploy.sh" #遍历文件夹 res=0 #默认类未更新不须要重启tomcat function 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 $1 echo $res
#!/bin/bash testapi='http://www.cn-healthcare.com/freezing' urlstatus=$(curl -s -m 5 -IL $testapi|grep 200) if [ "$urlstatus" == "" ];then echo "testapi result is error" return error else echo "testapi result is right:"$urlstatus fi
补充:代码须要同步到多台机器,可能须要用到,各个机器之间免密登陆
配置服务器之间的ssh登陆
三台服务器的ip分别是:
192.168.0.101,
192.168.0.102,
192.168.0.103
gitlab-runner安装的服务器为101,另外两台为应用服务器
因此要创建 gitlab-runner用户与102和103之间的root ssh免密码登陆
1.先在101服务器切换gitlab-runner用户
su gitlab-runner
2.使用ssh-keygen -t rsa
生成ssh的公钥和私钥
ssh-keygen -t rsa #回车以后3次回车便可
会在 /home/gitlab-runner/.ssh目录下发现2个文件
id_rsa.pub 和id_rsa
3.而后在应用服务器root用户,重复上述操做,这样 root用户的ssh的公钥和私钥也生成了,接下来就是将gitlab-runner用户的公钥写入root用户的authorized_keys文件中
cat /home/gitlab-runner/.ssh/id_rsa.pub >>/root/.ssh/authorized_keys
4.重启ssh service ssh restart
5.先切换到gitlab-runner用户 su gitlab-runnner
6.使用ssh登陆root用户 ssh root@192.168.56.102
你会发现你已经切换到了root用户了
注意 第一次链接会提示yes/no, 输入yes便可