gitlab上CI/CD的一些小实践和理解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sureSand/article/details/83186122

前言

之前用jenkins做的CI/CI,现在在新公司的业务CI/CD的流程是全部跑在gitlab上面,弄了一两天终于算是能完全跑通了,其实归根到底还是要把持续集成的部署给拆开成链式的,一步步执行,即使每次迭代一点点东西也可以使得逻辑依然清晰。

我这里主要记录三个主流程,代码只是部分展示,不能实践,只是简单的记录下自己的操作思路。

三个流程:

编译代码–打包成镜像推送到registry–远程让指定服务器拉取代码部署系统

.gitlab-ci.yml的编写

image: golang:1.9.2
#三个主流程分别是build,build_docker,deploy
stages:
  - build
  - build_docker
  - deploy
#设置最终推送的镜像名变量
variables:
  MAIN_IMAGE: registry.yun-ti.com/liweidong/picautoframe:$CI_COMMIT_REF_NAME

#编译go代码
build_main:
  stage: build
  only:
    - master@liweidong/picautoframe   #这里就是要编译的gitlab代码地址,only表示只有master或者 打了tag的推送才会触发
    - tags@liweidong/picautoframe
  artifacts:
    name: bin-files
    paths:
    - bin
    expire_in: 1 week
  script:
    - cd $GOPATH/src
    - ln -sf $CI_PROJECT_DIR picautoframe # 把工程目录映射到GOPATH的src文件夹下
    - cd picautoframe/pic/main
    - go build -o $CI_PROJECT_DIR/bin/picautoframe  #编译
  tags:
    - docker
#打包镜像
build_main_docker:
  image: docker:stable-dind
  only:
  - master@liweidong/picautoframe
  - tags@liweidong/picautoframe
  stage: build_docker
  services:
  - name: docker:stable-dind
    entrypoint: ["dockerd-entrypoint.sh", "--registry-mirror=http://f2d6cb40.m.daocloud.io"]
  before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD registry.yun-ti.com   #登陆registry
  script:
  - echo "build main image:"
  - docker build -t $MAIN_IMAGE -f ./deep.Dockerfile .    #根据Dockerfile生成镜像,deep.Dockerfile 我会在后面给出
  - docker push $MAIN_IMAGE   #推送镜像
  tags:
  - docker

#拉取镜像并运行
deploy_sys_test:
  image: centos:7
  only:
  - master@liweidong/picautoframe
  - tags@liweidong/picautoframe
  when: manual    #manual表示要手动操作,前面两步都是自动的
  stage: deploy
  variables:
    DEEP_CONTAINER_NAME: "picautoframe"
    ENV_DEEP_ADDR: "119.3.x.xxx"         #远程部署主机的ip
    ENV_DEEP_ENV_ROOT_PWD: *** # 密码
    ENV_DEEP_NAME: "picautoframe"
    ENV_DEEP_PORT: "21006"   #端口
    ENV_DEEP_HTTPS_PORT: "21007"
    ZYL_REG_PWD: "xxxx"   #登陆远程主机的密码
  environment:
    name: test_env
    url: 119.3.x.xxx   #远程部署主机的ip
  before_script:
  - echo "start deploy"
  - yum -y update
  - yum -y install epel-release
  - yum -y install fabric
  script:
  - echo "start deploy"
  - fab -f $CI_PROJECT_DIR/contrib/deploy_deep.py deploy   #用python的fabric远程部署 
  tags:
  - docker

deep.Dockerfile

FROM centos:7

MAINTAINER "[email protected]"

ENV TZ "Asia/Shanghai"

COPY bin/picautoframe /usr/local/bin/
COPY contrib/config.yml.tpl /etc/picautoframe/
COPY contrib/picautoframe.sh /usr/local/bin/

VOLUME /var/log/picautoframe

WORKDIR /usr/local/bin

picautoframe

简单的dockerfile,主要是复制编译后的代码到docker里面,挂载一个文件夹,然后直接运行二进制go文件

deploy_deep.py

from fabric.api import *
from fabric.contrib.files import *
import os


env.user = 'root'
env.password = os.environ.get('ENV_DEEP_ENV_ROOT_PWD')  #这些变量都是前面.gitlab-ci.yml已经设置好的
env.hosts = [os.environ.get('ENV_DEEP_ADDR')]
#先拉取镜像,然后运行
def deploy():
    run('docker login -u liweidong -p %s registry.yun-ti.com' % os.environ.get('ZYL_REG_PWD'))
    run('docker pull %s' % os.environ.get('MAIN_IMAGE'))
    with settings(warn_only=True):
        run('docker rm -f %s' % os.environ.get('DEEP_CONTAINER_NAME'))
# --net=host模式表示docker直接使用宿主机的端口和ip
    run( ('docker run -d --name=%s --net=host --restart=always'
          ' -v /var/log/picautoframe/picautoframe.log:/var/log/picautoframe/picautoframe.log'
          ' -e DEEP_NAME="%s" '
          ' -e DEEP_LISTEN_ADDR="%s" -e DEEP_HTTP_PORT=%s %s'
          ) % (os.environ.get('DEEP_CONTAINER_NAME'),
               os.environ.get('DEEP_CONTAINER_NAME'),
               os.environ.get('ENV_DEEP_ADDR'),
               os.environ.get('ENV_DEEP_PORT'),
               os.environ.get('MAIN_IMAGE')))

总结

其实看着文件脚本很多,就是就是简单的三步,根据流程去套脚本,当然还可以有更多的步骤,包括测试,代码检查等等,实际上还有很多有意思的脚本都可以选择性的写在里面,当然我现在理解的也不是很深,但是感觉CI/CD是个一劳永逸的过程,之前传统的部署方式确实让人头疼,大多数都是重复劳动,而且看不见迭代的过程,CI/CD的一套东西既可以规避人力重复的错误,又可以节省很多劳动力,还可以清晰的看到整个项目的迭代过程,我们都应该去拥抱它。

猜你喜欢

转载自blog.csdn.net/sureSand/article/details/83186122
今日推荐