简单介绍 Gitlab CI 的功能
从 GitLab 8.X 开始, GitLab CI 就已经集成在 GitLab 中, 我们只要在项目中添加一个. gitlab-ci.yml 文件, 然后添加一个 Runner, 开启 Runner, 即可进行持续集成. 而且随着 GitLab 的升级, GitLab CI 变得越来越强大.
GitLab Runner
在没使用过 Gitlab 之前, 我也有一个困惑, 到底 Gitlab Runner 是什么东西, 它的作用是什么?</br>GitLab Runner 就是来执行这些构建任务的
而此时又会多了一个困惑, Gitlab CI 不是也是用来运行构建任务的吗?</br>
一般来说, 构建任务都会占用很多的系统资源 (譬如编译代码), 而 GitLab CI 又是 GitLab 的一部分, 如果由 GitLab CI 来运行构建任务的话, 在执行构建任务的时候, GitLab 的性能会大幅下降. GitLab CI 最大的作用是管理各个项目的构建状态, 因此, 运行构建任务这种浪费资源的事情就交给 GitLab Runner 来做拉! 因为 GitLab Runner 可以安装到不同的机器上, 所以在构建任务运行期间并不会影响到 GitLab 的性能.
1, 首先部署安装 Gitlab
首先安装 git
yum install -y git
安装 Gitlab 依赖
yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python
启动 ssh,postfix 并设置开机启动和配置防火墙规则
- sudo systemctl enable sshd
- sudo systemctl start sshd
- sudo yum install postfix
- sudo systemctl enable postfix
- sudo systemctl start postfix
- sudo firewall-cmd --permanent --add-service=http
- sudo systemctl reload firewalld
下载安装 Gitlab
- curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
- yum install gitlab-ce
修改 Gitlab 配置, 将 external_url 变量地址改为自己域名或 IP 地址
- vim /etc/gitlab/gitlab.rb
- ## GitLab URL
- ##! URL on which GitLab will be reachable.
- ##! For more details on configuring external_url see:
- ##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
- external_url 'http://gitlab.test.com'
- ## Roles for multi-instance GitLab
重新启动加载配置文件
- gitlab-ctl reconfigure
- gitlab-ctl restart
可以 netstat -ntlp 查看启动的服务及端口 (可以看出已经启动了 nginx 服务及端口为 80 端口, 所以可以直接访问前面配置的域名或 IP 地址)
在浏览器上访问地址 (管理员账号密码在 UI 界面上进行设置)
2, 接下来安装与配置 Gitlab Runner
点开 Runners 可以看到一个设置的 manually![]
点击 install GitLab Runner https://docs.gitlab.com/runner/install/ 安装 Gitlab Runner
- # For Debian/Ubuntu
- $ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
- $ sudo apt-get install gitlab-ci-multi-runner
- # For CentOS
- $ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
- $ sudo yum install gitlab-ci-multi-runner
注册 Runner(这里可以选择注册一个指定的 Runner 或者注册一个共享的 Runner)
指定的 Runner 可以理解为只能对某个份代码有效的一个 Runner, 共享 Runner 可以理解为所有的代码都可以应用得到同一个 Runner, 但是注册共享 Runner 只有 admin 权限又才可.
注册一个共享的 Runner(注册指定 Runner 也是一样的操作)</br>
首先 admin 的账号下看到 Runner 的设置 manually 的 URL 与 token 信息
sudo gitlab-ci-multi-runner register
输入 Gitlab CI 地址
输入项目 Gitlab CI token
输入 Gitlab Runner 描述
输入 Gitlab Runner 标签
输入 Gitlab Runner 执行的语言
可以查看在 Gitlab 共享 Runner 上多了一条 Runner 记录
也可以使用 list 查看 Runner 的状态:
- gitlab-runner list
- Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
- cml_test*.*.172.123 Executor=shell Token=ece68d167647507d1aa61d80ca0f05 URL=http://gitlab.test.com/
接下来编写. gitlab-ci.yml 文件, 推送到远程代码仓库.
这里演示一个简单的 git pull 操作
- cat .gitlab-ci.yml
- # 定义 stages
- stages:
- - test
- # 测试
- test:
- stage: test
- script:
- # Deploy test
- - ansible cml_test*.*.172.123 -a "cd /home/www/test;git pull"
- (这里我使用了 ansible 去管理, 更新代码操作)
最后推送到远程代码仓库上去.
- git add .
- git commit -m "fix .gitlab-ci.yml"
- git push
在相应的代码库下开启的这个共享 Runner.
提交代码触发 CI
来源: http://blog.51cto.com/legehappy/2175334