Jenkins Git构建基本远程代码

网友投稿 755 2022-10-18

Jenkins Git构建基本远程代码库

Jenkins Git构建基本远程代码库

Git名词解释

几个专用名词的译名如下: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库

一. 新建代码库

# 在当前目录新建一个Git代码库$ git init# 新建一个目录,将其初始化为Git代码库$ git init [project-name]# -一个项目和它的整个代码历史$ git clone [url]

二. 增加/删除文件

# 添加指定文件到暂存区$ git add [file1] [file2] ...# 添加指定目录到暂存区,包括子目录$ git add [dir]# 添加当前目录的所有文件到暂存区$ git add .# 添加每个变化前,都会要求确认# 对于同一个文件的多处变化,可以实现分次提交$ git add -p# 删除工作区文件,并且将这次删除放入暂存区$ git rm [file1] [file2] ...# 停止追踪指定文件,但该文件会保留在工作区$ git rm --cached [file]# 改名文件,并且将这个改名放入暂存区$ git mv [file-original] [file-renamed]

三. 代码提交

# 提交暂存区到仓库区$ git commit -m [message]# 提交暂存区的指定文件到仓库区$ git commit [file1] [file2] ... -m [message]# 提交工作区自上次commit之后的变化,直接到仓库区$ git commit -a# 提交时显示所有diff信息$ git commit -v# 将add和commit合为一步$ git commit -am 'message'# 使用一次新的commit,替代上一次提交# 如果代码没有任何新变化,则用来改写上一次commit的提交信息$ git commit --amend -m [message]# 重做上一次commit,并包括指定文件的新变化$ git commit --amend [file1] [file2] ...

四.远程同步

# -远程仓库的所有变动$ git fetch [remote]# 显示所有远程仓库$ git remote -v# 显示某个远程仓库的信息$ git remote show [remote]# 增加一个新的远程仓库,并命名$ git remote add [shortname] [url]# 取回远程仓库的变化,并与本地分支合并$ git pull [remote] [branch]# 上传本地指定分支到远程仓库$ git push [remote] [branch]# 强行推送当前分支到远程仓库,即使有冲突$ git push [remote] --force# 推送所有分支到远程仓库$ git push [remote] --all

为Jenkin构建远程代码仓库

(1)安装Git

[root@localhost ~]# yum install git -y

(2)创建Git用户并且设置密码

[root@localhost ~]# useradd git[root@localhost ~]# passwd gitChanging password for user git.New password: BAD PASSWORD: The password is shorter than 8 charactersRetype new password: passwd: all authentication tokens updated successfully

(3)创建仓库

[root@localhost ~]# su -gitsu: group it does not exist[root@localhost ~]# su - git[git@localhost ~]$ mkdir repos[git@localhost ~]$ cd repos/ #这个目录存放项目分支[git@localhost repos]$ mkdir app.git #这个是第一个目录[git@localhost repos]$ cd app.git/[git@localhost app.git]$ git --bare init #初始化git项目仓库,如果不初始化,那么目录仅仅是目录而已,初始化之后目录里面就会提供仓库的一些信息了(在当前目录新建一个Git代码库)Initialized empty Git repository in /home/git/repos/app.git/[git@localhost app.git]$ ls #这些信息就是用来支持代码版本控制的branches config description HEAD hooks info objects refs

好了之后找一个看看可不可以往该仓库里面提交代码。

[root@localhost ~]# yum install git -y测试机安装git是为了测试从上面仓库拉取代码,主要充当客户端

[root@localhost test]# git clone git@192.168.179.104:/home/git/repos/app.gitCloning into 'app'...The authenticity of host '192.168.179.104 (192.168.179.104)' can't be established.ECDSA key fingerprint is SHA256:222NBCna+TCSJ4bx8WE3Dd4SLhrrcrrfRweJn7Rmx0c.ECDSA key fingerprint is MD5:e3:bb:90:13:6e:bd:21:3d:4a:aa:ea:0e:f8:32:9c:29.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.179.104' (ECDSA) to the list of known hosts.git@192.168.179.104's password: warning: You appear to have cloned an empty repository.[root@localhost test]# lsApp

提交到本地版本仓库当中,因为本地的也是版本控制仓库。先提交本地,然后从本地合并到git服务器上面。

[root@localhost app]# cd /root/test/app/[root@localhost app]# lsindex.html#添加到本地版本仓库当中[root@localhost app]# git add .[root@localhost app]# git status# On branch master## Initial commit## Changes to be committed:# (use "git rm --cached ..." to unstage)## new file: index.html##这就提交到了本地仓库[root@localhost app]# git commit -m "first commit"[master (root-commit) f86e528] first commit Committer: root Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) create mode 100644 index.html[root@localhost app]# git status #可以看到提交到本地仓库当中# On branch masternothing to commit, working directory clean

默认创建的分支为master分支,amster分支一般是主分支作为线上发布项目用的,其他下面的分支一般用于功能性开发,最后合并到主分支

#这样就推送到git服务器上面了[root@localhost app]# git push origin mastergit@192.168.179.104's password: Counting objects: 3, done.Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@192.168.179.104:/home/git/repos/app.git * [new branch] master -> master

秒免密钥

#可以看到免密钥拉取代码[root@localhost test]# ssh-copy-id -i ~/.ssh/id_rsa.pub "git@192.168.179.104"[root@localhost ~]# mkdir -p test2[root@localhost ~]# cd test2[root@localhost test2]# git clone git@192.168.179.104:/home/git/repos/app.gitCloning into 'app'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Receiving objects: 100% (3/3), done.

可以看到之前推送代码成功

[root@localhost test2]# ls app/ index.html

待会这些拉取操作都会在jenkins去做,简单的总结上面步骤如下

Git安装与使用1、安装Git # yum install git2、创建Git用户并设置密码#useradd git #passwd git 3、创建仓库#su- git# mkdir-r repos/app.git #cd repos/app.git # git --bare init 4、配置sSH免密码认证5、提交代码#gite192.168.0.214:/home/git/repos/ap.git #git add.#git commit -m "1" #git push origin master

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Upmin Admin - 一个框架来创建强大的Ruby on Rails管理后端以最小的努力
下一篇:一款小而美的路由框架。网页动态添加自定义参数启动应用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~