GitLab与GitLab Runner安装(RPM与Docker方式),CI/CD初体验

news2024/9/22 7:23:38

背景

GitLab 是一个强大的版本控制系统和协作平台,记录一下在实际工作中关于 GitLab 的安装使用记录。

一开始使用 GitLab 时,是在 CentOS7 上直接以 rpm 包的方式进行安装,仅作为代码托管工具来使用,版本: 14.10.4

后续预研 GitLabCI/CD 及流水线时,采用 Docker 方式安装,版本: 16.2.3-jh ;引入了 GitLab Runner ,版本: 16.2.0

GitLab

系统环境

[root@gitlab1 opt]# uname -a
Linux gitlab1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@gitlab1 opt]# cat /proc/version
Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020
[root@gitlab1 opt]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@gitlab2 ~]# docker -v
Docker version 20.10.18, build b40c2f6

RPM方式安装GitLab

下载地址,清华镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

[root@gitlab1 local]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm

错误: 无法验证 mirrors.tuna.tsinghua.edu.cn 的由 “/C=US/O=Let's Encrypt/CN=R3” 颁发的证书:
  颁发的证书已经过期。
解决:将https修改为http

[root@gitlab1 local]# rpm -ivh gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm 
警告:gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
错误:依赖检测失败:
        policycoreutils-python 被 gitlab-ce-14.10.4-ce.0.el7.x86_64 需要

解决:yum install -y curl policycoreutils-python openssh-server

[root@gitlab1 local]# rpm -ivh gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm 
警告:gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:gitlab-ce-14.10.4-ce.0.el7       ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  

     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

Help us improve the installation experience, let us know how we did with a 1 minute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnibus&release=14-10

# 编辑配置,设置访问地址
[root@gitlab1 local]# vi /etc/gitlab/gitlab.rb
external_url 'http://gitlab1'

# 重新配置生效
[root@gitlab1 local]# gitlab-ctl reconfigure

Running handlers:
Running handlers complete
Chef Infra Client finished, 606/1618 resources updated in 05 minutes 34 seconds

Warnings:
Rehashing of trusted certificates present in `/etc/gitlab/trusted-certs` failed. If on a FIPS-enabled machine, ensure `c_rehash` binary is available in $PATH.

Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.

NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

gitlab Reconfigured!

Note:

  1. 如何查看版本信息?在GitLab后台中查看版本:在部署域名/IP后加上help。
  2. 如果修改了ip或者端口等配置信息,每次更改完之后,务必记得重新加载配置:gitlab-ctl reconfigure。

[root@gitlab1 local]# cat /etc/gitlab/initial_root_password | grep Password

使用浏览器访问配置的地址,输入用户名与密码。

GitLab 常用命令

  1. gitlab-ctl start:启动gitlab,
  2. gitlab-ctl stop:停止gitlab。
  3. gitlab-ctl status:查看gitlab状态
  4. gitlab-ctl restart:重启服务

作为代码仓库, GitLab 使用包括了以下几方面:

  • 创建组
  • 创建项目
  • 添加SSHkey
  • 克隆
  • 用户注册
  • 用户审核
  • 加入组

这种方式下,如果想使用 CI/CD 功能,则需要接着安装 GitLab Runner (不过不建议用这种直接安装的方式来用,实际生产更多的是使用 Docker 以及 Kubernetes ),分两步:安装和注册,下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/

Note:

  1. GitLab Runner注册分两步:获取runner token,注册
  2. GitLab Runner分为共享型、分组型、项目型,分别在不同的位置
  3. 注册分为交互式和非交互式两种方式。

Docker方式安装使用GitLab

新开一台虚拟机,使用 Docker 的方式来安装和运行 GitLabGitLab Runner

[root@gitlab2 ~]# cd /opt/
[root@gitlab2 opt]# mkdir gitlab
[root@gitlab2 opt]# export GITLAB_HOME=/opt/gitlab

根据官方文档,安装运行 GitLab Runner 的命令如下:

sudo docker run --detach \
  --hostname gitlab.example.com \
  --publish 443:443 --publish 80:80 --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  --shm-size 256m \
  registry.gitlab.cn/omnibus/gitlab-jh:latest

实际执行时,简化了参数,设置了主机名,修改了映射端口号。

docker run -d \
  -h gitlab2 \
  -p 80:80 -p 222:22 \
  --name gitlab \
  --restart always \
  -v $GITLAB_HOME/config:/etc/gitlab \
  -v $GITLAB_HOME/logs:/var/log/gitlab \
  -v $GITLAB_HOME/data:/var/opt/gitlab \
  --shm-size 256m \
  registry.gitlab.cn/omnibus/gitlab-jh:latest

Note: 宿主机的端口22修改为222,否则报错: Error starting userland proxy: listen tcp4 0.0.0.0:22: bind: address already in use.

