gitlab上CI/CD的一些小實踐和理解

lightTrace發表於2018-10-19

前言

之前用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 "noshoestech@hotmail.com"

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的一套東西既可以規避人力重複的錯誤,又可以節省很多勞動力,還可以清晰的看到整個專案的迭代過程,我們都應該去擁抱它。

相關文章