Gitlab CI集成工具的应用

Gitlab CI集成工具的应用

Gitlab CI集成工具的应用

什么是Gitlab CI工具

Continuous Integration is a software development practice in which you build and test software every time a developer pushes code to the application, and it happens several times a day.

持续集成是一种软件开发实践,它可以在每次开发人员将代码推送到应用程序时构建和测试软件,并且这种情况每天都可能发生几次。

Gitlab CI工具有什么用

  • Unit test
  • Build software
  • Integration tests

我们使用Gitlab CI工具做什么

  1. 自动判断代码coding style是否符合PEP8编码格式(flake8)
  2. 自动执行单元测试,判断流程代码能否执行成功(pytest)

这样每次将代码上传(PUSH)到gitlab上后,都会自动执行以上两个步骤,只有以上两个步骤通过后, 才会开始后续的人工Code View操作

怎么使用Gitlab CI工具

1. 在gitlab项目中集成CI工具

在项目中新增.gitlab-ci.yml文件,编写测试步骤

test:
  script:
  - pip install virtualenv
  - yum install -y mysql-devel python-devel
  - virtualenv dobot-env
  - source dobot-env/bin/activate
  - pip install -r requirements.txt
  - pip install flake8
  - flake8 --exclude dobot-env
  - pytest

2. 在服务器上安装gitlab-runner

sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
/usr/local/bin/gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
/usr/local/bin/gitlab-runner start

3. 为项目配置gitlab-runner

/usr/local/bin/gitlab-runner register

并输入项目中给予的链接和授权码

Reference

  1. continuous-integration-delivery-and-deployment-with-gitlab
  2. GitLab Continuous Integration (GitLab CI/CD)
  3. Install GitLab Runner manually on GNU/Linux
  4. Registering Runners