CI持续集成环境搭建(3)--git的安装与搭建

1. 安装git

*Note:不要用yum安装git, yum源安装git最新版本是1.8.3,该版本太老,之后用git commit 有可能会报错:git: ‘interpret-trailers’ is not a git command. See ‘git --help’.
cannot insert change-id line in .git/COMMIT_EDITMSG
必须安装更新版本,比如git-2.12.2, 需要掌握手动编译安装git的方法。

1) 安装git依赖包

yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

2) 卸载已安装的git (not need if git not installed)

如果环境已经用yum装过git, gitweb,或 git-review,则不能用yum remove git来卸载git, 因为这样会同时卸载依赖包gitweb, git-review.

卸载老git而不卸载依赖包gitweb, git-review :

rpm -e --nodeps git

这个命令,就只删除git这个包,不会删除依赖包,

3) 下载并安装git

a. 进入目录/usr/src/, 将git下载到该目录

	wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.12.2.tar.gz

b. 解压git-2.12.2.tar.gz

	tar -xzvf git-2.12.2.tar.gz

c. 配置git安装路径:

	cd /usr/src/git-2.12.2
	./configure prefix=/usr/local/git/

d. 编绎并且安装

	make && make install

e. 把git加到环境变量

vim /etc/profile
把:/usr/local/git/bin 添加到$PATH后面
source /etc/profile

e. 检查git版本是不是安装的版本 2.12.2

	git version

在这里插入图片描述

2. 初次运行git前的配置

配置git用户

cd ~
touch .git-credentials
vi .git-credentials
http://root:123456@$CentOSIP

git config --global credential.helper store
git config --global user.name $username
git config --global user.email $mail
git config --list    //查看配置信息

Git 提供了一个叫做 git config 的工具(译注:实际是 git-config 命令,只不过可以通过 git 加一个名字来呼叫此命令。),专门用来配置或读取相应的工作环境变量。而正是由这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:

a. /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。
b. ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。
c. 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。

3. git服务器的搭建

大家应该都用过svn, git, github, gitlab等等吧,github是公用仓库,如果私有仓库是要付费的,gitlab私有仓库不用付费,大家都可以选择。

除了github, gitlab之外,我们也可以在自己的服务器上搭建自己的git服务器。
这样一来呢,速度变快了,而且更加方便管理,安全方面也得到了提升。

1) 创建初始化Git仓库

首先我们需要选定一个目录作为git仓库, 进入到任意想要放置仓库的目录下,一般来说git仓库的名称都是以.git结尾。

cd /opt/   
git init --bare devops_test.git   #初始化项目

2) 配置证书登陆

配置所有需要登陆的用户的公钥,公钥位于id_rsa.pub文件中,如果没有的话可以使用ssh-keygen生成。然后将我们的公钥添加到服务器的authorized_keys文件中。

cd ~  
ssh-keygen -t rsa     //生成证书
cat id_rsa.pub > authorized_keys
chmod 644 authorized_keys

2)克隆仓库

cd /tmp/
git clone root@$CentOSIP:/opt/devops_test.git

3) 提交更新到git仓库

把我们的文件加入到本地仓库中,然后执行如下命令将项目提交到服务器!

cd devops_test
把需要提交的文件放到该目录
git add .
git commit -m "xxxxx"
git push

Note: 养成好的习惯,在每次准备修改代码前一定先执行git pull命令将本地仓库同步到最新版本!!!减少冲突的可能。

issues

Issue 1: git: ‘interpret-trailers’ is not a git command. See ‘git --help’.

cannot insert change-id line in .git/COMMIT_EDITMSG
很奇怪,加上issue3的solution gitdir,commit-msg后,就是出现这个问题,平时不会有,在网上也找不到答案。

solution:
check whether it is existed in git installation path: such as /usr/libexec/git-core
we can get the git-ls-tree, git-mailinfo, git-ls-remote and so on

issue 2:bash: git-upload-pack: command not found

代码服务器上的git安装路径是/usr/local/git,不是默认路径,根据提示,在git服务器上, 建立链接文件:

解决方法:
硬连接 ln /usr/local/git/bin/git-upload-pack /usr/sbin/

猜你喜欢

转载自blog.csdn.net/a10703060237/article/details/89704924