文章目录
- 前言
- 一、git简介以及基础操作
- 二、github或者在gitee上创建项目并且上传本地项目
- 自动化创建(触发jenkins)
前言
一、git简介以及基础操作
git 简单来说就是版本控制系统
但是相对于其他版本控制系统来说,它又具有一些优点:
1:git直接记录快照,而非差异比较
2:近乎所有操作都是本地执行,所以git的速度非常快
3:git保证数据的完整性
4:git一般只添加数据,所以说Git 几乎不会执行任何可能导致文件不可恢复的操作
A表示添加状态、M表示修改状态,右边显示表示在工作区,数据还没有add,左边显示表示数据已经add;下面就是具体的展示如何添加和修改,以及如何使用git命令来查看状态
[root@k8s4 demo]# echo msy > msy.txt
[root@k8s4 demo]# git status -s
?? msy.txt
[root@k8s4 demo]# git add msy.txt
[root@k8s4 demo]# git status -s
A msy.txt
[root@k8s4 demo]# echo nb >> msy.txt
[root@k8s4 demo]# git status -s
AM msy.txt
[root@k8s4 demo]# git add msy.txt
[root@k8s4 demo]# git status -s
A msy.txt
[root@k8s4 demo]# git commit -m "add msy.txt"
[master 2d061a3] add msy.txt
1 file changed, 2 insertions(+)
create mode 100644 msy.txt
[root@k8s4 demo]# git status -s
我们有的时候难免会生成一些临时文件,而这些临时文件一般都是以.开头的,但是在git commit 提交的时候不论是普通文件还是正式文件都会一起被提交,所以为了防止这个情况出现,我们创建.gitignore文件,在里边添加.*表示忽略所有以.开头的文件,这样我们在进行提交的时候就不会提交临时文件
**[root@k8s4 demo]# cat .gitignore
.*
[root@k8s4 demo]# l.
. .. .a .b .dir1 .git .gitignore
**
git diff可以查看已暂存和未暂存的修改
git commit -a -m可以直接添加和提交,但是只是针对之前有提交过的文件,对于第一次创建的文件还是得先add后commit
[root@k8s4 demo]# echo 2002 >> msy.txt
[root@k8s4 demo]# git diff
diff --git a/msy.txt b/msy.txt
index 3c6527a..e746c23 100644
--- a/msy.txt
+++ b/msy.txt
@@ -1,2 +1,3 @@
msy
nb
+2002
[root@k8s4 demo]# git status -s
M msy.txt
[root@k8s4 demo]# git commit -a -m "add msy.txt"
[master ef2e7e1] add msy.txt
1 file changed, 1 insertion(+)
[root@k8s4 demo]# git status -s
[root@k8s4 demo]# cat msy.txt
msy
nb
2002
当我们删除文件后,如果想要找回,只需要git checkout 就可以找回刚刚删除的文件
[root@k8s4 ~]# cd demo
[root@k8s4 demo]# ls
msy.txt README.md test.txt
[root@k8s4 demo]# rm -f test.txt
[root@k8s4 demo]# git status -s
D test.txt
[root@k8s4 demo]# git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@k8s4 demo]# git checkout -- text.txt
error: pathspec 'text.txt' did not match any file(s) known to git.
[root@k8s4 demo]# git checkout -- test.txt
[root@k8s4 demo]# ls
msy.txt README.md test.txt
当我们在工作区删除文件且add并且提交之后,如果再次想要找回就需要版本回退,我们可以git log查看日志或者git reflog 来简易查看日志,并且通过git reset --hard来进行版本回滚
[root@k8s4 demo]# git rm test.txt
rm 'test.txt'
[root@k8s4 demo]# git commit -m "delete test.txt"
[master 12ddd8e] delete test.txt
1 file changed, 3 deletions(-)
delete mode 100644 test.txt
[root@k8s4 demo]# ls
msy.txt README.md
[root@k8s4 demo]# git status -s
[root@k8s4 demo]# git log
commit 12ddd8e627e707c8883a6d0dec763fd3f6a57363
Author: Msy <msy@westos.org>
Date: Wed Nov 16 23:29:10 2022 +0800
delete test.txt
commit ef2e7e18800f2fc5ae8e5301d38af72419c4ce07
Author: Msy <msy@westos.org>
Date: Wed Nov 16 18:15:16 2022 +0800
add msy.txt
commit 2d061a3a271c5b230c7b44d9d4719843f440ff96
Author: Msy <msy@westos.org>
Date: Wed Nov 16 13:38:24 2022 +0800
add msy.txt
commit 6351510677d3dc5ca3f13ab2a988d7a700858837
Author: Msy <msy@westos.org>
Date: Wed Nov 16 13:36:01 2022 +0800
add test.txt
commit 87785ad6aa728539714743b8777814e1b7091acc
Author: Msy <msy@westos.org>
Date: Wed Nov 16 13:05:20 2022 +0800
add README.md
commit dc85ee9e977599ca912e376ef525b85bd0efa3d8
Author: Msy <msy@westos.org>
Date: Sun Nov 13 10:27:23 2022 +0800
add test.txt
commit 42c38e24fedd3391699f8355e1b08d8f9f1d3916
Author: Msy <msy@westos.org>
Date: Sun Nov 13 10:21:33 2022 +0800
add README.md
[root@k8s4 demo]# git reflog
12ddd8e HEAD@{0}: commit: delete test.txt
ef2e7e1 HEAD@{1}: commit: add msy.txt
2d061a3 HEAD@{2}: commit: add msy.txt
6351510 HEAD@{3}: commit: add test.txt
87785ad HEAD@{4}: commit: add README.md
dc85ee9 HEAD@{5}: reset: moving to dc85ee9
2da336b HEAD@{6}: commit: delete test.txt
dc85ee9 HEAD@{7}: commit: add test.txt
42c38e2 HEAD@{8}: commit (initial): add README.md
[root@k8s4 demo]# git reset --hard ef2e7e1
HEAD is now at ef2e7e1 add msy.txt
[root@k8s4 demo]# ls
msy.txt README.md test.txt
二、github或者在gitee上创建项目并且上传本地项目
首先在github上新建项目,并且添加公钥
公钥的位置可以用下面的方式获取,如果没有也可以ssh-keygen来获取
[root@k8s4 ~]# cd .ssh/
[root@k8s4 .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@k8s4 .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6U8ejrTO2xNAGk0B6Y4BhrVGWDprZAEP6HSJOIra2Lxn5KYxCZX9MrfRcauCzMX8LZ/U1hFRTlX1gynxIgSq5D8cs9DQcxvTqMDyFYEdCcfqRhAH5GuyOMAZV4GWoPHoiALR3nwaBXRyjLowiC+TZG/7RjBP+JCay/Xrx7/klpvEi/VwQ9My5ya1efCgYuFt1KYRpQqb8wo47C4vfD2XyZYyhwlmQzLTx0nK+V0zT6SVW4Z5DlL2m+MtLhc7tq4ryMyy4xQARFOAV1KaRKZ9O5IzkMJAhHD6NKfljSB/QVUoMZoHWN8QAG8uIVMSrGAQkjAp+zrQRju2tA4OEv8KL root@k8s4
添加、切换到主分支、上传然后在github界面刷新就可以看到我们的代码同步到了github上
[root@k8s4 demo]# git remote add origin git@github.com:moumumu/demo.git
[root@k8s4 demo]# git remote -v
origin git@github.com:moumumu/demo.git (fetch)
origin git@github.com:moumumu/demo.git (push)
[root@k8s4 demo]# git branch -M main
[root@k8s4 demo]# git push -u origin main
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
ECDSA key fingerprint is MD5:7b:99:81:1e:4c:91:a5:0d:5a:2e:2e:80:13:3f:24:ca.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Counting objects: 19, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (19/19), 1.49 KiB | 0 bytes/s, done.
Total 19 (delta 0), reused 0 (delta 0)
To git@github.com:moumumu/demo.git
* [new branch] main -> main
Branch main set up to track remote branch main from origin.
上传之后如果我们想要克隆代码,直接在github上进行克隆上传在本地就可以,很方便而且不担心代码丢失
连接github仓库并且上传本地文件
在创建的私有仓库push本地文件(首先要add再commit最后才能push)
[root@k8s4 511demo]# vim index.html
[root@k8s4 511demo]# ls
index.html README.md
[root@k8s4 511demo]# git add index.html
[root@k8s4 511demo]# git commit -m "add index.html"
[main 131f8a0] add index.html
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@k8s4 511demo]# git push -uf origin main
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.1.14:root/511demo.git
5b672e3..131f8a0 main -> main
Branch main set up to track remote branch main from origin.
下载安装Jenkins和jdk之后通过8080端口访问Jenkins,我们可以在下面的界面搜索我们想要安装的插件,并且在download中可以看到我们已经安装或者正在安装的插件
在gitlab上添加两台主机的公钥,并且在jenkins上添加私钥后进行连接,有的时候会出现 No ECDSA host key is known for 192.168.1.14错误,这是因为它会去检查ssh安全等级,我们只需要进行以下操作
在/etc/ssh/ssh_config中添加StrictHostKeyChecking no并且重启sshd服务就可以解决刚刚的问题
自动化创建(触发jenkins)
一定要记得做时间同步,每个主机都要做
Installed:
chrony.x86_64 0:3.2-2.el7
Complete!
[root@docker1 ~]# vim /etc/chrony.conf
[root@docker1 ~]# systemctl restart chronyd
[root@docker1 ~]# chronyc sources -v
210 Number of sources = 5
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? time.cloudflare.com 0 7 0 - +0ns[ +0ns] +/- 0ns
^? a88-157-128-22.static.cp> 0 7 0 - +0ns[ +0ns] +/- 0ns
^- tick.ntp.infomaniak.ch 1 6 17 17 -1816us[-1375us] +/- 88ms
^- 139.199.215.251 2 6 35 16 -3875us[-3875us] +/- 25ms
^* 120.25.115.20 2 6 27 17 -717us[ -275us] +/- 21ms
[root@docker1 ~]# date
Mon Nov 21 12:18:12 CST 2022
[root@docker1 ~]# cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
server ntp1.aliyun.com iburst
我们在jenkins上配置完成后,进行自动化创建,我们在修改文件并且push到gitlab上后,会触发jenkins然后对harbor进行自动化创建
jenkins中的大概配置如下
我们查看镜像也是一样能看到已经上传的镜像
[root@k8s5 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
reg.westos.org/library/webserver 11 6080b65c6c68 10 minutes ago 142MB
reg.westos.org/library/webserver latest 6080b65c6c68 10 minutes ago 142MB
webserver 9 d7cacfb28022 42 minutes ago 142MB
webserver latest d7cacfb28022 42 minutes ago 142MB
reg.westos.org/library/webserver 10 d7cacfb28022 42 minutes ago 142MB
nginx latest 88736fe82739 5 days ago 142MB