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

背景

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.mfbz.cn/a/85895.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Maven安装及IDEA集成Maven

一、简介 Maven是apache旗下的开源项目,是一款用于管理和构建java项目的工具。 基于项目对象模型(POM)的概念,通过一小段描述信息来管理项目的构建。 在Maven项目中,有一个核心文件pom.xml。POM项目对象模型定义了项目的基本信息&#xff0c…

数据结构——队列(C语言)

需求:无 本篇文章将解决一下几个问题: 队列是什么?如何实现一个队列?什么场景下会用队列? 队列的概念: 队列:一种只允许一端进行插入数据操作,在另一端进行删除操作的特殊线性表。…

Ubuntu20 安装 libreoffice

1 更新apt-get sudo apt-get update2 安装jdk 查看jdk安装情况 Command java not found, but can be installed with:sudo apt install default-jre # version 2:1.11-72, or sudo apt install openjdk-11-jre-headless # version 11.0.138-0ubuntu1~20.04 sud…

【网络】IP网络层和数据链路层

IP协议详解 1.概念 1.1 四层模型 应用层:解决如何传输数据(依照什么格式/协议处理数据)的问题传输层:解决可靠性问题网络层:数据往哪里传,怎么找到目标主机数据链路层(物理层)&…

【AIGC】一款离线版的AI智能换脸工具V2.0分享(支持图片、视频、直播)

随着人工智能技术的爆发,AI不再局限于大语言模型,在图片处理方面也有非常大的进步,其中AI换脸也是大家一直比较感兴趣的,但这个技术的应用一直有很大的争议。 今天给大家分享一个开源你的AI换脸工具2.0,只需要一张所需…

Vue实现Excel表格中按钮增加小数位数,减少小数位数功能,多用于处理金融数据

效果图 <template><div><el-button click"increaseDecimals">A按钮</el-button><el-button click"roundNumber">B按钮</el-button><el-table :data"tableData" border><el-table-column v-for&q…

冠达管理:股票分红的钱会计算到收益吗?为什么分红之后出现亏损?

我们常说炒股的主要收益来源便是除了高抛低吸赚取差价收益之外&#xff0c;还有参加股票分红取得。那么股票分红的钱管帐算到收益吗&#xff1f;为什么分红之后呈现亏本&#xff1f;下面就由冠达管理为大家剖析&#xff1a; 股票分红的钱管帐算到收益吗&#xff1f; 不会。 股…

轻松搭建远程Node.js服务端,让你的应用在公共网络中畅行无阻!

文章目录 前言1.安装Node.js环境2.创建node.js服务3. 访问node.js 服务4.内网穿透4.1 安装配置cpolar内网穿透4.2 创建隧道映射本地端口 5.固定公网地址 前言 Node.js 是能够在服务器端运行 JavaScript 的开放源代码、跨平台运行环境。Node.js 由 OpenJS Foundation&#xff0…

【Redis从头学-8】Redis中的ZSet数据类型实战场景之用户积分榜

&#x1f9d1;‍&#x1f4bb;作者名称&#xff1a;DaenCode &#x1f3a4;作者简介&#xff1a;啥技术都喜欢捣鼓捣鼓&#xff0c;喜欢分享技术、经验、生活。 &#x1f60e;人生感悟&#xff1a;尝尽人生百味&#xff0c;方知世间冷暖。 &#x1f4d6;所属专栏&#xff1a;Re…

Vant 4.6.4发布,增加了一些新功能,并修复了一些bug

导读Vant 4.6.4发布,增加了一些新功能&#xff0c;并修复了一些bug等。 新功能 feat(area-data): 更新芜湖的县区数据&#xff0c;由 nivin-studio 在 #12122 中贡献feat(Locale): 添加塞尔维亚语到国际化&#xff0c;由 RogerZXY 在 #12145 中贡献feat(ImagePreview): 添加 c…

带你了解SpringBoot---开启Durid 监控

