gitlab的ci功能测试之旅

简述:gitlab ci ,依赖runner 来执行 Pipelines,Pipelines包含对多个阶段中job的定义。

第一步:安装runner

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

第二步:注册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

# 定义 job
build-job:
  stage: build
  script:
    - mvn clean compile

# 定义 job
deploy-job:
  stage: deploy
  script:
    - /data/script/deploy.sh

这里注意一下:我们的项目是依赖maven 构建的,所以需要在runner的服务器上需要安装 maven

附加一下我测试用的构建部分代码:为了匹配历史项目,正规项目跟进自己的需求修改构建代码

<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

 

转载于:https://my.oschina.net/haitaohu/blog/3031846

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值