首页 文章

在Travis CI(以及其他CI工具)上使用Composer安装的git存储库缓存?

提问于
浏览
1

我在GitHub上托管了几个Symfony包,并使用Travis CI自动测试 .

测试中最长的部分是Composer安装的要求 .

configured Travis CIcomposer update --prefer-dist 安装软件包并缓存 $HOME/.composer/cache 目录 . 由于缓存,测试总时间为18 minutes

安装doctrine / lexer(v1.0.1)从缓存加载

但是一周前我看到了Composer的一条消息:

安装doctrine / lexer(v1.0.1)正在下载:正在连接...无法从dist下载doctrine / lexer:无法对github.com进行身份验证

因为这个,我changed the configurationcomposer update --prefer-source . 这似乎是跨Symfony捆绑包的常见做法 . 测试套件采用28 minutes .

我知道我可以在Travis CI中注册GitHub密钥,以避免在使用Composer --prefer-dist 选项时出现API限制 .

他们是否有其他一些缓存依赖关系的方法?例如,通过克隆缓存中的依赖存储库?

2 回答

  • 1

    GitHub已经删除了API速率限制, composer 现在可以与 --prefer-dist 一起使用,并且可以缓存zip文件 . 以下是 .travis.yml 中的配置示例:

    cache:
      directories:
        - $HOME/.composer/cache
    
    # …
    
    install:
      - composer update --prefer-dist
    

    这是宣布:

    嗨Niels和Jordi,我们花了一些时间深入研究,考虑解决问题的所有选项,根据基础设施的压力和可用性来衡量您的需求 . 我很高兴地报告我们的基础架构团队认为,由于他们在引入这些API后我们的Git后端工作,我们现在能够在API方面降低这些速率限制 . 我们在几个小时前部署了这些更改 . 通过API获取存档链接1将不再计入您的每小时费率限制(已验证或未通过验证) . 这应该让Composer安装得很开心 . 如果您看到任何有趣的业务,请告诉我们 . 干杯,Wynn Netherland平台工程经理,GitHub

    Source .

  • 0

    我测试了 vendor/ 目录的缓存并且它有效 . 我使用 tar 来创建未压缩的存档 $HOME/vendor-cache/ 并为此目录配置了Travis CI .

    命令有两个目标:

    • 如果缓存可用,则从缓存中提取 vendor/

    • 在测试后将 vendor/ 放入缓存中

    这是一个示例 .travis.yml 文件:

    sudo: false
    
    language: php
    
    cache:
      directories:
        - $HOME/.composer/cache
        # This is where vendor/ backups will be stored
        - $HOME/vendor-cache/
    
    php:
      - […]
    
    env:
      - SYMFONY_VERSION="2.7.*"
      - SYMFONY_VERSION="2.8.*"
      - SYMFONY_VERSION="3.0.*"
    
    before_install:
     # Create an hash corresponding to the PHP version and the dependencies
     - tohash="${SYMFONY_VERSION}"
     - cachefile="`echo -n "$tohash" | sha1sum | cut -d " " -f 1`.tar"
     # Extract cache archive if the file exists
     - if [[ -f $HOME/vendor-cache/$cachefile ]]; then tar -xf $HOME/vendor-cache/$cachefile ; fi
    
    install:
      - composer self-update
      - composer update --profile --no-progress
    
    script: php ./vendor/bin/phpunit
    
    # Create cache archive from vendor/ directory
    before_cache:
     - if [[ -f $HOME/vendor-cache/$cachefile ]]; rm -fv $HOME/vendor-cache/$cachefile ; fi
     - tar -cf $HOME/vendor-cache/$cachefile vendor/
    

    这是一个带有更详细输出的完全带注释的 .travis.yml 文件:

    sudo: false
    
    language: php
    
    cache:
      directories:
        - $HOME/.composer/cache
        # This is where vendor/ backups will be stored
        - $HOME/vendor-cache/
    
    git:
      depth: 5
    
    php:
      - […]
    
    env:
      - SYMFONY_VERSION="2.7.*"
      - SYMFONY_VERSION="2.8.*"
      - SYMFONY_VERSION="3.0.*"
    
    before_install:
     # Create an hash corresponding to the PHP version and the dependencies
     - echo "Vendor cache content:" ; ls -lh $HOME/vendor-cache/
     - echo "Values used for hash:"
     - tohash="${SYMFONY_VERSION}"
     - echo "$tohash"
     - cachefile="`echo -n "$tohash" | sha1sum | cut -d " " -f 1`.tar"
     - echo "cachefile = ${cachefile}"
     # Extract cache archive if the file exists
     - if [[ -f $HOME/vendor-cache/$cachefile ]]; then echo "Extract cache archive"; tar -xf $HOME/vendor-cache/$cachefile ; echo "Done" ; else echo "Cache archive does not exist" ; fi
     - if [[ -d vendor/ ]]; then echo "Size of vendor directory extracted from cache:" ; du -hs vendor/; else echo "vendor/ directory does not exist"; fi
    
    install:
      - composer self-update
      - composer update --profile --no-progress
    
    script: php ./vendor/bin/phpunit
    
    # Create cache archive from vendor/ directory
    before_cache:
     - if [[ -f $HOME/vendor-cache/$cachefile ]]; then echo "Delete previous cache archive"; rm -fv $HOME/vendor-cache/$cachefile ; echo "Done" ; else echo "No cache archive to delete" ; fi
     - echo "Create cache archive" ; tar -cf $HOME/vendor-cache/$cachefile vendor/ ; echo "Done"
     - echo "Size of cache archive:" ; ls -lh $HOME/vendor-cache/$cachefile
    

    通过使用此方法, composer update 取代30 seconds,而不是没有缓存的about 2 minutes(注意比较不完美,应用了一些其他更改,但这仍然是一个很好的估计) .

    最好在第一次启动构建时限制并行构建的数量,这样缓存就不会受到竞争条件的影响 .

相关问题