现有网络上的大部分资料主要都是将私有仓库放到第三方托管平台,如gitee等。可能很多用户并不想讲私有仓库托管到第三方机构。本文主要做了将自己的将私有仓库放到私有git服务器的尝试。
大致流程如下
- 搭建git服务器
- cocoapods私有库创建
- 组件远程仓库创建
- 本地组件仓库创建
- 本地组件仓库关联到远程组件仓库
- 提交本地组件仓库代码到远程组件仓库
- 提交组件podspec文件到私有仓库
- 使用
注: 5步非必须,我们可以首先建立远程仓库clone 到本地 然后在本地创建仓库,这样就直接获取了关联好的本地仓库了 ,但是可能会有一个问题下文提到。
搭建git服务器
搭建git服务器教程很多,可以参考如下文章。
搭建Git服务器
使用Smart HTTP和Gitweb搭建简易个人git服务器
注意:一般git安装完成之后都是使用ssh协议拉取和推送git服务器,如果需要使用http协议的方式,需要一个http容器和额外的配置。可以使用nginx等作为http容器。
cocoapods私有库创建
可以借助第三方的git平台完成,这里在我们自己搭建的git服务器上创建私有仓库。
首先服务器端执行
sudo git init --bare repos.git
repos.git为私有库名称 --bare(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repository)只会生成一类文件:用于记录版本库历史记录的.git目录下面的文件;而不会包含实际项目源文件的拷贝; --bare好处是可以防止冲突。
创建完毕后将远程的私有仓库添加到本地cocoapod中去。
在本地执行
pod repo add repos http://10.10.1.1/git/repos.git
执行完毕后,cocoapods 会把 repos clone 在 ~/.cocoapods/repos 目录。
这一步的主要作用是为了能够让我们在中行 pod install 或者pod search的时候从我们从我们的私有仓库中寻找需要导入的代码。
pod 首先从我们本地cocoapods官方的仓库中寻找有没有对应的仓库,如果没有再从我们本地的私有仓库中寻找对应的组件。如果都没有则失败。(旧版本cocoapods的可能会执行首先只想pod update 更新本地的仓库,新版本需要手动调用pod update )。
也可以通过如下命令查看本地仓库列表,master为cocoapods官方仓库,repos为我们自己创建的私有仓库。
pod repo list

组件远程仓库创建
服务器端完成
sudo git init --bare FchKit.git
FchKit.git为组件仓库名称
本地组件仓库创建
基本可分为两种方式
- pod lib create FchKit直接创建
- 现有工程生成podpec文件进行配置
以下是方式一创建流程
pod lib create FchKit
回答几个问题

创建成功

将组件代码拖到 组件名称下的class文件夹内 ,回到Example文件下下 执行
pod install
每次添加文件都需要重新pod install 。 执行后项目pod文件taget下会出现加入的文件,可修改。
注意这里,本地的文件结构、我们导入后的导入的文件夹结构、我们正式使用时的文件结构,这三者可能并不一致,
开发文件结构。

本地文件结构。

通过pod FchKit 导入文件结构。

如要设置pod进来的文件夹目录结构需要在podspec文件中设置子项目,可参照AFNetworking的podsepc进行子项目设置。(较复杂)
AFNetworking
修改podspec文件方便后续提交到私有库

s.homepage 主页地址 可有可无 如果找不到在将组建添加到私有仓库时会弹出警告
s.source 远程仓库地址
s.source_files 使用组件时pod要从远程长裤拉取的文件
“” 表示匹配所有文件
“.{h,m}” 表示匹配所有以.h和.m结尾的文件
“**” 表示匹配所有子目录
s.dependency 组件要依赖的其他库 可以写多个
s.swift_version 可以设置变异版本 防止他人导入时编译版本不匹配导致的编译无法通过问题。
本地组件仓库关联到远程组件仓库
设置关联
git remote add origin http://10.10.1.1/git/FchKit.git
取消关联
git remote remove origin
查看关联
git remote show origin
origin 为名称
上文提到的问题
上文提到过我们可以通过将远程组件仓库clone下来,然后在仓库中常见本地仓库来避免本地仓库与远程仓库关联这一步。但实际操作过程可能会存在问题。
举个例子 如果我们远程创建组件仓库test 。 然后通过git clone到本地,在本地执行pod lib create test 创建组件仓库代码。这时的目录结构如下

那么我们代码修改完毕后提交代码时应该怎样在test目录下还是在test-test目录下呢?
若果在test目录下 git add 出现警告。