访问极狐 GitLab URL ,并使用用户名 root 和来自以下命令的密码登录:

[root@gitlab2 ~]# docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
Password: zIrC8HPFfuxVmGSyx27nRbgTRwLaoiIhu+a2edEySMw=

2023-08-19-GitLabHome.jpg

2023-08-19-GitLabVersion.jpg

通过 Docker 来对 GitLab 的服务管理。

docker start gitlab
docker stop gitlab
docker restart gitlab
docker rm gitlab

Docker方式安装注册gitlab-runner

安装GitLab Runner

根据 GitLab 的版本,指定了 GitLab Runner 的版本。

docker run -d --name gitlab-runner --restart always \
  -v /opt/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:v16.2.0

# 查看安装的GitLab Runner的版本信息
[root@gitlab2 ~]# docker exec -it gitlab-runner gitlab-runner --version
Version:      16.2.0
Git revision: 782e15da
Git branch:   16-2-stable
GO version:   go1.20.5
Built:        2023-07-21T22:52:42+0000
OS/Arch:      linux/amd64

注册GitLab Runner

接下来,创建组,创建项目,克隆,为项目注册一个 Runner

Create new group ——> Create new project ——> git clone http://gitlab2/iot/test.git

# 注册为`Shell`类型的执行器。
docker run --rm -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v16.2.0 \
 register \
  --non-interactive \
  --executor "shell" \
  --url "http://192.168.44.149" \
  --registration-token "GR1348941NNVnhpcsLqgBbL-1JxPT" \
  --description "shell-runner" \
  --tag-list "shell,test" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected"

注册过程中遇到的问题:

  1. ERROR: Registering runner… error runner=GR1348941NNVnhpcs status=only http or https scheme supported

原因是我将 --url 的值写成了主机名,得写成 HTTP 地址链接的形式。

  1. ERROR: Registering runner… failed runner=GR1348941NNVnhpcs status=couldn’t execute POST against http://gitlab2/api/v4/runners: Post “http://gitlab2/api/v4/runners”: dial tcp: lookup gitlab2 on 192.168.44.2:53: no such host

原因是我将 --url 的值写成了主机名,在容器中无法通过主机名注册成功,然后改成了 IP 地址,注册成功,以下是注册成功的效果。

[root@gitlab2 ~]# docker run --rm -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v16.2.0 \
>  register \
>   --non-interactive \
>   --executor "shell" \
>   --url "http://192.168.44.149" \
>   --registration-token "GR1348941NNVnhpcsLqgBbL-1JxPT" \
>   --description "shell-runner" \
>   --tag-list "shell,test" \
>   --run-untagged="true" \
>   --locked="false" \
>   --access-level="not_protected"
Runtime platform                                    arch=amd64 os=linux pid=7 revision=782e15da version=16.2.0
Running in system-mode.                            
                                                   
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872 
Registering runner... succeeded                     runner=GR1348941NNVnhpcs
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 

2023-08-19-GitLabRunner.jpg

CI/CD

为了体验下 GitLab RunnerCI/CD 功能,接下来以一段后端服务构建与部署的配置来模拟实际的流水线过程,在创建的项目根目录下新建文件: .gitlab-ci.yml (提交后项目会自动启动流水线的执行)。

stages:
  - build
  - deploy
 
build:
  stage: build
  tags:
    - shell
  only:
    - master
  script:
    - echo "mvn clean"
    - echo "mvn package"

deploy:
  stage: deploy
  tags:
    - test
  only:
    - master
  script:
    - echo "java -jar x.jar"

CI/CD 过程中遇到的问题:

  1. 第一次添加上述配置文件并提交后,并没有触发流水线。。

原来在我用的新版 GitLab 中的主分支从 master 改为了 main ,修改 .gitlab-ci.yml 中的分支名称即可。

  1. 流水线失败,Job日志:fatal: unable to access ‘http://gitlab2/iot/test.git/’: Could not resolve host: gitlab2

2023-08-19-GitLabRunnerError.jpg

感觉还是因为 Docker 容器无法获取宿主机的主机名,进入 gitlab-runner 所在的容器, ping 宿主机 ip 是通的,但是主机名不通,简单的办法是在 config.toml 文件中添加 clone_url = "http://192.168.44.149" ,以 IP 的方式来拉取代码。

# 没有自带ping命令,自行安装
root@249b6b18ffa8:/# apt update
root@249b6b18ffa8:/# apt install -y iputils-ping

# 修改配置,增加clone_url配置,跟url并列
[root@gitlab2 ~]# vi /opt/gitlab-runner/config/config.toml
clone_url = "http://192.168.44.149"

# 重启gitlab-runner所在容器
[root@gitlab2 ~]# docker restart gitlab-runner
gitlab-runner

