您的位置:首页 > 编程语言

提交代码到远程GIT仓库,代码自动同步到远程服务器上。

2017-01-17 20:09 816 查看
一般的做法是,我们开发者先push代码到远程git仓库上,然后我们在登陆自已的服务器上,通过pull把git仓库的代码拉到服务器上。这种做法虽然可行,但过于麻烦了,如下图所示。



如果能够push代码后,服务器上自动的拉git仓库代码那就最好了,这里需要用到webhook了。



使用webhook的步骤如下:
(*这里我们用oschina的码去作为git仓库演示,远程服务器使用阿里云的)
1、在服务器上我们需要安装git客户端,用于拉远程仓库代码。
2、创建公钥并添加公钥。
3、配置webhook,并创建脚本。

一、安装git客户端

> yum install git


二、创建公钥
(*因为apache默认是以daemon用户来运行的,所以生成公钥,要切换成daemon用户)

> cd /sbin/.ssh/
> sudo -u daemon ssh-keygen -f 文件名 -t rsa -C "自已填写内容"


Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in 123456.
Your public key has been saved in 123456.pub.
The key fingerprint is:
56:3a:8a:03:f0:38:d7:5f:c3:9b:81:71:a8:4b:a9:21 123456
The key's randomart image is:


设置密码与确认密码,这里留空,直接回车就行了。
最后生成两个文件:一个你指定的文件名,一个你指定的文件名.pub。
没有指定文件名,则默认为id_rsa和id_rsa.pub。

如果你指定了保存的文件名(因为ssh默认提交id_rsa)
那么需要在/sbin/.ssh/下创建一个config文件,配置如下:

#HostName 主机名
HostName git.oschina.net
#IdentityFile 密钥文件的路径
IdentityFile /sbin/.ssh/你保存的文件名,不带.pub的那个


如果你有两个密钥,那么配置如下:

Host a.git.oschina.net
HostName git.oschina.net
IdentityFile /sbin/.ssh/a
Host b.git.oschina.net
HostName git.oschina.net
IdentityFile /sbin/.ssh/b


这样的修改以后git的地址就要换下了:

原来的:git@git.oschina.net:lackone/gittest.git
改成:git@a.git.oschina.net:lackone/gittest.git


查看公钥,并添加到oschina项目中

> vi /sbin/.ssh/id_rsa.pub


在项目管理下的添加公钥中,把id_rsa.pub中生成的公钥复制到下面。



三、配置webhook
我们要能在服务器上通过daemon用户clone仓库,必须给daemon在/data/www下的读写权限。

> chmod -R 777 /data/www


我们远程服务器项目都放在/data/www目录下。

> sudo -u daemon git clone https://git.oschina.net/lackone/gittest.git


通过上面的命令,我们拷贝了一份gittest的版本库。

我们为这个/data/www/gittest来创建一个虚拟主机,这里我们演示apache的配置

<VirtualHost *:80>
ServerName gittest.chuyouxi.com
DocumentRoot "/data/www/gittest"
</VirtualHost>


(*注意这里的gittest.chuyouxi.com一定是能外网访问的。)

3、创建代码拉取脚本,这里使用php,文件名为update.php

<?php
$data = '/data/www/gittest';
$git = 'git@git.oschina.net:lackone/gittest.git';
echo shell_exec(" cd {$data} && git pull {$git} 2>&1 ");


(*注意,如果不知道git地址的,可以修改update.php成如下代码,来查看)

<?php
file_put_contents('./1.txt', serialize($_POST));


4、配置webhook的url地址



如果访问url地址出现如下错误

From git.oschina.net:lackone/gittest
* branch            HEAD       -> FETCH_HEAD
warning: unable to access '/root/.config/git/attributes': Permission denied
warning: unable to access '/root/.config/git/ignore': Permission denied
warning: unable to access '/root/.config/git/attributes': Permission denied
Updating 29e71d4..f56bc9c
Fast-forward
warning: unable to access '/root/.config/git/attributes': Permission denied
4.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 4.txt


我们创建/root/.config/git目录,并添加文件

> mkdir /root/.config/git
> echo "" > /root/.config/git/attributes
> echo "" > /root/.config/git/ignore
> chmod -R 777 /root/.config/git


如果出现如下错误:

Host key verification failed


则我们需要删除/sbin/.ssh/konw_hosts文件

> rm -rf known_hosts


并修改ssh_config文件

> vi /etc/ssh/ssh_config


#StrictHostKeyChecking yes
改成如下形式,去掉前面的#号
StrictHostKeyChecking no


> service sshd restart


最后我们在本地提交代码到gittest上,远程服务器同步了gittest上的代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: