使用 Satis 搭建私有的 Composer 包倉庫

簡述

iBrand 產品立項時是商業性質的項目,可是在搭建架構時考慮後續的通用性,所以每一個模塊都設計成一個 Package,做爲公司內部用,所以這些包並不能提交到 packagist.org 上去。 因此就想是否可以搭建私有的包倉庫,實現一個私有的 packagist 。php

仔細翻閱 Composer 文檔,發現官方有相應的解決方案:Handling private packageshtml

這裏推薦使用 Satis ,也正是咱們目前使用的方案,目前運行一切良好。nginx

Satis 是一個靜態的 composer 資源庫生成器。它像是一個超輕量級的、基於靜態文件的 packagist 版本。

你給它一個包含 composer.json 的存儲庫,定義好 VCS 和 資源庫。它會獲取全部你列出的包,並打印 packages.json 文件,做爲 composer 類型的資源庫。git

說明

服務器環境github

  1. centos7.2
  2. nginx
  3. php7.1

代碼管理平臺:碼雲web

文章中儘可能以一個真實的狀況來撰寫,可是文章的倉庫地址,網頁地址均是不可訪問的,用虛擬信息替換了真實信息。sql

Create Private Git Project

既然爲公司內部項目源碼是不公開的,咱們選擇了碼雲,未選擇 github,主要有兩個緣由:typescript

  1. github 由於是國外服務器,國內偶爾會抽風。
  2. 國內也有比較優秀的 git 代碼託管平臺,免費支持 Private Project。好比:碼雲Coding

github private project 是收費的,對於一個公司來講費用不高,可是加上以上兩點緣由後,因此未選擇。shell

假設咱們已經在碼雲上創建好了私有項目,而且已經編寫好了全部的代碼和單元測試。apache

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 生成倉庫列表

clipboard.png

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 看到本身的倉庫列表,以下圖:

clipboard.png

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/" } ]

待續

  1. 實現 webhooks 源碼更新時自動 Build

參考資料

  1. Handling private packages
  2. Hosting your own package
  3. 使用私有資源庫

討論交流

聯繫咱們

相關文章
相關標籤/搜索