gitlab搭建和自动化
🤖 自动化部署
执行过程
配置过程
本教程默认安装 nginx,docker 和 docker-compose
全文参考链接: https://blog.csdn.net/amethystcity/article/details/104843735
1. 搭建GitlabEE
1.1 获取GitLab破解文件
参考链接: https://developer.aliyun.com/article/893530
安装 Ruby
1
2
3sudo apt update sudo
apt install ruby-full生成许可证
1
2gem install gitlab-license
输入内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72cat > license.rb
require "openssl"
require "gitlab/license"
key_pair = OpenSSL::PKey::RSA.generate(2048)
File.open("license_key", "w") { |f| f.write(key_pair.to_pem) }
public_key = key_pair.public_key
File.open("license_key.pub", "w") { |f| f.write(public_key.to_pem) }
private_key = OpenSSL::PKey::RSA.new File.read("license_key")
Gitlab::License.encryption_key = private_key
license = Gitlab::License.new
license.licensee = {
"Name" => "none",
"Company" => "none",
"Email" => "example@test.com",
}
license.starts_at = Date.new(2020, 1, 1) # 开始时间
license.expires_at = Date.new(2050, 1, 1) # 结束时间
license.notify_admins_at = Date.new(2049, 12, 1)
license.notify_users_at = Date.new(2049, 12, 1)
license.block_changes_at = Date.new(2050, 1, 1)
license.restrictions = {
active_user_count: 10000,
}
puts "License:"
puts license
data = license.export
puts "Exported license:"
puts data
File.open("GitLabBV.gitlab-license", "w") { |f| f.write(data) }
public_key = OpenSSL::PKey::RSA.new File.read("license_key.pub")
Gitlab::License.encryption_key = public_key
data = File.read("GitLabBV.gitlab-license")
$license = Gitlab::License.import(data)
puts "Imported license:"
puts $license
unless $license
raise "The license is invalid."
end
if $license.restricted?(:active_user_count)
active_user_count = 10000
if active_user_count > $license.restrictions[:active_user_count]
raise "The active user count exceeds the allowed amount!"
end
end
if $license.notify_admins?
puts "The license is due to expire on #{$license.expires_at}."
end
if $license.notify_users?
puts "The license is due to expire on #{$license.expires_at}."
end
module Gitlab
class GitAccess
def check(cmd, changes = nil)
if $license.block_changes?
return build_status_object(false, "License expired")
end
end
end
end
puts "This instance of GitLab Enterprise Edition is licensed to:"
$license.licensee.each do |key, value|
puts "#{key}: #{value}"
end
if $license.expired?
puts "The license expired on #{$license.expires_at}"
elsif $license.will_expire?
puts "The license will expire on #{$license.expires_at}"
else
puts "The license will never expire."
endctrl + c 退出编辑
生成文件
1
2ruby license.rb
生成在root下 GitLabBV.gitlab-license license_key license_key.pub 这三个文件。
1.2 使用docker-compse 完成安装
参考轻量化运行: https://zhuanlan.zhihu.com/p/389717047
docker-compose 内容:
1 | version: '3.3' |
./gitlab/.license_encryption_key.pub:/opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
将上一步骤中的.license_encryption_key.pub 使用 cp指令 复制到该目录下 ./gitlab/.license_encryption_key.pub
1.3 nginx反向代理
参考部分配置(需要补全):
1 | location / { |
1.4 启动与激活
使用 docker-compose up -d 进行镜像拉取和启动
使用指令获取密码
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password登录时使用 root 与 方才获取 password
前往 ${url}//admin/application_settings/general 路径下,
添加许可证中手动输入GitLabBV.gitlab-license中内容并激活
2. 安装GitlabRunner
2.1 添加源
1 | curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash |
2.2 执行更新
1 | apt update |
2.3 下载
1 | apt install gitlab-runner |
2.4 添加docker权限
参考链接: https://blog.csdn.net/shancyr45/article/details/118441417
1 | sudo usermod -aG docker gitlab-runner |
3. 配置链接GitlabRunner
- 选择一个项目
- 选择
设置-CI/CD
- 点击
展开 - 选择
新建项目 runner,如下图
点击最下方创建runner
根据提示完成注册
注意!: gitlab-runner run 命令会在当前指令目录下创建 build目录,需要修改目录可以简单的在其他目标目录中执行该命令
gitlab-runner run。其他方法请查找 gitlab-runner config.toml,一般情况下这条指令可以不执行,因为程序默认执行/usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner,详情见额外内容
8. 完成后效果
4. 在项目中配置.gitlab.ci
参考链接: https://www.cnblogs.com/baoruizhe/p/16198309.html
:::tip
前往官方文档获取更多信息: https://docs.gitlab.com/16.7/ee/ci/yaml/index.html
:::
- 选择
构建-流水线编辑器
选择需要自动构建的branch
参考官方文档进行编写
参考文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39# 脚本执行之后删除该镜像(为了不与下次镜像打包冲突)
after_script:
- docker rmi $( docker images -q -f dangling=true)
# 定义几个流程(job),这里是定义了三个自动化流程
stages:
- build
- test
- deploy
job_build:
stage: build
script:
- cd ./bbsbackend
- mvn clean install
only:
- dev
job_test:
stage: test
script:
- cd ./bbsbackend
- mvn test
only:
- dev
job_deploy:
stage: deploy
script:
- cd ./bbsbackend
# 通过Dockerfile生成镜像
- mvn package dockerfile:build
# 删除已经在运行的容器
- if [ $(docker ps -aq --filter name=bbsbackend) ]; then docker rm -f bbsbackend;fi
# 通过镜像启动容器,并把本机端口映射到容器端口
- docker run -d -p 9090:9090 --name bbsbackend org.sevenstar/bbsbackend:0.0.1-SNAPSHOT
only:
- dev选择下方提交更改以触发自动构建
以下是自动构建效果
5. 配置计划性作业
前往图中位置进行创建
6. (可选)配置SMTP
- 前往编辑挂载文件 ./gitlab/config/gitlab.rb
- 在 80行左右 进行编辑,参考链接: https://blog.csdn.net/T748588330/article/details/79915074/
\
额外内容
需要在gitlab.cli中执行root权限操作
https://blog.csdn.net/white_pure/article/details/108039238
安装gitlab-runner构建机时默认会将用户设置为:gitlab-runner,该设置会使编写.gitlab-ci.yml的脚本,操作带来一些权限上的问题。
为了解决这些权限带来的问题,我将gitlab-runner构建机上的默认用户设置为root。
请注意:
这种方式虽然简单粗暴,当然也会带来一些弊端,比如:通过.gitlab-ci.yml脚本进行操作文件(删除、修改时)要格外小心。
- 通过ps aux|grep gitlab-runner命令查看gitlab-runner进程,可以查看到gitlab-runner的工作目录和默认用户等一系列相关信息。
- 通过该命令sudo gitlab-runner uninstall可以卸载掉gitlab-runner默认用户。
- 重新安装gitlab-runner并将用户设置为root gitlab-runner install –working-directory /home/gitlab-runner –user root。
- sudo service gitlab-runner restart重启gitlab-runner。
再通过第一步的命令查看gitlab-runner看默认用户是否变成root。







