jenkins_git代码管理

2021-04-13  本文已影响0人  慕知

一,Jenkins介绍

Jenkins 持续集成,自动部署  
Git     代码管理


持续集成( Continuous integration , 简称 CI ),频繁地(一天多次)将代码集成到主干。 一般使用SVN或Git
       其目的是让产品可以快速迭代,同时还能保持高质量(代码集成到主干之前,必须通过自动化测试,只要有一个测试用例失败,就不能集成。)


PS:
一个自动构建过程,从检出代码、编译构建、运行测试、结果记录、测试统计等都是自动完成的,无需人工干预。

image.png image.png

二,代码管理

⦁ GitLab私有代码仓库

1,GITLab介绍

GitLab和GitHub一样属于第三方基于Git开发的作品(私有仓库)

GITLAB免费且开源(基于MIT协议),与Github类似, 可以注册用户,任意提交你的代码,添加SSHKey等等;

GitLab是可以部署到自己的服务器上,数据库等一切信息都掌握在自己手上,适合团队内部协作开发




Git 是一个开源的分布式版本控制系统,敏捷高效处理任何或小或大的项目;
     它采用了分布式版本库的方式,不必有服务器端软件支持


PS:
gitee国内版的github;
GitLab看作个人版的GitHub

2,GIT 安装

1)下载git
# 下载
[root@\ nfs~/repertoty]# yum install -y git


# 查看版本(测试)
[root@\ nfs~]# git --version
git version 1.8.3.1

2)初始化仓库
# 创建仓库目录
[root@\ nfs~]# mkdir repertoty

PS:
对应的就是一个目录,这个目录中的所有文件被git管理起来。以后会将一个项目的根目录,作为仓库。仓库中的每个文件的改动 都由git跟踪




# 仓库初始化
[root@\ nfs~]# cd repertoty/
[root@\ nfs~/repertoty]# git init
Initialized empty Git repository in /root/repertoty/.git/


[root@\ nfs~/repertoty]# ll -a 
drwxr-xr-x. 3 root root   18 Apr 11 10:59 .
dr-xr-x---. 6 root root 4096 Apr 11 10:58 ..
drwxr-xr-x. 7 root root  119 Apr 11 10:59 .git


# 将git添加到远程仓库   (https://gitee.com 先注册)
[root@\ nfs~/repertoty]# git remote add origin https://gitee.com/urbanezxx/test.git

# 当本地已经有了代码仓库,现在需要同步远程仓库内容
git pull origin master



# 查看仓库信息
[root@\ nfs~/repertoty]# cd .git/
[root@\ nfs~/repertoty/.git]# ll
drwxr-xr-x. 2 root root   6 Apr 11 10:59 branches
-rw-r--r--. 1 root root 199 Apr 11 10:59 config
-rw-r--r--. 1 root root  73 Apr 11 10:59 description
-rw-r--r--. 1 root root  23 Apr 11 10:59 HEAD
drwxr-xr-x. 2 root root 242 Apr 11 10:59 hooks
drwxr-xr-x. 2 root root  21 Apr 11 10:59 info
drwxr-xr-x. 4 root root  30 Apr 11 10:59 objects
drwxr-xr-x. 4 root root  31 Apr 11 10:59 refs

[root@\ nfs~/repertoty/.git]# cat config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://gitee.com/urbanezxx/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*


3)代码提交到暂存区

# 本地仓库文件夹创建文件测试
[root@\ nfs~/repertoty]# touch a.txt
[root@\ nfs~/repertoty]# git add .
[root@\ nfs~/repertoty]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   a.txt
#   new file:   nginxWebUI
#
[root@\ nfs~/repertoty]# echo "111" > a.txt 
[root@\ nfs~/repertoty]# git add a.txt 
[root@\ nfs~/repertoty]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   a.txt
#   new file:   nginxWebUI

4)文件添加到git缓冲区
git commit -m "commnet"   提交更改,添加备注信息(此时将暂存区的信息提交到本地仓库

[root@\ nfs~/repertoty]# git commit -m 'inits'
[master a6a4ca8] inits
 2 files changed, 2 insertions(+)
 create mode 100644 a.txt
 create mode 160000 nginxWebUI



4)回滚
# 改动a.txt
[root@\ nfs~/repertoty]# echo "222" >> a.txt