git 仓库冲突,test是一个git仓库。test-test也是一个git仓库。
如果在 test - test 目录下。git add git commit无问题,但是git push origin master却遇到问题。

这是因为我们远程仓库test里面没有test这个仓库,所以也不会有test-test下的origin。
这种情况情况下我们可以将test-test的内容整个复制到test目录下在进行git push即可。
提交本地组件仓库代码到远程组件仓库
git push origin master
输入正确用户名密码就可以成功了

常见问题
1.unable to access 'xxx': The requested URL returned error: 500

服务器端配置不正确,需要在服务器端配置用户名密码。
首先查看http服务器配置信息
vi /etc/apache2/sites-enabled/000-default.conf
加入一下信息到配置文件中
SetEnv GIT_PROJECT_ROOT /var/www/html/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
RewriteEngine On
RewriteCond %{QUERY_STRING} service=git-receive-pack [OR]
RewriteCond %{REQUEST_URI} /git-receive-pack$
RewriteRule ^/git/ - [E=AUTHREQUIRED]
<Files "git-http-backend">
AuthType Basic
AuthName "Git Access"
AuthUserFile /var/www/html/.htpasswd
Require valid-user
Order deny,allow
Deny from env=AUTHREQUIRED
Satisfy any
</Files>

箭头部分可根据需要修改,表示仓库地址和用户配置文件地址。
其次在.htpasswd文件中端配置用户名密码如下
sudo htpasswd -c /var/www/html/.htpasswd 用户名

2. error: RPC failed; curl 52 Empty reply from server

原因可能是因为提交文件过大导致,输入如下命令
git config --global http.postBuffer 524288000
还可能是因为网络原因,或者git服务器存在问题没有http服务器用户写权限导致需要在服务器增加权限。
chmod u=rwx /var/www/html/git -r
表示 增加文件拥有者读写执行权限。/var/www/html/git 为远程仓库存放地址。
提交组件podspec文件到私有仓库
提交之前需要在服务器端将组件代码打tag,并且tag要与podspec文件中的版本号一致。
在服务端执行
git tag 0.1.0
或者在本地打tag 然后把对应tag版本提交到服务器。
本地执行
git tag 0.1.0
git push origin 0.1.1
随后验证spec
pod spec lint

出现上述问题可以加--allow-warnings 参数忽略警告
pod lib lint --allow-warnings
出现 xxx passed validation 表示验证通过

验证通过后,使用pod repo push 命令将podsepc 文件提交到私有仓库, repos 是私有仓库名称 FchKit.podspec是仓库的spec文件。
pod repo push repos FchKit.podspec
常见问题
1. Authentication failed for 'xxx'

提示是因为你用户验证没通过,这里是因为s.source不对,指向了github,而我本地终端登录的是并不是github账号。
可通过下面的命令查看登录的用户
git config user.name
修改用户
git config --global user.name "Your_username"
git config --global user.email "Your_email"
2.Remote branch 0.1.0 not found in upstream origin

远程仓库中没有0.1.0的tag。需要在远程仓库中打tag 。当然在这之前你得有与本地仓库关联的远程仓库。并且将本地仓库代码push到远程仓库。
参照上述tag操作解决。
服务器端执行
git tag 0.1.0
或者本地执行
git tag 0.1.0
git push origin 0.1.1
下面的命令可以查看仓库中的所有tag
git tag
3. xxx did not pass validation, due to 6 errors and 3 warnings.

库未编译通过,需要修改库中的错误
4. remote:error:insufficient permission fo adding an object to repository database ./object

服务器权限问题,一般涉及到permission的错误大部分为服务器端权限问题,需要在服务器设置相应权限。www-data为FchSpecs.git拥有者的用户名。
chown www-data FchSpecs.git -R
chgrp www-data FchSpecs.git -R
这里FchSpecs.git为私有仓库名称,与以上repos.git一样。

成功后可通过pod search 搜索到。
pod search FchKit

组件更新流程为
- 修改并组件代码
- 修改本地podspec文件中的version
- 提交组件代码。
- 服务器端打tag,必须与podspec文件中的version一致。
- 重新提交组件podspec文件到私有仓库
使用组件
将私有仓库添加到本地。如果在创建私有仓库的时候已经添加过则不需要再次添加。
pod repo add repos http://10.10.1.1/git/repos.git
在需要引用组件的Podfile文件中加入
source 'http://10.10.1.1/git/repos.git'
source 'https://github.com/CocoaPods/Specs.git'
从本地的repos.git 和官方的specs.git里面找组件。
加入我们的库
pod 'FchKit'
然后执行
pod install


网友评论