Gitlab CI集成Gitlab Runner

关于Gitlab CI与Gitlab Runner的概念自行百度,此处不做赘述;Gitlab8.0之后已经集成了Gitlab CI;集成环境:CentOS6.5+Gitlab8.2.3-4+Gitlab Runner1.10.7-1


1、添加GitLab官方库

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash

2、安装Gitlab Runner(若安装其他版本,请参考官方文档)

yum install gitlab-ci-multi-runner-1.10.7-1

3、注册Gitlab Runner(Gitlab CI的url和token请从自己的gitlab里面的项目里的runners模块里面查看)

[root@localhost ~]# gitlab-ci-multi-runner register
Running in system-mode.                            

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.4.241:81/ci
Please enter the gitlab-ci token for this runner:
c5552857cfa2bd91c7b69507c8aec5
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag
Whether to run untagged builds [true/false]:
[false]: true
Registering runner... succeeded                     runner=c5552857
Please enter the executor: parallels, shell, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, ssh, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
[root@localhost ~]# 

4、启动Gitlab Runner

gitlab-ci-multi-runner run

此时可以在你们项目里看到刚刚添加的runner

Gitlab CI集成Gitlab Runner_第1张图片

5、在项目根目录添加.gitlab-ci.yml文件,Gitlab Runner运行全靠它(具体配置参考官方文档)

# Define commands that run before each job's script
before_script:
  - mvn clean

# Define build stages
stages:
  - deploy_test
  #- deploy_stage

# Define job
deploy_test_job:
  stage: deploy_test
  only:
      - develop
  script:
    # You can also list all environment variables with the export command
    #- export
    - mvn install -Ptest -DskipTests
    - sh /data/apps/tomcat-instance/deploy_test.sh

deploy_test.sh

#!/bin/sh
export JAVA_HOME=/data/apps/jdk1.8.0_111
export JRE_HOME=/data/apps/jdk1.8.0_111/jre
export CATALINA_HOME=/data/apps/tomcat-8.5.8
export CATALINA_BASE=/data/apps/tomcat-instance/jgj-api
war=/data/target/*.war
bin=$(cd `dirname $0`; pwd)
echo "Deploy ${war##*/}..."
rm -rf ${bin}/jgj-api/webapps/*
rm -rf ${bin}/jgj-api/logs/*
cp ${war} ${bin}/jgj-api/webapps/
echo "Restart tomcat.."
pid=$(lsof -t -i:8098)
if [ ! $pid ]; then
  echo "jgj-api pid not exist"
else
  echo "jgj-api pid is ${pid}"
  kill -9 $pid
fi
sleep 1

如果要部署其他环境可以通过ssh无密码登录实现参考文档


这里有一个弱点,至今没找到解决办法,就是在deploy_test.sh脚本最后直接启动tomcat,启动不起来,应该是跟jenkins一样,jenkins执行完job后会杀死其他进程,只不过jenkins有解决办法,Gitlab Runner没找到,各位大侠如果有办法,欢迎留言!


更多配置请参考官方文档

你可能感兴趣的:(Linux,脚本,gitlab)