一 gitlab代码仓库
1.1 gitlab简介
- GitLab 是一个用于仓库管理系统的开源项目,使用 Git 作为代码管理工具,并在此基础上搭建起来的 web 服务。
- GitLab 具有很多功能,比如代码托管、持续集成和持续部署(CI/CD)、问题跟踪、合并请求管理等。它可以帮助开发团队更好地协作开发软件项目,提高开发效率和代码质量。
官网:https://about.gitlab.com/install/
中文站点: https://gitlab.cn/install/
官方包地址:https://packages.gitlab.com/gitlab/gitlab-ce
1.2 gitlab 的部署实施
1.2.1 部署gitlab
部署gitlab需要内存大于4G
#在安装包之前需配置好软件仓库来解决依赖性
[root@gitlab ~]# yum install -y curl policycoreutils-python-untils openssh-server perl [root@gitlab ~]# dnf install gitlab-ce-17.3.6-ce.0.el9.x86_64.rpm -y
1.2.2 配置gitlab
#修改配置文件
[root@gitlab ~]# cd /etc/gitlab/
[root@gitlab gitlab]# ls
gitlab.rb
[root@gitlab gitlab]# vim gitlab.rb
32 external_url 'http://172.25.254.80'#修改配置文件后需利用gitlab-crt来生效,
[root@gitlab gitlab]# gitlab-ctl reconfigure#执行命令成功后会把所有组件全部启动起来
1.2.3 登陆gitlab
用户名默认为 root
。如果在安装过程中指定了初始密码,则用初始密码登录,如果未指定密码,则系统会随机生成一个密码并存储在 /etc/gitlab/initial_root_password
文件中, 查看随机密码并使用 root
用户名登录。
[!WARNING]
注意:出于安全原因,24 小时后,
/etc/gitlab/initial_root_password
会被第一次gitlab-ctl reconfigure
自动删除,因此若使用随机密码登录,建议安装成功初始登录成功之后,立即修改初始密码。
#查看原始密码
[root@gitlab ~]# cat /etc/gitlab/initial_root_password
# WARNING: This value is valid only in the following conditions
# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
# 2. Password hasn't been changed manually, either via UI or via command line.
#
# If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.Password: H9zmvW4Jml49LoiNBEvN1uyuQXtZ8vaMNvLfYRWkg3k=# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
登陆
设置语言
点击“Save changes“,然后刷新页面
设置密码
1.3 在gitlab中新建项目
#生成sshd密钥
[root@gitlab ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:8BOWjhO4L7tLFWyi71i/x7RfMjcQ7T0dm1/HLwk9Zbk root@gitlab.org
The key's randomart image is:
+---[RSA 3072]----+
| |
| o . . .|
| o * + . . oo|
| . + O . o o ==|
| . . + S . o =E=|
| . o ... . . +=|
| = .o .o + o o|
| = + + = . . |
| . =ooo .. |
+----[SHA256]-----+
[root@gitlab ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC2iVGLEc+Yt6kb9CJP+AzR8R7v1HBAameILKrcVTVUDhR0xw5vBnWYXpfHIoxSQVk13Gzg6Ub1PGp8t+QB+w3nneOAGi4tvs1DfSAc3KjcE1RXMzZ6RH35QcWRI40OcX99cJOVm+VAsBjV+Qq4H57gWnrtkpnJZRddP9MRf+3fqX7q9ReYK3d93d5O8Y9tzo5rO+h9OJqwkElaCNeTxmy/lUZ7LRX4zV00uGI6zZ7xJWNlvXPEORrPOI0RKdcyeL3m5xaL/JfQs7diBwzkbCQQP7bdoSzp3kaWg0xJm4BkRtlHskb/ZC5JIEFHmkZ/n4K3j1Dxk8mtGmjfsnZ8yEAQ4ZDQBxQjxPJ7cMpgE6aBb+2mcYiEgogZQvNqA0EdZVRzQF+vHc1+UYBBpOQ14cTVumEQ/dl7ytyyMY3JhHQRi55zgAwBnKJjBKJpSPdZm2pYudMMb+VPKh6IZrNj+hqdp5Lfi96ASkCuzkVCyhVxZhD0X2J4orEtLhmzpen+nFk= root@gitlab.org
上传公钥到gitlab中
下载项目
[root@gitlab ~]# mkdir ran123
[root@gitlab ~]# git clone git@172.25.254.80:root/ran.git ran123
正克隆到 'ran123'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
接收对象中: 100% (3/3), 完成.[root@gitlab ~]# ls -R
.:
anaconda-ks.cfg gitlab-ce-17.3.6-ce.0.el9.x86_64.rpm ran ran123./ran:
dir1 hao.txt README.md./ran/dir1:./ran123:
README.md
[root@gitlab ~]# cd ran123/
[root@gitlab ran123]# ls
README.md
[root@gitlab ran123]# git remote -v
origin git@172.25.254.80:root/ran.git (fetch)
origin git@172.25.254.80:root/ran.git (push)#文件提交
[root@gitlab ran123]# echo jran > ran123
[root@gitlab ran123]# ls
ran123 README.md
[root@gitlab ran123]# git add ran123
[root@gitlab ran123]# git commit -m "add ran123"
[main e0d88f3] add ran1231 file changed, 1 insertion(+)create mode 100644 ran123
[root@gitlab ran123]# git push -u origin main
枚举对象中: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 265 字节 | 265.00 KiB/s, 完成.
总共 3(差异 0),复用 0(差异 0),包复用 0
To 172.25.254.80:root/ran.git5a59d56..e0d88f3 main -> main
分支 'main' 设置为跟踪 'origin/main'。
二 jenkins
2.1 jenkins 简介
- Jenkins是开源CI&CD软件领导者, 提供超过1000个插件来支持构建、部署、自动化, 满足任何项目的需要。
- Jenkins用Java语言编写,可在Tomcat等流行的servlet容器中运行,也可独立运行
CI(Continuous integration持续集成)持续集成强调开发人员提交了新代码之后,立刻进行构建、(单元)测试。
CD(Continuous Delivery持续交付) 是在持续集成的基础上,将集成后的代码部署到更贴近真实运行环境(类生产环境)中
2.2 部署 jenkins
软件下载:https://www.jenkins.io/download/
jenkins需要部署在新的虚拟机中
[!WARNING]
jenkins需要部署在新的虚拟机中,建议最少4G内存,4核心cpu
#安装依赖包
[root@jenkins ~]# yum install fontconfig java-17-openjdk git#安装jenkins
[root@jenkins ~]# dnf install jenkins-2.479.1-1.1.noarch.rpm #启动jenkins
[root@jenkins ~]# systemctl enable --now jenkins.service#查看原始密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
66215362152d4644985c71965ed2b1ea
部署插件
浏览器访问172.25.254.90:8080
[!NOTE]
建议修改admin的密码,在admin的设置中修改即可
2.3 jenkins 与gitlab的整合
这个错误的原因是因为本机没有gitlab上的sshkey
[root@jenkins ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:RwdTvlA20uSJ7zrEnFLAz+k/18kRFcUf1HN0XH7SaU8 root@jenkins.org
The key's randomart image is:
+---[RSA 3072]----+
| . ++* .*X|
| o X.o =O|
| ++.= .oE|
| .=+ ..++|
| S=..o o|
| ..*. . |
| o .. ..o|
| ..o .o.|
| .. o |
+----[SHA256]-----+
[root@jenkins ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCqK8m8jjv1/47fV5iMqfBVF3yxCo/gmVlT/o/mVAMHpoPE/Ys+lBtNNVUgZpkY/CPVsWRcsU8AbNyTEiKzvlywIhvQHTKn/YIBHZ9qYTXJ4vtDB7fEHkcNhg7Jy2oRS5EKVndiGiYUNgVaNW7uHK5hQURLKci3DpQcagnZnGcXU14C35TFkLRn7t8ksgqYDDP+5Ue0d7dOmg2jDVQeRwNElDe2m8BMdNrvtGC7quuHIS5vUSRt8yor0C4WeTNE0jSCK289UIoRo4l8b5XsaDkLku9qL4Ny1BF/7vz69HKTlsw7oEiDUgQP0qMieV06mj7XosMqjd3OJPEIsfvL5oziayjKG+uETSXhZtXBT4WmJGNhgvXNcA8o6Prs8H+UxuOH7JJQAYatQ9eeZAPMOGSbae2zkz2N8XDIp2h/SOnduFo+7U8v3KU63kqTuBW0e6wWKdurbBdhxXRSTrUVrit6sMeC8mvuSPP/VIaFzK3eU2WErd8kiJX7TDrWrjqTh0U= root@jenkins.org
把此密钥添加到gitlab上即可
添加密钥凭据
添加完成后报错依然存在,因为ssh首次连接主机是需要签名认证,需要手动输入yes
还有两太主机之间一定要做好解析 (/etc/hosts)
[root@jenkins ~]# vim /etc/ssh/ssh_config
Host *StrictHostKeyChecking no