DevOps 流水线工具 GitLab-CI 实践

作者: scottlin | 来源:发表于2019-03-24 21:27 被阅读72次

    GitLab-CI

    GitLab-CI 是一套配合 GitLab 使用的持续集成系统。当然,还有其它的持续集成系统,同样可以配合 GitLab 使用,比如 Jenkins。上一遍文章利用自定义 DSL 简化 jenkins 流水线,就是利用 Jenkins 配合 GitLab 实现持续集成。
    与 jenkins pipeline 相比,GitLab-CI 更轻,更方便。它直接通过简单 yaml 文件定义 pipeline,相比与 jenkins 复杂的 groovy 语法,GitLab-CI 更简单。

    GitLab-Runner

    GitLab-Runner 是配合 GitLab-CI 进行使用的。一般地,在 GitLab 里面定义一个属于这个工程的软件集成脚本,而 GitLab-Runner 是用来自动化地完成一些软件集成脚本工作,从而实现持续集成。

    安装GitLab-Runner(Mac)

    1、GitLab-Runner 安装
    请参考官方文档:
    https://docs.gitlab.com/runner/install/osx.html

    2、GitLab-Runner 注册
    To register a Runner under macOS:

    • Run the following command:

       gitlab-runner register
      
      
    • Enter your GitLab instance URL:

       Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
       https://gitlab.com
      
      
    • Enter the token you obtained to register the Runner:
      token 值按照如下流程获取:
      GitLab->Setting->CI/CD->Running->Set up a specific Runner manually

       Please enter the gitlab-ci token for this runner
       xxx
      
      
    • Enter a description for the Runner, you can change this later in GitLab’s UI:

       Please enter the gitlab-ci description for this runner
       [hostame] my-runner
      
      
    • Enter the tags associated with the Runner, you can change this later in GitLab’s UI:

       Please enter the gitlab-ci tags for this runner (comma separated):
       my-tag,another-tag
      
      
    • Enter the Runner executor:

       Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
       docker
      
      
    • If you chose Docker as your executor, you’ll be asked for the default image to be used for projects that do not define one in .gitlab-ci.yml:

       Please enter the Docker image (eg. ruby:2.1):
       python:3.7
      

    3、GitLab-Runner 启动

     gitlab-runner install
     gitlab-runner start
    

    启动后可以用 gitlab-runner list 查看列表

    新建 .gitlab-ci.yml, 触发 gitlab-ci(以自建项目 poc 为例)

    1、GitLab 新建 project ,命名为 poc, 如下


    2、新建文件 poc.py ,内容如下:

    """
        gitlab-ci poc
        ~~~~~~~~~
        This module implements gitlab-ci poc.
        :copyright: © 2019 by the scott.lin..
    """
    
    print("hello gitlab-ci!")
    

    然后在新建文件 .gitlab-ci.yml,添加如下文件:
    实现 build stages, 在 build stages 实现 codecheck job

    stages:
      - build
      
    
    codecheck:
      stage: build
      script:
        - pip install pylint
        - pylint poc.py
      tags:
        - python3.7
    
    

    添加完文件后,会触发 gitlab-ci,成功情况下输出:
    pipeline 包括 build stage,多个 stage 串行执行
    build 阶段包括 codecheck job,多个 job 并行执行


    会遇到以下错误,通过在 gitlab-runner 配置 extra_hosts 参数,以添加 host 映射


    具体操作如下:
    1、 gitlab-runner list 查看 gitlab-runner 配置文件,如下:

    2、config.toml 文件添加如下配置:

    更多实践错误参考以下:
    http://arder-note.blogspot.com/2017/01/dockergitlab.html

    总结

    • stages 是流水线阶段,按照 yml 定义顺序执行
    • 然后在 stages 定义 job,比如上面例子中的 codecheck,job 是在同一个 stages 中并行执行的
    • 更多 yml 定义请参考.gitlab-ci.yml
    • 如何整合 kubernetes,待后续研究,kubernetes
    • 还有 ChatOps,ChatOps

    相关文章

      网友评论

        本文标题:DevOps 流水线工具 GitLab-CI 实践

        本文链接:https://www.haomeiwen.com/subject/udfpvqtx.html