gitlab 可持续集成 CI/CD 流水线

准备环境 : 3 台 Centos7 的机器
gitlab 服务器 : 192.168.22.62
开发人员 : 192.168.22.64 (需安装gitlab-runner,后面会讲)
构建/测试服务器 : 192.168.22.190 (需安装gitlab-runner,后面会讲)

Install gitlab

添加 yum 源 默认会自动安装最新版本

[root@localhost ~]# cat /etc/yum.repos.d/gitlab.repo 
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

[root@localhost ~]# yum makecache
[root@localhost ~]# yum -y install gitlab-ce
... ...
[root@localhost ~]# gitlab-ctl start 

如需自定义web端口可修改

[root@localhost ~]# vim /etc/gitlab/gitab.rb

[root@localhost ~]# grep -vE '^$|^#' /etc/gitlab/gitlab.rb 
external_url 'http://192.168.22.62:8888'
[root@localhost ~]#

修改 gitlab 项目创建后的地址 git@example.com 为 自定义地址

[root@localhost ~]# vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

## GitLab settings
  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: 192.168.22.62         >> 这里改为主机的IP即可
    port: 80
    https: false
    
[root@localhost ~]# gitlab-ctl restart

Install gitlab-runner

在流水线需要调用的机器上装,举个栗子,当开发人员在本机(22.64)提交代码到 gitlab 服务器时 ,想在 构建/测试(22.190) 环境自动更新顺便做一些脚本测试,那么这个gitlab-runner 就要装在(22.190)

官方各系统版本安装 >>>

这里我使用 # Linux x86-64

[root@localhost ~]# curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
[root@localhost ~]# chmod +x /usr/local/bin/gitlab-runner

# 添加注册令牌
[root@localhost onepiece]# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=13113 revision=1564076b version=12.4.0
Running in system-mode.                            
                                               
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.22.62/
Please enter the gitlab-ci token for this runner:
4_xxP9J7-nFSD2BUj-NC
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: leader
Please enter the gitlab-ci tags for this runner (comma separated):
leader
Registering runner... succeeded                     runner=4_xxP9J7
Please enter the executor: docker-ssh+machine, docker, shell, ssh, virtualbox, kubernetes, custom, docker-ssh, parallels, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
[root@localhost onepiece]#  

这里的 url 和 token 都是在你的 项目--设置--Runner -->

添加成功后是这样的激活状态

装完 git-runner 后 我们来试一下

先拉取代码(以下在开发人员机器进行):

[root@localhost zolo]# pwd
/data/zolo
[root@localhost zolo]# git clone http://192.168.22.62/zolo/onepiece.git
正克隆到 'onepiece'...
Username for 'http://192.168.22.62': zolo
Password for 'http://zolo@192.168.22.62': 
remote: Enumerating objects: 53, done.
remote: Counting objects: 100% (53/53), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 53 (delta 19), reused 37 (delta 11)
Unpacking objects: 100% (53/53), done.
[root@localhost zolo]# ls
onepiece
[root@localhost zolo]# cd onepiece/
[root@localhost onepiece]# ls
Readme.md

编辑 .gitlab-ci.yml (在开发人员那台)

[root@localhost onepiece]# vim .gitlab-ci.yml     
stages:
  - build
  - test
build:
  stage: build
  script:
    - echo  `uname -r` >> /tmp/1.txt
  tags:
    - leader
test:
  stage: test
  script:
    - echo  `pwd`
  tags:
    - zolo
[root@localhost onepiece]#

参数详解: >>>

更新推送 触发 .gitlab-ci.yml

[root@localhost onepiece]#  
Readme.md
[root@localhost onepiece]# echo 'haha' >> Readme.md
[root@localhost onepiece]# git add . 
[root@localhost onepiece]# git commit -m 'haha'
[master fd4cb01] haha
1 file changed, 1 insertion(+)
[root@localhost onepiece]# git push 
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

   git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Username for 'http://192.168.22.62': zolo
Password for 'http://zolo@192.168.22.62': 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 299 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://192.168.22.62/zolo/onepiece.git
   cbfd67c..fd4cb01  master -> master
[root@localhost onepiece]# 

验证

去那台安装了 gitlab-runner 而且我们.gitlab-ci.yml 指定的机器看看是否有上面的东西/tmp/1.txt

[root@localhost onepiece]# cd
[root@localhost ~]# cat /tmp/1.txt 
3.10.0-693.el7.x86_64
3.10.0-693.el7.x86_64
[root@localhost ~]# 
comments powered by Disqus