# 查看文件变动
[root@\ nfs~/repertoty]# git diff a.txt 
diff --git a/a.txt b/a.txt
index 58c9bdf..a30a52a 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
 111
+222

# 提交到缓存区(这个时候就有了2个版本)
[root@\ nfs~/repertoty]# git commit -m 'inits'


# 查看历史版本
[root@\ nfs~/repertoty]# git log
commit a6a4ca8ba507412ad215a484c6e29be2df6b83b3
Author: urbanezxx <996005378@qq.com>
Date:   Tue Apr 13 10:59:58 2021 +0800

    inits

commit f1f8dd9b5e82d0645914e7b91073cb1d494f21a4
Author: urbanezxx <996005378@qq.com>
Date:   Sun Apr 11 11:03:17 2021 +0800

    init





# 回滚到历史某版本
[root@\ nfs~/repertoty]# git reset --hard a6a4ca8ba507412ad215a484c6e29be2df6b83b3
HEAD is now at a6a4ca8 inits
[root@\ nfs~/repertoty]# cat a.txt 
111



创建仓库
5)上传到远程仓库
# 下载远程仓库(先删除本地仓库,会生成与远程同步的仓库)
[root@\ nfs~]# git clone https://gitee.com/urbanezxx/zxx.git
Cloning into 'zxx'...
warning: You appear to have cloned an empty repository.

[root@\ nfs~]# ll
drwxr-xr-x. 3 root root      18 Apr 13 11:46 zxx


# 基本配置
[root@\ nfs~/repertoty]# git config --global user.name "urbanezxx"
[root@\ nfs~/repertoty]# git config --global user.email "996005378@qq.com"





# 查看配置信息
[root@\ nfs~/repertoty]# git config -l
user.name=urbanezxx
user.email=996005378@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://gitee.com/urbanezxx/repertoty.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master




# 推送到远程仓库


方式一:

[root@\ nfs~]# cd zxx/
[root@\ nfs~/zxx]# ll
total 0
[root@\ nfs~/zxx]# vim a.txt
[root@\ nfs~/zxx]# git add a.txt 
[root@\ nfs~/zxx]# git commit -m 'zxx'
[master (root-commit) 00370d3] zxx
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt


# 输入账户和密码
[root@\ nfs~/zxx]# git push -u origin master
Username for 'https://gitee.com': urbanezxx
Password for 'https://urbanezxx@gitee.com': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 204 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/urbanezxx/zxx.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.






方式二:基于ssh

[root@\ nfs~/zxx]# vim .git/config 
url = https://gitee.com/urbanezxx/zxx.git
更改为
git@gitee.com:urbanezxx/zxx.git


[root@\ nfs~/zxx]# 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:DpOyFXtxaSQ/3mvTjZiV72LPho1q/mPUwzC0wOQNdGc root@nfs
The key's randomart image is:
+---[RSA 2048]----+
|        . =+ . E |
|         +.++.o  |
|      . . *.o..  |
|       + = o + . |
|    . * S . . *. |
|     + =     *.*.|
|    .   .   *.o++|
|           ...Boo|
|           oo=.=+|
+----[SHA256]-----+


[root@\ nfs~/zxx]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcrn/NYJtgXXIEmU5ah0gnm+R3FbO4tbUAvUE1hA+zE+TB1OtjN0g9z377gAERb3cLT3MdsWS7EeOezQhf956u6dudp2ErE0lTiqejHyWe1OUqpzaPyIhmMv0oqW3G/fOxM8OqCh9t5hNWt/9Lubrcs84mCJEuM6Jey2tTvgsvcTE3qqVA402e4JswEPj/uIy5JloIwjhJtsunJft4USJ7XQGHmP3uCq7HDsGpZZNBx/DtpJWKUVV2dQz5mmwZzXBpSXeY5OdLYkKBvrA7yqRBatDEv7H42yVD0CE446YMFkLl28i1GVO7YGkgIG7PNQ71hb9CVx0baciaXLiBU5NL root@nfs



