阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

搭建Gitlab CI持续集成环境入门教程

142次阅读
没有评论

共计 2887 个字符,预计需要花费 8 分钟才能阅读完成。

简单介绍 Gitlab CI 的功能

  • 从 GitLab 8.X 开始,GitLab CI 就已经集成在 GitLab 中,我们只要在项目中添加一个.gitlab-ci.yml 文件,然后添加一个 Runner,开启 Runner,即可进行持续集成。而且随着 GitLab 的升级,GitLab CI 变得越来越强大。

GitLab Runner

  • 在没使用过 Gitlab 之前,我也有一个困惑,到底 Gitlab Runner 是什么东西、它的作用是什么?GitLab Runner 就是来执行这些构建任务的
  • 而此时又会多了一个困惑,Gitlab CI 不是也是用来运行构建任务的吗?一般来说,构建任务都会占用很多的系统资源 (譬如编译代码),而 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 地址)

搭建 Gitlab CI 持续集成环境入门教程

在浏览器上访问地址(管理员账号密码在 UI 界面上进行设置)

2、接下来安装与配置 Gitlab Runner

点开 Runners 可以看到一个设置的 manually![]搭建 Gitlab CI 持续集成环境入门教程
点击 install GitLab Runner 安装 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 信息
    搭建 Gitlab CI 持续集成环境入门教程

sudo gitlab-ci-multi-runner register

搭建 Gitlab CI 持续集成环境入门教程

  • 输入 Gitlab CI 地址
  • 输入项目 Gitlab CI token
  • 输入 Gitlab Runner 描述
  • 输入 Gitlab Runner 标签
  • 输入 Gitlab Runner 执行的语言

可以查看在 Gitlab 共享 Runner 上多了一条 Runner 记录
搭建 Gitlab CI 持续集成环境入门教程
也可以使用 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。
    搭建 Gitlab CI 持续集成环境入门教程

提交代码触发 CI

搭建 Gitlab CI 持续集成环境入门教程

Docker 的搭建 Gitlab CI 全过程详解  http://www.linuxidc.com/Linux/2013-12/93537.htm

持续集成环境 Gitlab CI 的官方安装过程解析  http://www.linuxidc.com/Linux/2013-12/93535.htm

GitLab CI 的详细介绍 :请点这里
GitLab CI 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计2887字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中