文章目录 数据库操作--开启Durid 监控整合Druid 到Spring-Boot官方文档基本介绍Durid 基本使用代码实现 Durid 监控功能-SQL 监控需求:SQL 监控数据SQL 监控数据-测试页面 Durid 监控功能-Web 关联监控需求:Web 关联监控配置-Web 应用、URI 监控重启项目 Durid 监控功能-SQL 防…

VBA Excel函数的使用

一个简单的教程&#xff0c;实现VBA自定义函数。 新建模块 复制后面的代码放进来 函数的入口参数不定义&#xff0c;则认为是一块区域&#xff1b; 反之&#xff0c;如FindChar1 As String&#xff0c;则认为是输入的单值。 循环和分支如下例子&#xff0c;VB比较接近自然语…

删除ubuntu开始菜单中的图标

背景 本来是很好看干净的界面 更新谷歌浏览器后出现了Gmail&#xff0c;幻灯片&#xff0c;谷歌硬盘等跟谷歌相关的乱七八糟东西搞得界面就很丑 解决问题 删掉那个图标 输入命令 sudo nautilus /usr/share/applicationssudo nautilus ~/.local/share/applications可以…

爬虫借助代理会让网速快点吗?

亲爱的程序员朋友们&#xff0c;你曾经遇到过爬虫网速慢的情况吗&#xff1f;别着急&#xff01;今天我将和你一起探讨一下使用代理是否可以加速爬虫&#xff0c;让我们一起进入这个轻松又专业的知识分享。 一、原因和机制的解析 1.IP限制 某些网站为了保护资源和防止爬虫行…

js逆向工具-v-jstools插件自动补环境

目录 一、反爬参数如图二、知识点提前Get三、v_jstools安装四、详细分析流程方法一&#xff1a;本案例操作流程之-生成临时环境-直接可以用的情况方法二&#xff1a;本案例操作流程之-生成临时环境-不可以直接用&#xff0c;需要调试补下 五、文章与视频 一、反爬参数如图 二、…

2023-08-22 Unity Shader 开发入门2 —— Shader 开发介绍

文章目录 一、必备概念1 计算机图形程序接口2 图形接口程序与其他概念的联系 二、Shader 开发1 Shader2 Shader 开发3 需掌握的内容 一、必备概念 1 计算机图形程序接口 ​ 计算机图形程序接口&#xff08;Graphics API&#xff09;是一套可编程的开放标准&#xff0c;不论 2…

【C++奇遇记】内存模型

&#x1f3ac; 博客主页&#xff1a;博主链接 &#x1f3a5; 本文由 M malloc 原创&#xff0c;首发于 CSDN&#x1f649; &#x1f384; 学习专栏推荐&#xff1a;LeetCode刷题集 数据库专栏 初阶数据结构 &#x1f3c5; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如…

五种 CSS 位置类型以实现更好的布局

在 Web 开发中&#xff0c;CSS&#xff08;层叠样式表&#xff09;用于设置网站样式的设置。为了控制网页上元素的布局&#xff0c;使用CSS的position属性。因此&#xff0c;在今天这篇文章中&#xff0c;我们将了解 CSS 位置及其类型。 CSS 位置属性用于控制网页上元素的位置…

Anaconda Conda实现Python多环境管理

Anaconda Conda实现Python多环境管理 Python多环境AnacondaConda环境管理下载安装镜像配置环境管理常用命令创建Python3.10环境 Python多环境 Python多环境指的是在同一台计算机上同时安装并管理多个不同的Python版本。可以在不同版本的Python之间切换&#xff0c;并确保每个项…

FastDeploy部署(C++ Win10)

参考链接&#xff1a;FastDeploy C部署保姆级教程 FastDeploy是百度为了解决AI部署落地难题&#xff0c;发布的新一代面向产业实践的推理部署工具。它旨在为AI开发者提供模型部署最优解&#xff0c;具备全场景、简单易用、极致高效三大特点。项目地址&#xff1a;FastDeploy项…
最新文章