[root@\ nfs~/zxx]# echo '333' >> a.txt 
[root@\ nfs~/zxx]# git add a.txt 
[root@\ nfs~/zxx]# git commit -m 'zxx'
[master 84dc1c3] zxx
 1 file changed, 1 insertion(+)

[root@\ nfs~/zxx]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 236 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:urbanezxx/zxx.git
   00370d3..84dc1c3  master -> master

创建仓库 提交后查看 添加ssh公钥 更新仓库查看
6)拉取
在其他机器拉取
[root@\ backup~]# git clone git@gitee.com:urbanezxx/zxx.git

# 修改a.txt
[root@\ backup~]# cd zxx/
[root@\ backup~/zxx]# ll
total 8
-rw-r--r-- 1 root root 23 Apr 13 16:34 a.txt
-rw-r--r-- 1 root root  4 Apr 13 16:05 b.txt
[root@\ backup~/zxx]# cat a.txt 
666
777
7777
8888
999
[root@\ backup~/zxx]# git add a.txt

# 提交到缓存区并推送到远程仓库
[root@\ backup~/zxx]# git commit -m 'ini' a.txt 
[root@\ backup~/zxx]# git push -u origin master





再原来机器查看更新
# 拉取更新的信息
[root@\ nfs/zxx]# git pull


[root@\ nfs/zxx]# cat a.txt 
666
777
7777
8888
999
















##
克隆指定的分支:git clone -b 分支名  仓库地址
克隆某分支到本地目录,然后在该目录下push时,对应的就是push到远端的对应分支。
image.png
7)分支
# 查看当前分支
[root@\ nfs~/zxx]# git branch
* master

# 查看远程分支(*在哪 就在哪个分支)
[root@\ nfs~/zxx]# git branch -a
* master
  remotes/origin/master

#创建分支
[root@\ nfs~/zxx]# git checkout -b test
Switched to a new branch 'test'

[root@\ nfs~/zxx]# git branch
  master
* test


# 推送到远程test分支
[root@\ nfs~/zxx]# git push -u origin test
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
remote: Create a pull request for 'test' on Gitee by visiting:
remote:     https://gitee.com/urbanezxx/zxx/pull/new/urbanezxx:test...urbanezxx:master
To git@gitee.com:urbanezxx/zxx.git
 * [new branch]      test -> test
Branch test set up to track remote branch test from origin.



# 切换分支
[root@\ nfs~/zxx]# git branch
  master
* test

[root@\ nfs~/zxx]# git checkout master
Switched to branch 'master'
[root@\ nfs~/zxx]# git branch
* master
  test



8)标签
标签:一种特殊的分支,只能够删除,不能够修改

创建tag,方式一:
#  查看当前标签
[root@\ nfs~/zxx]# git tag

# 查看当前分支
[root@\ nfs~/zxx]# git branch
* master
  test

# 创建tag
[root@\ nfs~/zxx]# git tag -a v1-stable -m '第一个tag'
[root@\ nfs~/zxx]# git tag
v1-stable



方式二:
如下图



# 查看标签内容
[root@\ nfs~/zxx]# git show v1-stable
tag v1-stable
Tagger: urbanezxx <996005378@qq.com>
Date:   Tue Apr 13 12:22:38 2021 +0800

第一个tag

commit 6fa5b282ad226cb1b31d2e273307fe224952f974
Author: urbanezxx <996005378@qq.com>
Date:   Tue Apr 13 12:18:03 2021 +0800

    init

diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..b2a7546
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+ccc







# 按照分支拉取代码
git clone -b [分支名称] [git链接]




# 按照标签拉取代码
[root@\ backup~/zxx]# git clone https://gitee.com/urbanezxx/zxx.git v1-stable
Cloning into 'v1-stable'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (26/26), done.
[root@\ backup~/zxx]# ll
total 4
-rw-r--r-- 1 root root  3 Apr 13 17:09 a.txt
drwxr-xr-x 3 root root 31 Apr 13 17:13 v1-stable


image.png
上一篇下一篇

猜你喜欢

热点阅读