美文网首页
使用satis搭建私有仓库

使用satis搭建私有仓库

作者: 某不科学的XX | 来源:发表于2018-08-10 16:57 被阅读0次

    近期国内composer镜像维护,所以只能通过自己的代理才能正常的拉取代码。而公司里面也需要一个可以存放自己项目的静态代码库,所以就让我搭建一个satis作为简单的静态composer存储库。

    安装配置satis

    可以使用国内镜像也可以终端下设置代理的方法:

    // windows的cmd下
    set http_proxy=127.0.0.1:1080;
    set https_proxy=127.0.0.1:1080;
    // linux的bash下
    export http_proxy=127.0.0.1:1080;
    export https_proxy=127.0.0.1:1080;
    // 测试代理状态
    curl http://ip.cn;
    curl https://ip.cn;
    

    拉取项目

    composer create-project composer/satis --stability=dev --keep-vcs
    

    进入目录下新建配置satis.json文件

    {
      "name": "MY SATIS",//项目名称
      "homepage": "http://satis.example.work",//项目地址
      "repositories": [//指定获取包的仓库地址
        {
          "type": "vcs",
          "url": "git@gitlab.example.cn:xx/test.git"
        },
        {
          "type": "vcs",
          "url": "git@gitlab.example.cn:xx/weibo.git"
        },
       {
    // 不再拉取packagist.org上的包,可节省大量时间
         "packagist.org": false
        },
      ],
      "require": {//指定可以获取包的版本,存在"require-all": true设置时将从仓库获取所有相关的依赖包,所以一般不设置。使用"require-all": true时就不能再从packagist.org上拉取包了,不然satis将会把所有packagist.org上的包都拉取下来
        "xx/test": "*",//尝试从以上两个配置好的仓库与管理员的全局配置中的仓库中拉取
        "laravel/laravel": "5.6.21",//由于以上两个仓库中没有该项目,所以将从管理员的全局配置文件中使用的仓库地址中拉取
        "xx/weibo": "1.5.0|1.0.3"//"*"为全部版本,"1.5.0|1.0.3"为1.5.0与1.0.3两个版本
      },
      "archive": {//镜像缓存设置,该设置会缓存require配置项中各个仓库的代码
        "directory": "dist",//缓存目录名称
        "format": "tar",//缓存格式
        "prefix-url": "http://satis.example.work",//下载的前缀不要写成http://satis.example.work/不然链接将会出问题
        "skip-dev": false//是否跳过开发版本,一般为true,但我为了方便测试就选择了false
    // 其他配置项:absolute-directory: 绝对目录
    // whitelist: 白名单,只下载哪些
    // blacklist: 黑名单,不下载哪些
    // checksum: 可选,是否验证sha1
      },
      "config": {//拉取代码时的配置
        "secure-http":false,//由于公司部分项目的存放地址可能使用http链接,所以此处设置为false
        "github-oauth": {//部分放在github中的项目需要提前配置好token避免拉取时再输入
            "github.com": "83dxxxxxxxxxxx8888888800062"
        },
        "bitbucket-oauth": {},
        "gitlab-oauth": {},
        "gitlab-token": {
            "gitlab.example.cn":"zxxxxxxxxxxxQjZT"//公司gitlab的token配置
        },
        "http-basic": {}
      }
    }
    

    拉取代码生成项目

    //为了避免在拉取时反复输入密码建议先添加密钥
    //ssh-add ~/.ssh/id_rsa
    php bin/satis build satis.json public/
    //添加新的repo
    php bin/satis add git@gitlab.example.cn:xx/newProject.git satis.json
    

    此时已经配置完成satis,只需要再配置nginx服务就可以使用,配置完成后打开http://satis.example.work即可看见该仓库所存储的项目,如果不能访问需考虑修改host文件。

    使用私有composer镜像

    在新的目录下手动新建composer.json文件或使用composer init向导完成composer.json配置
    在使用composer require公司内部项目时注意关闭终端的代理,否则可能拉取失败
    如果使用的是http地址需要设置"secure-http":false
    http://satis.example.work主页有修改composer.json配置的提示

    {
      "repositories": [{//将该配置项添加进你的配置文件即可
        "type": "composer",
        "url": "http://satis.example.work"
      }]
    }
    

    不建议使用以下命令修改当前项目的仓库地址,因为执行该命令后会导致"repositories"配置项由数组变为对象,使得composer在拉取项目时只从http://satis.example.work中拉取代码,而这个仓库只有公司的代码,在获取其他开源代码时会报错提示该仓库中没有需要的代码。

    composer config repo.packagist composer http://satis.example.work
    

    相关参考

    satis地址:https://github.com/composer/satis
    satis配置英文文档:https://getcomposer.org/doc/articles/handling-private-packages-with-satis.md
    satis配置示例:https://laravel-china.org/topics/1900/use-satis-to-build-a-private-composer-warehouse
    composer配置文档:https://getcomposer.org/doc/06-config.md
    中国composer镜像网址:https://pkg.phpcomposer.com/

    相关文章

      网友评论

          本文标题:使用satis搭建私有仓库

          本文链接:https://www.haomeiwen.com/subject/smwkbftx.html