簡述
iBrand 產品立項時是商業性質的項目,但是在搭建架構時考慮后續的通用性,因此每個模塊都設計成一個 Package,作為公司內部用,因此這些包並不能提交到 packagist.org 上去。 所以就想是否能夠搭建私有的包倉庫,實現一個私有的 packagist 。
仔細翻閱 Composer 文檔,發現官方有相應的解決方案:Handling private packages
這里推薦使用 Satis ,也正是我們目前使用的方案,目前運行一切良好。
Satis 是一個靜態的 composer 資源庫生成器。它像是一個超輕量級的、基於靜態文件的 packagist 版本。你給它一個包含 composer.json 的存儲庫,定義好 VCS 和 資源庫。它會獲取所有你列出的包,並打印 packages.json 文件,作為 composer 類型的資源庫。
說明
服務器環境:
- centos7.2
- nginx
- php7.1
代碼管理平台:碼雲
文章中盡量以一個真實的情況來撰寫,但是文章的倉庫地址,網頁地址均是不可訪問的,用虛擬信息替換了真實信息。
Create Private Git Project
既然為公司內部項目源碼是不公開的,我們選擇了碼雲,未選擇 github,主要有兩個原因:
github private project 是收費的,對於一個公司來說費用不高,但是加上以上兩點原因后,所以未選擇。
假設我們已經在碼雲上建立好了私有項目,並且已經編寫好了所有的代碼和單元測試。
ssh 地址: git@gitee.com:iBrand/test-private-composer.git
composer.json
{
"name": "iBrand/test-private-composer", "type": "library", "description": "iBrand test private composer", "keywords": [ "iBrand crop", "private composer", ], "license": "MIT", "authors": [ { "name": "shjchen", "email": "ibrand.shjchen@foxmail.com" } ], "require": { "php": "^5.6|^7.0", }, "autoload": { "psr-4": { "iBrand\\Prviate\\Composer\\": "src/" } }, "minimum-stability": "dev", "prefer-stable": true }
Create Satis Server
Install
$ cd /data/www/
$ composer create-project composer/satis company-private-composer --stability=dev --keep-vcs
Setup
$ cd /data/www/company-private-composer $ vi satis.json
{
"name": "iBrand Private Composer Repository", "homepage": "http://packagist.iBrand.com", "repositories": [ { "type": "vcs", "url": "git@gitee.com:iBrand/test-private-composer.git" } // more vcs url. ], "require": { "ibrand/test-private-composer": "*", // you more package. }, "archive": { "directory": "dist", "format": "tar", "prefix-url": "http://packagist.iBrand.com" } }
解釋下 satis.json
配置文件
- name:倉庫名稱,可以隨意定義
- homepage:倉庫主頁地址
- repositories:指定包源
- require:指定包源版本,* 代碼編譯所有版本,如果想獲取所有包,使用 require-all: true,
- directory: required, the location of the dist files (inside the output-dir)
- format: optional, zip (default) or tar
- prefix-url: optional, location of the downloads, homepage (from satis.json) followed by directory by default
Authentication
在進行 Build
前,我們需要解決代碼權限問題,因為前面的項目源碼是私有的,所以服務器上需要有讀取源碼的權限,依然以碼雲舉例:
生成ssh公鑰
你可以按如下命令來生成 sshkey:
$ ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public/private rsa key pair... # 三次回車即可生成 ssh key
查看你的 public key,並把他添加到碼雲(Gitee.com) SSH key添加地址:https://gitee.com/profile/ssh...
$ cat ~/.ssh/id_rsa.pub # ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
Build
php bin/satis build satis.json public/
執行后會在 /data/www/company-private-composer/public
生成倉庫列表
Setup Nginx
上一步已經生成了倉庫列表,為了保證可訪問需要通過 nginx
or apache
把服務暴露出去,我們使用的是 nginx ,因此以 nginx
舉例。
server { listen 80; server_name packagist.iBrand.com; root /data/www/company-private-composer/public; index index.php index.html; access_log /data/logs/packages-access.log; error_log /data/logs/packages-error.log error; rewrite_log on; location ~* \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; } location = /favicon.ico { log_not_found off; access_log off; } }
配置好后需要執行 service nginx reload
,然后就可以通過訪問 http://packagist.iBrand.com
看到自己的倉庫列表,如下圖:
Usage
接下來就可以在項目中使用這個私有的 Composer 包倉庫。
添加如下配置到項目中的 composer.json
文件中
"require": { "iBrand/test-private-composer": "~1.0" } "config": { "preferred-install": "dist", "secure-http": false }, "repositories": [ { "type": "composer", "url": "http://packagist.iBrand.com/" } ]
待續
- 實現 webhooks 源碼更新時自動 Build