改成以上配置后,手动启动一个 Pipeline ,执行成功。

2023-08-19-PipelinePass1.jpg

2023-08-19-PipelinePass2.jpg

2023-08-19-PipelinePass3.jpg

# 进入容器
[root@gitlab2 ~]# docker exec -it gitlab-runner /bin/bash

# 列出所有的runner
root@249b6b18ffa8:/# gitlab-runner list
Runtime platform                                    arch=amd64 os=linux pid=27 revision=782e15da version=16.2.0
Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
shell-runner                                        Executor=shell Token=XQEENnyfzyEVs8-2iDB9 URL=http://192.168.44.149

# 查看runner的状态
root@249b6b18ffa8:/# gitlab-runner verify
Runtime platform                                    arch=amd64 os=linux pid=37 revision=782e15da version=16.2.0
Running in system-mode.                            
                                                   
Verifying runner... is alive                        runner=XQEENnyf

Note:

  1. CI Lint可以用来校验.gitlab-ci.yml的语法;
  2. 一个流水线包含多个作业,每个作业下至少包含一个script。
  3. 修改服务器的IP之后,Docker容器无法从服务器外部访问
# 服务器IP更改之后,linux数据包转发配置net.ipv4.ip_forward会变为0,即关闭状态。
[root@gitlab2 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

# 重启docker,docker会默认将net.ipv4.ip_forward值临时改为1(再次新增或更改IP之后会失效)
[root@gitlab2 ~]# systemctl restart docker

小总结

以上是安装和使用 GitLab 的过程,以及CI/CD初体验, GitLab 是一个功能丰富、易于使用和安全可靠的协作平台,可以帮助团队更好地管理和开发项目。

  1. 集成的工作流程:GitLab 提供了一个集成的工作流程,包括代码托管、问题跟踪、持续集成和部署等功能。这使得团队成员可以在同一个平台上协作开发和管理项目。
  2. 代码托管:GitLab 提供了强大的代码托管功能,支持 Git 分布式版本控制系统。团队成员可以轻松地共享和管理代码,进行版本控制和协作开发。
  3. 问题跟踪:GitLab 提供了问题跟踪功能,可以帮助团队成员记录和解决项目中的问题。通过问题跟踪,团队成员可以更好地协作和追踪项目进展。
  4. 持续集成和部署:GitLab 提供了强大的持续集成和部署功能,可以自动化构建、测试和部署代码。这可以大大提高开发效率和代码质量。
  5. 安全性:GitLab 提供了强大的安全性功能,包括访问控制、权限管理和代码审查等。这可以帮助团队保护代码和敏感信息的安全。

Reference

  • https://docs.gitlab.cn/runner/register/

If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/902000.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

OpenCV基础知识(6)— 滤波器

前言:Hello大家好,我是小哥谈。在尽量保留原图像信息的情况下,去除图像内噪声、降低细节层次信息等一系列过程,被叫做图像的平滑处理(或者叫图像的模糊处理)。实现平滑处理最常用的工具就是滤波器。通过调节…

对容器、虚拟机和 Docker 的初学者友好介绍

一、说明 如果你是一个程序员或技术人员,你可能至少听说过Docker:一个有用的工具,用于在“容器”中打包,运输和运行应用程序。很难不这样做,这些天它得到了所有的关注 - 来自开发人员和系统管理员。即使是像谷歌、VMwa…

idea gerrit 插件使用指引

IDEA安装gerrit插件 在线安装(推荐) 直接搜索gerrit,安装即可离线安装 可以到github下载离线包:https://github.com/uwolfer/gerrit-intellij-plugin/releases,不过可能会有版本不兼容问题,还是推荐在线安装…

完美版积分商城系统-奇偶商城系统源码+独立代理后台

奇偶商城系统源码 完美版独立代理后台 1.演示环境:Linux Centos7以上版本 宝塔 2.Nginx 1.18.0 PHP7.0 Mysql5.6 3.伪静态选择thinkphp 4./Application/Common/Conf 修改数据库信息 详细搭建教程附在压缩包内了,下载查看

信号量

信号量(semaphore)和信号只有一字之差,却是不同的概念,信号量与之前介绍的IPC不同,它是一个计数器,用于实现进程间的互斥于同步 本文参考: Linux 的信号量_linux 信号量_行孤、的博客-CSDN博客 …

常见的 Python 错误及其解决方案

此文整理了一些常见的 Python 错误及其解决方案。 1、SyntaxError: invalid syntax 说明:无效的语法是最常见的错误之一,通常是由于编写代码时违反了 Python 的语法规则。可能的原因: 忘记在 if、while、for 等语句后写冒号,或者…

perl下载与安装教程【工具使用】

Perl是一个高阶程式语言,由 Larry Wall和其他许多人所写,融合了许多语言的特性。它主要是由无所不在的 C语言,其次由 sed、awk,UNIX shell 和至少十数种其他的工具和语言所演化而来。Perl对 process、档案,和文字有很强…

GAN!生成对抗网络GAN全维度介绍与实战

目录 一、引言1.1 生成对抗网络简介1.2 应用领域概览1.3 GAN的重要性 二、理论基础2.1 生成对抗网络的工作原理2.1.1 生成器生成过程 2.1.2 判别器判别过程 2.1.3 训练过程训练代码示例 2.1.4 平衡与收敛 2.2 数学背景2.2.1 损失函数生成器损失判别器损失 2.2.2 优化方法优化代…

windows批处理set命令:设置变量,计算表达式

文章目录 基础模式表达式模式变量输入模式环境变量 基础模式 变量是一切编程的基础,在批处理语言中,通过set设置变量,而在调用变量时,需要在变量两端加上百分号,例如 set a1 echo %a%运行结果为 >set a1 >ech…

离市区太远的高铁站,导致旅客舍弃高铁而转用其他交通工具

随着高铁网络的完善,如今旅客出行越来越多选择快速、便捷的高铁作为出行交通工具,不过旅客显然也对一些城市的高铁站离市区太远有所抱怨,导致不少消费者选择舍弃高铁而转用其他交通工具。 网上有一份高铁站离市区距离的排名,据悉离…

Linux Xshell常用命令

一、查看服务器信息 1.1、查看CentOS服务器版本号 执行以下命令 cat /etc/redhat-release结果如下: 1.2、查看服务器根目录下空间占用情况 执行以下命令 cd / du -h --max-depth1 |grep G |sort参数说明: –max-depth1 最大深度为1 grep ‘G’ 文…

JavaScript中的this关键字的作用,以及它如何确定其值

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ this关键字的作用⭐ this的值取决于执行上下文⭐ 示例⭐ 总结⭐ 写在最后 ⭐ 专栏简介 前端入门之旅:探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅!这…

Linux常用命令——dig命令

在线Linux命令查询工具 dig 域名查询工具 补充说明 dig命令是常用的域名查询工具&#xff0c;可以用来测试域名系统工作是否正常。 语法 dig(选项)(参数)选项 <服务器地址>&#xff1a;指定进行域名解析的域名服务器&#xff1b; -b<ip地址>&#xff1a;当主…

探秘Maven神奇力量:使用systemPath加载外部JAR包

&#x1f60a; 作者&#xff1a; 一恍过去 &#x1f496; 主页&#xff1a; https://blog.csdn.net/zhuocailing3390 &#x1f38a; 社区&#xff1a; Java技术栈交流 &#x1f389; 主题&#xff1a; 探秘Maven神奇力量&#xff1a;使用systemPath加载外部JAR包 ⏱️ 创作…

攻防世界-ics-06

原题解题思路 看着页面多&#xff0c;其实只有报表中心能够跳转&#xff0c;但是选了确定后没反应&#xff0c;应该不是注入&#xff0c;只有id会变化。 在burp中设置好负载进行爆破 有一个长度与众不同的包 打开发现flag。

攻防世界-simple_php

原题 解题思路 flag被分成了两个部分&#xff1a;flag2&#xff0c;flag2。获得flag1需要满足变量a0且变量a≠0&#xff0c;这看起来不能实现&#xff0c;但实际上当变量a的值是字符时&#xff0c;与数字比较会发生强制类型转换&#xff0c;所以a为字符型数据即可&#xff0c;变…

STP生成树总结

一、什么是STP&#xff08;802.1D&#xff09; STP协议生来就是为了冗余而存在的&#xff0c;单纯树型的网络无法提供足够的可靠性&#xff0c;由此我们引入了额外的链路&#xff0c;这才出现了环路这样的问题。但单纯是标准的802.1D STP协议并不能实现真正的冗余与负载…

Threejs学习05——球缓冲几何体背景贴图和环境贴图

实现随机多个三角形随机位置随机颜色展示效果 这是一个非常简单基础的threejs的学习应用&#xff01;本节主要学习的是球面缓冲几何体的贴图部分&#xff0c;这里有环境贴图以及背景贴图&#xff0c;这样可以有一种身临其境的效果&#xff01;这里环境贴图用的是一个.hdr的文件…

openGauss学习笔记-45 openGauss 高级数据管理-物化视图

文章目录 openGauss学习笔记-45 openGauss 高级数据管理-物化视图45.1 全量物化视图45.1.1 全量物化视图语法格式45.1.2 全量物化视图参数说明45.1.3 全量物化视图示例 45.2 增量物化视图45.2.1 增量物化视图语法格式45.2.2 增量物化视图参数说明45.2.3 增量物化视图示例 openG…

Rest学习环境搭建:服务消费者

建一个子模块 导入依赖 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache…