基于gitlab-CI的持续集成与部署

 

一、gitlab-ci原理流程

gitlab-ci是gitlab为持续集成、持续部署提供的一个工具,主要为当利用git客户端做commit或tag操作提交代码时会触发项目工程根目录下的.gitlab-ci.yml文件里的代码shell脚本,从而实现利用脚本做具体的做操,细则见http://10.29.139.47/help/ci/quick_start/README。

具体执行流程图如下:

 

二、常见java应用gitlab-ci文件编写实例:

对于一般的工程CI文件分为四大步,分别为创建jar包(jarBuild)、创建镜像(imageBuild)、将镜像推送到镜像仓库(imagePush)和将镜像部署到远程服务器上(imageProcess)。


 

stages: #定义整个流程的大步骤

- jarBuild #创建jar包

- imageBuild #创建镜像

- imagePush #push到镜像仓库

- imageProcess #镜像部署

jarBuild-zhangyitong:

script:

#更新代码,切换分支

- cd /srv/gitlabci_project

- if [ ! -d "/srv/gitlabci_project/zhangyitong" ]; then

- git clone http://10.29.139.47/yaoxiaoqi/zhangyitong.git

- else

- echo "zhangyitong have existed"

- fi

- cd zhangyitong

- git checkout develop

- git pull

#创建jar包

- mvn clean install -X

- cd /srv/gitlabci_project/zhangyitong/employee-component-api && mvn clean deploy

- cd /srv/gitlabci_project/zhangyitong/employee-component && mvn clean package -Dmaven.test.skip=true

stage: jarBuild

tags:

- image-build

only:

- develop



dockerBuild-zhangyitong:

script:

- cp /srv/gitlabci_project/zhangyitong/employee-component/target/employee-component.jar /srv/gitlabci_project/zhangyitong/build-deploy

- cd /srv/gitlabci_project/zhangyitong/build-deploy

- docker build -t zhangyitong . #创建名为zhangyitong的镜像

- docker tag zhangyitong 10.29.139.47:5000/develop/zhangyitong:latest #给镜像打tag

#将配置文件提交到工程上去

- if [ ! -d "/srv/gitlabci_project/config_devuat" ]; then

- cd /srv/gitlabci_project

- git clone http://10.29.139.47/yuanwu/config_devuat.git

- fi

- cp -r /srv/gitlabci_project/zhangyitong/build-deploy/env_config /srv/gitlabci_project/config_devuat/marketing

- cd /srv/gitlabci_project/config_devuat

- git pull

- pres=$(git status)

- if [[ $pres =~ "nothing to commit" ]]; then

- echo "nothing changed"

- else

- git add .

- git commit -a -m "marking config changed"

- git push

- fi



stage: imageBuild

tags:

- image-build

only:

- develop

dockerPush-zhangyitong:

script:

#登录docker镜像仓库,并将镜像push到仓库

- docker login -u ${REGISTRY_NAME} -p ${REGISTRY_PSD} ${REGISTRY_IP}:5000

- docker push 10.29.139.47:5000/develop/zhangyitong:latest

stage: imagePush

tags:

- image-push

only:

- develop

dockerRun-zhangyitong:

script:

#利用ansible远程部署,其中zhangyitong-dev 映射到要部署机器的ip,“”双引号中内容为远程机器上执行的命令

- ansible zhangyitong-dev -m command -a "docker login -u ${REGISTRY_NAME} -p ${REGISTRY_PSD} ${REGISTRY_IP}:5000" #应用服务器登录注册中心

- ansible zhangyitong-dev -m command -a "docker pull ${REGISTRY_IP}:5000/develop/zhangyitong:latest" #应用服务拉取镜像

- ansible zhangyitong-dev -m copy -a "src=/srv/gitlabci_project/zhangyitong/build-deploy/env_config/zhangyitong_develop dest=/tmp/zhangyitong" #分发docker-compose文件到应用服务器

- ansible zhangyitong-dev -m copy -a "src=/srv/gitlabci_project/zhangyitong/employee-component/target/employee-component.jar dest=/tmp/zhangyitong"

- ansible marketing-dev -m copy -a "src=/srv/gitlabci_project/marketing/build-deploy/rm.sh dest=/tmp/marketing_develop"

- ansible marketing-dev -m command -a "cd /tmp/marketing_develop && chmod u+x rm.sh && ./rm.sh"

- ansible zhangyitong-dev -m command -a "docker-compose -f /tmp/zhangyitong/zhangyitong_develop/docker-compose.yml up -d" #应用服务器运行docker-compose

stage: imageProcess

tags:

- image-push

only:

- develop

你可能感兴趣的:(dorcker,gitlab)