git-jenkins阶段02 git标签, github使用, gitlab安装, 配置, 使用, 开发推送和克隆代码
-a 指定标签的名字 -m 指定说明信息 [root@git git_data]# git log --oneline 7d9c037 modify ccc.txt 1 3f8051e commit modify oldboy.txt c22a40f modify test #把某一次的提交作为一次标签 [root@git git_data]# git tag -a v2.0 7d9c037 -m "test" #tag -a打标签名称 -m做备注 [root@git git_data]# git tag #显示所有标签 v1.0 v2.0 [root@git git_data]# git show v2.0 #查看标签内容 tag v2.0 Tagger: qls <123@qq.com> Date: Mon Jun 10 08:54:54 2024 +0800 test commit 7d9c0374ff90dfbb0b6f5b7b7001b6b802d1e172 Author: qls <123@qq.com> Date: Mon Jun 10 08:53:21 2024 +0800 modify ccc.txt 1 diff --git a/ccc.txt b/ccc.txt new file mode 100644 index 0000000..90d2950 --- /dev/null +++ b/ccc.txt @@ -0,0 +1 @@ +111111 #将数据恢复到某个标签 [root@git git_data]# git reset --hard v2.0 HEAD is now at 7d9c037 modify ccc.txt 1 [root@git git_data]# ll total 8 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 7 Jun 10 19:59 ccc.txt -rw-r--r-- 1 root root 0 Jun 7 21:45 master.txt -rw-r--r-- 1 root root 32 Jun 7 23:17 oldboy.txt -rw-r--r-- 1 root root 0 Jun 7 21:40 test.txt [root@git git_data]# cat ccc.txt 111111 #删除标签 [root@git git_data]# git tag -d v1.0 Deleted tag 'v1.0' (was ccd9800)
git版本库的托管
1.注册好用户
创建仓库
#查看本地仓库的远程仓库 [root@git git_data]# git remote #添加一个远程仓库,这里选择秘钥的方式 [root@git git_data]# git remote add origin git@github.com:DingchaoLu-cloud/git_data.git [root@git git_data]# git remote origin #将本地仓库数据推送到远程仓库失败 [root@git git_data]# git push -u origin main Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. #删除原来的秘钥,重新创建(第一次创建不需要执行) [root@git .ssh]# rm -rf /root/.ssh/ #创建密钥对 [root@git git_data]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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:IItiiPSz2HneC8epkIV8cvM2rhq5SEahewArw8n0tz8 root@git The key's randomart image is: +---[RSA 2048]----+ | | | | |.+ . . | |X.*..o . | |X*.B.* S | |+=o % = . | |.+.O + B | |o.. * OE. | | . o.+.=o | +----[SHA256]-----+ #查看公钥信息,复制公钥,粘贴到web界面 [root@git ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA... root@git
github配上SSH keys, 把公钥复制进去
点setting
#再次推送数据 [root@git git_data]# git push -u origin master Counting objects: 30, done. Compressing objects: 100% (23/23), done. Writing objects: 100% (30/30), 2.25 KiB | 0 bytes/s, done. Total 30 (delta 10), reused 0 (delta 0) remote: Resolving deltas: 100% (10/10), done. remote: To git@github.com:DingchaoLu-cloud/git_data.git * [new branch] master -> master Branch master set up to track remote branch master from origin. #克隆远程仓库到本地 #注意:下载别人的项目不能用SSH,因为没有别人仓库的公钥,没法下载 [root@git git_data]# cd /opt/ [root@git opt]# git clone git@github.com:DingchaoLu-cloud/git_data.git #这里下载自己仓库,用SSH下载 Cloning into 'git_data'... remote: Enumerating objects: 30, done. remote: Counting objects: 100% (30/30), done. remote: Compressing objects: 100% (13/13), done. remote: Total 30 (delta 10), reused 30 (delta 10), pack-reused 0 Receiving objects: 100% (30/30), done. Resolving deltas: 100% (10/10), done.
清华源镜像 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ gitlab官方网站 https://about.gitlab.com/
官网选择安装,手动安装,centos版本,选ce社区版,ee是企业版,如图, 查看安装包
https://packages.gitlab.com/gitlab/gitlab-ce
环境 2G内存 50G磁盘 企业 >4G内存 安装包: gitlab-ce-10.2.2 禁用防火墙和selinux #1. 上传安装包 [root@git ~]# rz gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm #安装 出现报错,缺少依赖 [root@git ~]# rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm warning: gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY error: Failed dependencies: policycoreutils-python is needed by gitlab-ce-10.2.2-ce.0.el7.x86_64 #安装依赖 [root@git ~]# yum install -y policycoreutils-python #再次安装,安装成功 [root@git ~]# rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm warning: gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY Preparing... ################################# [100%] Updating / installing... 1:gitlab-ce-10.2.2-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 #gitlab相关文件及目录介绍 [root@git ~]# ll /etc/gitlab/gitlab.rb #主配置文件 [root@git ~]# cd /opt/gitlab/ #安装程序目录 [root@git gitlab]# ll /var/opt/ #数据存放目录 #修改配置文件,地址改成要访问的地址(需要重新加载才会生效) [root@git gitlab]# vim /etc/gitlab/gitlab.rb external_url 'http://10.0.0.81' #重新加载配置文件 [root@git gitlab]# gitlab-ctl reconfigure #gitlab的状态(查看各种服务运行状态) [root@git gitlab]# gitlab-ctl status run: gitaly: (pid 9098) 22s; run: log: (pid 8796) 69s run: gitlab-monitor: (pid 9116) 22s; run: log: (pid 8895) 57s #监控模式 run: gitlab-workhorse: (pid 9087) 23s; run: log: (pid 8751) 82s #反向代理服务 run: logrotate: (pid 8779) 75s; run: log: (pid 8778) 75s #日志 run: nginx: (pid 8758) 81s; run: log: (pid 8757) 81s #web界面 run: node-exporter: (pid 8845) 63s; run: log: (pid 8844) 63s run: postgres-exporter: (pid 9142) 21s; run: log: (pid 9025) 39s run: postgresql: (pid 8526) 131s; run: log: (pid 8525) 131s #数据库 run: prometheus: (pid 9132) 21s; run: log: (pid 8927) 45s #监控 run: redis: (pid 8466) 137s; run: log: (pid 8465) 137s #缓存数据库 run: redis-exporter: (pid 8912) 51s; run: log: (pid 8911) 51s run: sidekiq: (pid 8735) 89s; run: log: (pid 8734) 89s #后台执行队列任务 run: unicorn: (pid 8696) 95s; run: log: (pid 8695) 95s #动态的web程序 #gitlab管理命令 gitlab-ctl start stop restart
gitlab-ctl start #gitlab所有项目都启动 #单独停掉其中某个服务 gitlab-ctl stop nginx #查看gitlab所有服务日志 [root@git gitlab]# gitlab-ctl tail #查看每个服务单独的日志 [root@git gitlab]# ll /var/log/gitlab total 0 drwx------ 2 git root 47 Jun 10 23:50 gitaly drwx------ 2 git root 47 Jun 10 23:50 gitlab-monitor drwx------ 2 git root 188 Jun 10 23:49 gitlab-rails drwx------ 2 git root 30 Jun 10 23:49 gitlab-shell drwx------ 2 git root 47 Jun 10 23:50 gitlab-workhorse drwx------ 2 root root 47 Jun 10 23:50 logrotate drwxr-x--- 2 root gitlab-www 131 Jun 10 23:50 nginx drwx------ 2 gitlab-prometheus root 47 Jun 10 23:50 node-exporter drwx------ 2 gitlab-psql root 47 Jun 10 23:50 postgres-exporter drwx------ 2 gitlab-psql root 47 Jun 10 23:49 postgresql drwx------ 2 gitlab-prometheus root 47 Jun 10 23:50 prometheus drwxr-xr-x 2 root root 28 Jun 10 23:49 reconfigure drwx------ 2 gitlab-redis root 47 Jun 10 23:49 redis drwx------ 2 gitlab-redis root 47 Jun 10 23:50 redis-exporter drwx------ 2 git root 47 Jun 10 23:50 sidekiq drwx------ 2 git root 99 Jun 10 23:50 unicorn #web界面进行访问 http://10.0.0.81/ #输入root注册密码12345678 用户:root 密码:12345678 #进行登录
修改登录页面外观,点击扳手,如图
设置外观,如图
退出重新登录,如图
#关闭注册功能 #(公司人不多,可以关闭)
如图,点击扳手,点击setting
注意 Sign-in 登录千万不要关闭,否则无法登录
点击save,然后退出。在进入登录界面,如图,没有注册选项了
创建组
创建用户 qls
如图,其他地方不动,普通用户Regular,点击Create user创建
如图,点击edit,进入修改密码12345678
就可以用新创建的qls用户登录,重置密码(可以和原密码一致) #把用户添加到组里 重新用root登录,点击扳手,组名,点击Add users to group把用户添加到组里 Guest和Reportor几乎没有权限,一般给Developer,如果领导可以给Master权限
创建项目
#注意,下图中项目组一定要改所在项目组, 如这里的OPS;如果初始的root,OPS项目组的人无法查看
项目创建完成后,点击上方黄色提醒,添加SSH公钥 #这个SSH公钥是针对当前用户的公钥,并不是单单这个项目的公钥。 #刚刚创建qls用户,也只要上传自己的公钥即可管理项目 #获取机器的公钥 [root@git ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3N...Ropsf root@git
复制服务器上的公钥,如图
#提交代码到gitlab上 #先移除之前github的远程仓库 [root@git git_data]# git remote origin [root@git git_data]# git remote remove origin #移除远程仓库 [root@git git_data]# git remote #添加gitlab远程仓库 [root@git git_data]# git remote add origin git@10.0.0.81:OPS/git_data.git [root@git git_data]# git remote origin #把本地代码推送到远程仓库 [root@git git_data]# git push -u origin master #修改源代码,之后再推送到gitlab上面 [root@git git_data]# echo "test" >> ddd.txt #创建ddd.txt文件 [root@git git_data]# git add . [root@git git_data]# git commit -m "new file add.txt" [master a448a5e] new file add.txt 1 file changed, 1 insertion(+) create mode 100644 ddd.txt #-u选择哪一个远程仓库 [root@git git_data]# git push -u origin master Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To git@10.0.0.81:OPS/git_data.git 7d9c037..a448a5e master -> master Branch master set up to track remote branch master from origin. #开发人员推送代码到gitlab 1.创建一个开发用户,让其加入OPS组,拥有上传和下载代码的权限 #创建一个开发用户dev01,密码12345678,把他添加到OPS项目组,Developer权限 #用dev01账号登录 2.克隆一台新的服务器 #创建dev01-10.0.0.82环境(模拟开发环境) [root@dev01 ~]# yum install -y git #创建密钥对 [root@dev01 ~]# ssh-keygen -t rsa 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:Hf+Vr3ZvMNc2SSKy+8s5bS79+IhRDIj2OofPtT/ozfo root@dev01 The key's randomart image is: +---[RSA 2048]----+ | | | . . | | o ... | | . .o +o. ..| | S.+ ooo.+| | o. ..o+=| | + ..o+ .=o| | =.o+B=+.o| | ooXXE==o| +----[SHA256]-----+ [root@dev01 ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc...5l85N root@dev01 #把公钥添加到gitlab下dev01用户对应的SSH Keys #把gitlab上的项目下载到本地 [root@dev01 ~]# git clone git@10.0.0.81:OPS/git_data.git Cloning into 'git_data'... The authenticity of host '10.0.0.81 (10.0.0.81)' can't be established. ECDSA key fingerprint is SHA256:2SQdzwgUOhQNWwPZEZSV9NaJ2i3tS2GgUaCazKOyOew. ECDSA key fingerprint is MD5:24:68:a9:38:f3:00:b4:7b:8a:72:0f:2a:c9:77:82:e0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.81' (ECDSA) to the list of known hosts. remote: Counting objects: 33, done. remote: Compressing objects: 100% (25/25), done. remote: Total 33 (delta 11), reused 0 (delta 0) Receiving objects: 100% (33/33), done. Resolving deltas: 100% (11/11), done. [root@dev01 ~]# ll total 8 -rw-------. 1 root root 1457 Jun 14 2023 anaconda-ks.cfg drwxr-xr-x 3 root root 130 Jun 11 22:27 git_data -rw-r--r-- 1 root root 131 Jun 15 2023 host.sh #提交代码 [root@dev01 git_data]# echo "test" >> dev01.txt [root@dev01 git_data]# git add . #提交报错,因为没有配置用户和邮箱 [root@dev01 git_data]# git commit -m "new file dev01.txt" *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@dev01.(none)') #设置用户和邮箱 [root@dev01 git_data]# git config --global user.name dev01 [root@dev01 git_data]# git config --global user.email dev01@qq.com [root@dev01 git_data]# git commit -m "new file dev01.txt" [master 028f59a] new file dev01.txt 1 file changed, 1 insertion(+) create mode 100644 dev01.txt #上传到远端仓库 [root@dev01 git_data]# git push -u origin master Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 222 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) To git@10.0.0.81:OPS/git_data.git a448a5e..028f59a master -> master Branch master set up to track remote branch master from origin. #10.0.0.81环境同步数据,把原项目删了,重新拉取最新的代码 [root@git ~]# rm -rf git_data/ [root@git ~]# git clone git@10.0.0.81:OPS/git_data.git Cloning into 'git_data'... remote: Counting objects: 35, done. remote: Compressing objects: 100% (27/27), done. remote: Total 35 (delta 12), reused 0 (delta 0) Receiving objects: 100% (35/35), done. Resolving deltas: 100% (12/12), done. [root@git ~]# cd git_data/ [root@git git_data]# ll total 16 -rw-r--r-- 1 root root 0 Jun 11 21:37 aaa.txt -rw-r--r-- 1 root root 0 Jun 11 21:37 bbb.txt -rw-r--r-- 1 root root 7 Jun 11 21:37 ccc.txt -rw-r--r-- 1 root root 5 Jun 11 21:37 ddd.txt -rw-r--r-- 1 root root 5 Jun 11 21:37 dev01.txt -rw-r--r-- 1 root root 0 Jun 11 21:37 master.txt -rw-r--r-- 1 root root 32 Jun 11 21:37 oldboy.txt -rw-r--r-- 1 root root 0 Jun 11 21:37 test.txt