articles icon indicating copy to clipboard operation
articles copied to clipboard

自动化持续集成系列 -- gitlab CI安装和任务配置

Open zhengguorong opened this issue 6 years ago • 0 comments

gitlab安装

安装比较简单,跟着官网的教程,复制粘贴命令 参考地址:https://about.gitlab.com/installation/#ubuntu 安装完毕后浏览器输入服务器地址,gitlab默认使用80端口,如果正常打开,输入密码,默认账户为root。

gitlab-runner安装

因为gitlab CI需要依赖gitlab-runner执行任务,我们也还是按官方教程安装。 参考地址:https://docs.gitlab.com/runner/install/linux-repository.html

docker安装

gitlab-runner需要依赖docker作为构建环境,当然这不是非必要,你可以选择其他依赖,不过个人建议还是用docker比较方便。 docker安装参考地址:https://docs.docker.com/install/linux/docker-ce/ubuntu/

配置runner

gitlab-runner需要和gitlab做绑定,因此需要执行register操作。 1、执行以下命令

sudo gitlab-runner register

2、输入gitlab实例地址

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com(这个地址是你安装gitlab服务器地址)

3、输入token,可以在CI/CD菜单下Runners查看token

Please enter the gitlab-ci token for this runner
xxx

4、给你的runner输入个描述内容,随便填就好

Please enter the gitlab-ci description for this runner
[hostame] my-runner

5、给你的runner设置tag,随便输入

Please enter the gitlab-ci description for this runner
[hostame] my-runner

6、输入runner执行环境,我们用docker,你也可以用其他环境。

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker

7、配置默认docker镜像,用于当.gitlab-ci.yml没有配置时的镜像

Please enter the Docker image (eg. ruby:2.1):
alpine:latest

注册成功后,你可以在gitlab的settings -- CI/CD -- Runners看到关联的runner

配置构建任务

在项目根目录新建.gitlab-ci.yml文件

image: node:8
stages:
  - test
  # - build
  
test:
  stage: test
  before_script:
    - npm install
  script:
    - npm run lint
    - npm run test
  cache:
    key: "$CI_BUILD_REF_NAME"
    paths:
      - 'node_modules/'

配置完毕后,当提交代码或者进行RP的时候,CI会自动执行构建任务,我们的配置文件执行两个任务,检查代码风格和执行单元测试。

提交代码后,CI开始运行,如下图:

image

或者提交RP,走code review流程:

image

代码质量扫描

gitlab 9.3之后支持静态代码检测,可以添加以下任务 官方文档:https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html

code_quality:
  image: docker:stable
  variables:
    DOCKER_DRIVER: overlay2
  allow_failure: true
  services:
    - docker:stable-dind
  script:
    - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
    - docker run
        --env SOURCE_CODE="$PWD"
        --volume "$PWD":/code
        --volume /var/run/docker.sock:/var/run/docker.sock
        "registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
  artifacts:
    paths: [gl-code-quality-report.json]

如果执行任务时,提示权限问题,可以修改runner配置 文件:/etc/gitlab-runner/config.toml privileged = true

concurrent = 1
check_interval = 0

[[runners]]
  name = "kms"
  url = "http://example.org/ci"
  token = "3234234234234"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "alpine:3.4"
    privileged = true
    disable_cache = false
    volumes = ["/cache"]
  [runners.cache]
    Insecure = false

提交代码运行结果:

image

覆盖率报告

单元测试完毕生成的覆盖率报告,我们希望可以在RP的时候体现或者在构建完毕后显示,可以在setting--CI/CD -- General pipelines -- Test coverage parsing 输入

^All files\s+\|\s+\d+\.*\d*\s+\|\s*(\d+\.*\d*)

当提交RP或者提交代码构建时,就会显示覆盖率报告 image

zhengguorong avatar Sep 19 '18 10:09 zhengguorong