由于项目组员写的真的很是差劲,以前我都没怎么看过他们的代码,今天一看是真的受不了一行注释都不写,这是只管写不管维护是吗,然后我今天下午特地写了编码指南,要求所有的feature和bugfix等等不再统一一个分支,每个功能使用不同的分支从master上fork,然后我还取消了他们直接往master上push的权限,要求必须通过PR合并以及通过CI/CD代码Review,为此研究了一小下午实现Gitlab自动CI去检查代码。
1、先从宝塔把GitLab社区版装好
我们之前就有这东西了,而且安装贼简单,这里就不说了。
2、安装Runner
GitLab8之后是自带CI的,只是需要Runner而已,而且Runner是需要和你的GitLab版本对应的,我宝塔安装的8.8.5,所以用的runner是1.11.2版本的
2.1 创建目录
cd /home
mkdir GitLabRunner
cd GitLabRunner
2.2 下载、添加用户、注册
sudo wget https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/v1.11.2/binaries/gitlab-ci-multi-runner-linux-386
sudo chmod +x gitlab-ci-multi-runner-linux-386
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
./gitlab-ci-multi-runner-linux-386 install --user=gitlab-runner --working-directory=/home/GitLabRunner/WorkDir/
./gitlab-ci-multi-runner-linux-386 start
如果在start的时候提示下面的错误,就先执行一下安装:
FATAL: Failed to start gitlab-runner: "systemctl" failed: exit status 5, Failed to start gitlab-runner.service: Unit not
./gitlab-ci-multi-runner-linux-386 install -u gitlab-runner
2.3 注册
下面要注册Runner到Gitlab了,先去gtilab的设置看一下自己的url和token,然后执行下面的命令
./gitlab-ci-multi-runner-linux-386 register
按照提示输入:
1、URL
2、Token
3、描述,我这里使用的是Default
4、标签,我用的还是Default
5、是否要运行未标记的构建,我选的是true
6、Runner executor选Shell
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.121:8099/ci
Please enter the gitlab-ci token for this runner:
PrSZXXYweARxpu-utkCE
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: default
Please enter the gitlab-ci tags for this runner (comma separated):
default
Whether to run untagged builds [true/false]:
[false]: true
Registering runner... succeeded runner=PrSZXXYw
Please enter the executor: shell, virtualbox, kubernetes, docker, parallels, ssh, docker+machine, docker-ssh+machine, doc
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically rel
然后进入GitLab就可以看到注册成功了
3、编写脚本测试
我这里随便编写了一个脚本试一下,注意文件名必须是:.gitlab-ci.yml
,而且要在项目根目录
# 指定Runner
image: maven:3-jdk-8
# 定义阶段
stages:
- hello
- build
- test
hello:
stage: hello
script:
- sudo Hello world!
# 定义任务
build:
stage: build
script:
- mvn clean package -f admin/pom.xml
artifacts:
paths:
- admin/target/*.jar
test:
stage: test
script:
- java -jar wuqi-admin/target/*.jar --server.port=0
但是当运行的时候我发现Runner在运行任务时会克隆源码到本地,但是我GitLab的克隆地址是发生变化的,就是我现在用的是192.168.1.121:8099,但是GitLab自己显示的默认地址是182.xxx.xxx.xx:8099,然后我就看了一下官方文档,说是要修改/etc/gitlab/gitlab.rb
这个文件中的external_url
属性,ok修改就是了
3.1 但是!
但是我修改完,执行sudo gitlab-ctl reconfigure
命令去刷新Gitlab配置,却发现GitLab的nginx无法启动!!!
Recipe: gitlab::nginx
* service[nginx] action restart
================================================================================
Error executing action `restart` on resource 'service[nginx]'
================================================================================
Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of /opt/gitlab/embedded/bin/chpst -u root /opt/gitlab/embedded/bin/sv restart /opt/gitlab/service/nginx ----
STDOUT: timeout: down: /opt/gitlab/service/nginx: 0s, normally up, want up
STDERR:
---- End output of /opt/gitlab/embedded/bin/chpst -u root /opt/gitlab/embedded/bin/sv restart /opt/gitlab/service/nginx ----
Ran /opt/gitlab/embedded/bin/chpst -u root /opt/gitlab/embedded/bin/sv restart /opt/gitlab/service/nginx returned 1
Resource Declaration:
---------------------
# In /opt/gitlab/embedded/cookbooks/cache/cookbooks/runit/definitions/runit_service.rb
191: service params[:name] do
192: control_cmd = node[:runit][:sv_bin]
193: if params[:owner]
194: control_cmd = "#{node[:runit][:chpst_bin]} -u #{params[:owner]} #{control_cmd}"
195: end
196: provider Chef::Provider::Service::Simple
Compiled Resource:
------------------
# Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/runit/definitions/runit_service.rb:191:in `block in from_file'
这让我很是难堪啊,百度搜也很少有解决方案,然后我以为是它的nginx和我系统的nginx产生了冲突,都尝试过了重启nginx
# 获取nginx状态
sudo systemctl status nginx
# 重启
sudo systemctl restart nginx
以及尝试手动启动GitLab的nginx:
# 获取状态
sudo gitlab-ctl status nginx
# 重启
sudo gitlab-ctl restart nginx
结果都不行,于是我就去网上搜gitlab的nginx的日志在哪,看看日志咋回事,结果不知道为什么没日志
我就发懵了,想着自己哪搞错了,反复修改了gitlab.rb这个文件和重新刷新配置以及重启Gitlab等等一系列操作,都不行!!!后面偶尔看到一篇大佬的文章:
我是Superman的文章详情,按照文章所说修改了一下启动脚本就正常运行了,以下内容源自这位大佬,仅作记录
问题:
gitlab-ctl reconfigure
之后nginx无法启动
#查看日志
gitlab-ctl tail
#报错信息
chpst: fatal: unable to run: /opt/gitlab/embedded/sbin/nginx: file does not exist
修改启动脚本
vi /opt/gitlab/sv/nginx/run
exec chpst -P /opt/gitlab/embedded/sbin/nginx -p /var/opt/gitlab/nginx
改为
exec chpst -P /opt/gitlab/embedded/sbin/gitlab-web -p /var/opt/gitlab/nginx
就好了