ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

通过服务器中转的git push方法

通过服务器中转的git push方法

公司的机器无法访问github,正好有一个阿里云的服务器,用阿里云服务器搭建一个git仓库中转,并且公司禁止ssh连接阿里云,所以这里采用的方法比较麻烦。

这里先需要在阿里云上建立一个裸仓库,之后为裸仓库配置remote之后他就会自动的将发送到这个仓库的更新推送到remote上

主要流程

主机->http 8900 端口-> nginx -> relay.git(裸仓库) -> http ->github

首先是服务器-github的配置

# 先跳转到需要的目录,之后运行 git init --bare relay.git # relay是文件名,之后推送到这个仓库

配置裸仓库的remote地址

git remote add origin https://github.com/.......

这里需要写上用户名密码,但是博主这里省略了

配 post-receive 钩子(自动转发 GitHub)

cat > <地址>/relay.git/hooks/post-receive << 'EOF' #!/bin/bash git push origin --all git push origin --tags EOF chmod +x <地址>/relay.git/hooks/post-receive

接下来是主机-服务器的配置

默认的话是只能接受但是不能推送,所以需要打开推送的命令

# 允许 HTTP 推送 cd relay.git git config http.receivepack true

属主给 www-data(Nginx 要读写)

chown -R www-data:www-data <地址>/relay.git

安装 fcgiwrap

apt install -y fcgiwrap apache2-utils systemctl enable --now fcgiwrap

nginx配置转发

vim /www/server/nginx/conf/nginx.conf
systemctl reload nginx

server { listen 8900; server_name ip; location ~ ^/relay\.git(/.*)?$ { fastcgi_pass unix:/var/run/fcgiwrap.socket; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; fastcgi_param GIT_PROJECT_ROOT /home/user/slay2; fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param PATH_INFO $uri; fastcgi_param REMOTE_USER $remote_user; } }

除此之外注意云服务器的安全组放行8900端口,以及防火墙放行8900端口的tcp

查看端口的监听情况

ss -lntp | grep 8900

主机的配置:

主机的配置很简单,只要和服务器可以连接即可

git remote add ... git push relay
返回列表