• GitRunner+Docker镜像+k8s自动化部署


    GitRunner 搭起来不难,但也不是特别简单,为了避免少走弯路,现在整理一套Git Runner的操作文档

    gitlab-ci配置

     

    1.安装gitlab-runner服务

     

    1.1.windows本机安装

     

    windows64位下载地址:https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe

     
    1.  
      下载gitlab-runner.exe程序
       
    2.  
      安装gitlab-runner服务 gitlab-runner.exe install
       

    1.2.docker内安装

     
    1.  
      安装docker
       
    2.  
      拉取gitlab-runner镜像docker pull gitlab/gitlab-runner
       
    3.  
      基于gitlab-runner镜像运行容器docker run -d --name gitlab-runner --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
       

    2.注册runner

     

    注册gitlab-runner有手动设置runner和群组runner,群组runner即群组内所有项目共享,只有群组的拥有者能看到注册令牌注册runner的命令执行的环境根据安装环境而定

     
     
    1.  
      执行注册命令 gitlab-runner register
       
    2.  
      输入gitlab地址 https://gitlab.colipu.com/
       
    3.  
      输入注册令牌,从CI/CD设置中获取 xxxxxxxxxxxx
       
    4.  
      输入runner的描述 xxxxxx
       
    5.  
      输入runner的标签 xxxxxx
       
    6.  
      输入Runner执行者 docker
       
    7.  
      输入执行程序的默认镜像 alpine:latest
       

    注:如果是docker容器方式需要执行以下操作

     
    1.  
      进入gitlab-runner的docker容器内
       
    2.  
      修改config.toml配置文件vim /etc/gitlab-runner/config.toml
       
    3.  
      修改为volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"]
       
     
     
     
     
     
     
     

    成功后即可在项目的CI/CD设置中看到,如图:

     
     
     
     
     
     
     
     

    3.添加gitlab-ci.yml

     

    在项目根目录添加gitlab-ci.yml配置文件设置,详见链接

     

    内容参考如下:

     
    image: registry.cn-shanghai.aliyuncs.com/clp-test/it-docker:docker-190301
    services:- name: docker:19.03.1-dind
    stages:  - build  - deploy  - sync
    build-b2bwxweb:  image: registry.cn-shanghai.aliyuncs.com/clp-test/it-node-10.16.1-alpine:201911261839  stage: build  # tags:  #   - docker  except: # 排除dev分支    - dev  artifacts:    paths:      - dist/  script:    - npm run build
    deploy-b2bwxweb:  stage: deploy  # tags:  #   - docker  variables:    REGISTRY_HOST: registry.cn-shanghai.aliyuncs.com    DOCKER_TLS_CERTDIR: ''    DOCKER_HOST: tcp://docker:2375/    DOCKER_DRIVER: overlay2
      except: # 排除dev分支    - dev  when: # 手动触发    manual  allow_failure: false  script:    - export ENV_NAME="dev"    - if [ "${CI_COMMIT_REF_NAME}" = "master" ]; then ENV_NAME="uat"; fi;- if [ "${CI_COMMIT_REF_NAME}" = "test" ]; then ENV_NAME="test"; fi;- export IMAGE_NAME=${REGISTRY_HOST}/clp-${ENV_NAME}/web-b2bwx-web:${CI_COMMIT_SHORT_SHA}-$(date +%Y%m%d%H%M)- export IMAGE_LATEST=${REGISTRY_HOST}/clp-${ENV_NAME}/web-b2bwx-web:latest- docker login -u ${USER_NAME}-p ${USER_PWD} ${REGISTRY_HOST}- docker build -t ${IMAGE_NAME} .- docker tag ${IMAGE_NAME} ${IMAGE_LATEST}- docker push ${IMAGE_NAME}- docker push ${IMAGE_LATEST}  after_script:# - cd /dist- ls- docker imagessync-b2bwxweb:stage: sync    only:- test- master    script:- curl https://cs.console.aliyun.com/hook/trigger?token=$K8S_TOKEN
     

    注意: 在部署GatewayApi的时候镜像打包的有问题,后来发现是生成的镜像环境有问题,所以在部署docker镜像设置环境变量时要注意:(设置环境时一定要和build在同一行不然会无效)

     
        - mkdir -p build/linux/config    - cp config/app.ini.sample build/linux/config/app.ini    - go mod tidy    - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags=jsoniter -ldflags "-s -w" -o build/linux/gatewayapi
     

    4.执行流水线

     

    添加了ci配置文件后默认在推送代码时会创建一条基于当前推送版本的流水线,如果设置了自动执行的管道作业则推送后立即执行,一般设置打包推送镜像的作业设置为手动执行,则需要在流水线列表内手动运行该作业

     
     
     
    variables:
      REGISTRY: "registry.cn-shanghai.aliyuncs.com"
      DOCKER_IMAGE_NAME: "web-gateway-api"
      TEST_BRANCH_NAME: "test"
      TEST_BRANCH_REGISTRY_ENVIRONMENT: "clp-test"
      MASTER_BRANCH_NAME: "master"
      UAT_BRANCH_REGISTRY_ENVIRONMENT: "clp-uat"
    
    image: $REGISTRY/clp-test/it-docker:docker-191224
    
    services:
      - docker:19.03.1-dind
    
    stages:
      - build
      - deploy
      - sync
    
    build:
      stage: build
      rules: 
        - if: '$CI_COMMIT_REF_NAME == $MASTER_BRANCH_NAME'
        - if: '$CI_COMMIT_REF_NAME == $TEST_BRANCH_NAME'
      image: golang
      script:
        - export GO111MODULE=on 
        - export GOPROXY=goproxy.io,goproxy.cn
        - mkdir -p build/linux/config
        - cp config/app.ini.sample build/linux/config/app.ini
        - go mod tidy
        - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags=jsoniter -ldflags "-s -w" -o build/linux/gatewayapi
    
      after_script:
        
      artifacts:
        paths:
          - build
    
    deploy:
      stage: deploy
      variables:
        REGISTRY_HOST: $REGISTRY
        DOCKER_TLS_CERTDIR: ''
        DOCKER_HOST: tcp://docker:2375/
        DOCKER_DRIVER: overlay2
      when: #
        manual
      allow_failure: false
      script:
        - ls
        - export ENV_NAME="clp-dev"
        - if [ "${CI_COMMIT_REF_NAME}" = "$MASTER_BRANCH_NAME" ]; then ENV_NAME=$UAT_BRANCH_REGISTRY_ENVIRONMENT; fi;
        - if [ "${CI_COMMIT_REF_NAME}" = "$TEST_BRANCH_NAME" ]; then ENV_NAME=$TEST_BRANCH_REGISTRY_ENVIRONMENT; fi;
        - export IMAGE_NAME=$REGISTRY/${ENV_NAME}/$DOCKER_IMAGE_NAME:${CI_COMMIT_SHORT_SHA}-$(date -d @"$(($(date +%s)+8*60*60))" +%Y%m%d%H%M)
        - export IMAGE_LATEST=$REGISTRY/${ENV_NAME}/$DOCKER_IMAGE_NAME:latest
        - docker login -u ${USER_NAME}  -p ${USER_PWD} $REGISTRY
        - docker build -t ${IMAGE_NAME} .
        - docker tag ${IMAGE_NAME} ${IMAGE_LATEST}
        - docker push ${IMAGE_NAME}
        - docker push ${IMAGE_LATEST}
    
    sync:
      stage: sync
      rules: 
        - if: '$CI_COMMIT_REF_NAME == $MASTER_BRANCH_NAME'
        - if: '$CI_COMMIT_REF_NAME == $TEST_BRANCH_NAME'
      script:
        - export K8S_TOKEN=${DEV_K8S_TOKEN}
        - if [ "${CI_COMMIT_REF_NAME}" = "$MASTER_BRANCH_NAME" ]; then K8S_TOKEN=${UAT_K8S_TOKEN}; fi;
        - if [ "${CI_COMMIT_REF_NAME}" = "$TEST_BRANCH_NAME" ]; then K8S_TOKEN=${TEST_K8S_TOKEN}; fi;
        - curl -I https://cs.console.aliyun.com/hook/trigger?token=${K8S_TOKEN}
    

      

  • 相关阅读:
    bzoj 4583 购物
    hdu 4694 支配树
    弦图问题初步
    第一次省选总结
    初学kd树
    省选前集训 lca
    bzoj 3282 Tree
    bzoj 2157 旅游
    二分图匹配(匈牙利算法模板)
    最大流(模板)
  • 原文地址:https://www.cnblogs.com/dxqNet/p/12118103.html
Copyright © 2020-2023  润新知