运行环境 gitlab + gitlab runner运行环境
gitlab分gitlab/gitlab-ce, gitlab/gitlab-ee, 社区版本和企业版本
系统: CentOS7.9
docker: 20.10.8
gitlab版本: GitLab Community Edition 14.1.2
gitlab安装部署
docker方式启动,镜像较大需要些时间, 拉取完启动
1
2
3
4
5docker run --detach --publish 443:443 --publish 80:80 --publish 2222:22 --name gitlab --restart always \
--volume /mnt/gitlabconfig:/etc/gitlab \
--volume /mnt/gitlab/logs:/var/log/gitlab \
--volume /mnt/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest修改部分配置(gitlab_rails时间、邮件、oss key之类均可配置)
external_url ‘http://ip/' # 外网ip或域名
gitlab_rails[‘gitlab_ssh_host’] = ‘192.168.7.200’
gitlab_rails[‘gitlab_shell_ssh_port’] = 2222
registry_external_url ‘https://registry-gitlab.example.com'
nginx[‘ssl_certificate’] = “/etc/gitlab/ssl/gitlab.example.com.pem”
nginx[‘ssl_certificate_key’] = “/etc/gitlab/ssl/gitlab.example.com.key”
prometheus_monitoring[‘enable’] = false为保安全性,需要重置密码,网上很多说使用gitlab-rails console production,可是有报错,使用gitlab-rails console -e production,前者应该是之前的版本:
运行gitlab-rails console production报错: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#gitlab-rails console production
Traceback (most recent call last):
8: from bin/rails:4:in `<main>'
7: from bin/rails:4:in `require'
6: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/commands.rb:18:in `<top (required)>'
5: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/command.rb:50:in `invoke'
4: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/command/base.rb:69:in `perform'
3: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
2: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
1: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/console/console_command.rb:95:in `perform': wrong number of arguments (given 1, expected 0) (ArgumentError)
9: from bin/rails:4:in `<main>'
8: from bin/rails:4:in `require'
7: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/commands.rb:18:in `<top (required)>'
6: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/command.rb:50:in `invoke'
5: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.1.3.2/lib/rails/command/base.rb:69:in `perform'
4: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
3: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
2: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/command.rb:20:in `run'
1: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/command.rb:34:in `rescue in run'
/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/base.rb:525:in `handle_argument_error': ERROR: "rails console" was called with arguments ["production"] (Thor::InvocationError)
Usage: "rails console [options]"
运行gitlab-rails console -e production
#gitlab-rails console -e production
--------------------------------------------------------------------------------
Ruby: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
GitLab: 14.1.2 (8c67b499146) FOSS
GitLab Shell: 13.19.1
PostgreSQL: 12.6
--------------------------------------------------------------------------------
Loading production environment (Rails 6.1.3.2)
irb(main):001:0> user=User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password='mypassword'
irb(main):002:0> user.password='mypassword'
=> "mypassword"
irb(main):004:0> user.password_confirmation='mypassword'
=> "mypassword"
irb(main):005:0> user.save
Enqueued ActionMailer::MailDeliveryJob (Job ID: d2188216-9afb-4b87-aa33-bbd07d8931c9) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", {:args=>[#<GlobalID:0x00007fd186dba088 @uri=#<URI::GID gid://gitlab/User/1>>]}
=> truegitlab就完成了,可以登录添加项目了。
gitlab-runner安装配置
gitlab-runner作为gitlab ci/cd的执行者,可以支持各种环境如:shell, docker,以docker为例
启动gitlab-runner
1
2docker run -d --name gitlab-runner --restart always -v /mnt/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
注册执行器
1
2gitlab-ci-multi-runner register 或 docker exec -it gitlab-runner gitlab-ci-multi-runner register
config.toml配置文件(此处注意volumes的设置, build_dir):
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
28concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "test-env"
url = "https://gitlab.example.com/"
token = "tW-N66wByFxCk"
executor = "docker"
builds_dir = "/mnt"
[runners.custom_build_dir]
enabled = true
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/mnt/php-src:/mnt/php-src:rw", "/var/run/docker.sock:/var/run/docker.sock", "/cache", "/mnt/openresty:/mnt/openresty:rw"]
shm_size = 0
Dockerfile
1 | # Version 0.1 |
.gitlab-ci.yml
1 | stages: |
gitlab及gitlab-runner注意事项
- gitlab中的一些全局变量是针对项目还是针对项目组的,否则会变量没声明
- gitlab-runner中 volumes配置是下载放置的目录(如nginx配置这种),需要添加上
- gitlab可设置自定义变量,在pipeline run的时候传过去,如(dev、stg、prd各环境)
- gitlab ci/cd master下面需要有.gitlab-ci.yml,不管用不用