🤖 自动化部署

执行过程

过程

配置过程

本教程默认安装 nginx,docker 和 docker-compose

全文参考链接: https://blog.csdn.net/amethystcity/article/details/104843735

1. 搭建GitlabEE

1.1 获取GitLab破解文件

参考链接: https://developer.aliyun.com/article/893530

  1. 安装 Ruby

    1
    2
    3
    sudo apt update sudo 
    apt install ruby-full

  2. 生成许可证

    1
    2
    gem install gitlab-license

  3. 输入内容

    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
    72
    cat > 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."
    end

    ctrl + c 退出编辑

  4. 生成文件

    1
    2
    ruby license.rb

    生成在root下 GitLabBV.gitlab-license license_key license_key.pub 这三个文件。

1.2 使用docker-compse 完成安装

参考轻量化运行: https://zhuanlan.zhihu.com/p/389717047

docker-compose 内容:

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
72
73
74
75
76
77
78
79
80
version: '3.3'

services:
gitlab:
image: 'gitlab/gitlab-ee:latest'
restart: always
hostname: 'gitlab'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'https://git.akievergarden.top' # web站点访问地址 使用nginx进行反向代理
gitlab_rails['gitlab_shell_ssh_port'] = 222

# Terraform
gitlab_rails['terraform_state_enabled'] = false

# Usage Statistics
gitlab_rails['usage_ping_enabled'] = false
gitlab_rails['sentry_enabled'] = false
grafana['reporting_enabled'] = false

# 关闭容器仓库功能
gitlab_rails['gitlab_default_projects_features_container_registry'] = false
gitlab_rails['registry_enabled'] = false
registry['enable'] = false
registry_nginx['enable'] = false

# 包仓库
gitlab_rails['packages_enabled'] = false
gitlab_rails['dependency_proxy_enabled'] = false

# GitLab KAS
gitlab_kas['enable'] = false
gitlab_rails['gitlab_kas_enabled'] = false

# Mattermost
mattermost['enable'] = false
mattermost_nginx['enable'] = false

# Kerberos
gitlab_rails['kerberos_enabled'] = false
sentinel['enable'] = false

# GitLab Pages
gitlab_pages['enable'] = false
pages_nginx['enable'] = false

# 禁用 PUMA 集群模式
puma['worker_processes'] = 0
puma['min_threads'] = 1
puma['max_threads'] = 2

# 降低后台守护进程并发数
sidekiq['max_concurrency'] = 5


# 关闭监控
prometheus_monitoring['enable'] = false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_exporter['enable'] = false
grafana['enable'] = false
sidekiq['metrics_enabled'] = false
ports:
- '13000:80' #配置了external_url 'https://git.akievergarden.top' 即 https可以不需要映射该端口
- '8443:443'
- '222:22'
volumes:
- ./gitlab/config:/etc/gitlab
- ./gitlab/data:/var/opt/gitlab
- ./gitlab/logs:/var/log/gitlab
- ./gitlab/.license_encryption_key.pub:/opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
logging:
driver: 'json-file'
options:
max-size: '2g'

./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
2
3
4
5
6
7
8
9
10
11
12
13
location / {
proxy_pass https://localhost:8443;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect http:// https://;
}

1.4 启动与激活

  1. 使用 docker-compose up -d 进行镜像拉取和启动

  2. 使用指令获取密码 sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

    登录时使用 root 与 方才获取 password

  3. 前往 ${url}//admin/application_settings/general 路径下,添加许可证中手动输入GitLabBV.gitlab-license中内容并激活

2. 安装GitlabRunner

参考链接: https://zhuanlan.zhihu.com/p/676184653#:\~:text=Ubuntu%2022.04%20%E7%B3%BB%E7%BB%9F%E4%B8%8A%E5%AE%89%E8%A3%85%20Gitlab%20Runner%201%201%29%20%E6%9B%B4%E6%96%B0,5%205%29%20%E5%A6%82%E4%BD%95%E5%9C%A8%20GitLab%20%E4%B8%8A%E6%B3%A8%E5%86%8C%20GitLab%20Runner%20%EF%BC%9F

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

  1. 选择一个项目
  2. 选择 设置-CI/CD

添加runner

  1. 点击 展开
  2. 选择 新建项目 runner ,如下图

添加runner选项

  1. 参考 https://docs.gitlab.com/16.7/ee/ci/runners/configure_runners.html#use-tags-to-control-which-jobs-a-runner-can-run 了解添加标签作用

  2. 点击最下方创建runner

  3. 根据提示完成注册

    注册

注意!: 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

:::

  1. 选择构建-流水线编辑器

选择

  1. 选择需要自动构建的branch

  2. 参考官方文档进行编写

  3. 参考文件

    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

  4. 选择下方提交更改以触发自动构建

  5. 以下是自动构建效果

    效果

5. 配置计划性作业

前往图中位置进行创建

新建计划

6. (可选)配置SMTP

  1. 前往编辑挂载文件 ./gitlab/config/gitlab.rb
  2. 在 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。