我处理了几个托管在GitHub上的Symfony捆绑包,并使用Travis CI自动测试。
测试中最长的部分是 Composer 安装需求。
我配置 Travis CI 以安装带有 composer update --prefer-dist
的软件包并缓存$HOME/.composer/cache
目录。由于缓存,测试总共花费了 18 分钟的时间:
安装 doctrine/词法分析器 (v1.0.1(
从缓存加载
但是一周前,我看到了来自作曲家的消息:
安装 doctrine/词法分析器 (v1.0.1(
正在下载:正在连接... 无法从 dist 下载原则/词法分析器:无法针对 github.com 进行身份验证
因此,我将配置更改为composer update --prefer-source
。这似乎是Symfony捆绑包中的常见做法。测试套件花了 28 分钟。
我知道我可以在 Travis CI 中注册 GitHub 密钥,以便在使用 Composer --prefer-dist
选项时避免 API 限制。
它们是缓存依赖项的其他方法吗?例如,通过在缓存中克隆依赖项存储库?
GitHub 已经删除了 API 速率限制,composer
现在可以与 --prefer-dist
一起使用,并且可以缓存 zip 文件。以下是.travis.yml
中的配置示例:
cache:
directories:
- $HOME/.composer/cache
# …
install:
- composer update --prefer-dist
以下是公告:
嗨尼尔斯和乔迪,
我们花了一些时间深入研究,考虑解决您的问题的所有选项,权衡您的需求与基础架构压力和可用性。
我很高兴地报告,我们的基础设施团队认为,由于自引入这些 API 以来他们在 Git 后端的工作,我们现在能够在 API 端降低这些速率限制。我们在几个小时前部署了这些更改。通过 API 获取存档链接 1 将不再计入每小时速率限制(无论是否经过身份验证(。这应该会让作曲家安装满意。
如果您看到任何有趣的业务,请告诉我们。
干杯
永利荷兰
平台工程经理,GitHub
源。
我测试了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 秒,而不是没有缓存大约 2 分钟(请注意,比较并不完美,应用了一些其他更改,但这仍然是一个很好的估计(。
最好在首次启动生成时限制并行生成的数量,这样缓存就不会受到争用条件的影响。