文档

构建加速

在持续集成的代码构建中的耗时通常集中在2个部分,首先是从外部获取构建所需的依赖以及代码本身的编译行为。通过使用Flow的自定义缓存能够我们有效降低获取依赖时的耗时。本文将介绍如何加速常见的编译工具的构建效率。

Yarn构建加速

使用中国内地registry和mirror

使用中国内地registry仓库避免由于海外网络访问导致的依赖下载慢的问题

yarn config set registry https://npmmirror.com

对于构建中由于Building fresh packages导致构建慢的情况需要指定特定外部依赖的中国内地镜像源:

yarn config set sass_binary_site "https://npmmirror.com/mirrors/node-sass/"

除了通过yarn config set指定依赖下载路径以外,还可以通过.yarnrc进行配置。在项目根路径中创建.yarnrc并配置以下内容:

registry "https://registry.npmmirror.com"
sass_binary_site "https://npmmirror.com/mirrors/node-sass/"
phantomjs_cdnurl "https://cdn.npmmirror.com/binaries/phantomjs"
electron_mirror "https://cdn.npmmirror.com/binaries/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
chromedriver_cdnurl "https://cdn.npmmirror.com/binaries/chromedriver"

配置yarn全局缓存

设置缓存目录,并在流水线自定义缓存中添加缓存目录/root/.yarn配置

# 设置全局缓存
yarn config set cache-folder ~/.yarn
# 构建中优先使用缓存中
yarn install --prefer-offline

通过以上命令设置缓存目录,并确保下载依赖过程中优先使用本地缓存,可以最大限度减少由于外部网络导致的依赖下载慢的问题。

Npm构建加速

使用中国内地registry和mirror

说明

或者直接使用cnpm

对于使用npm的用户可以创建.npmrc并设置以下内容:

registry="https://registry.npmmirror.com"
sass_binary_site="https://npmmirror.com/mirrors/node-sass/"
phantomjs_cdnurl="https://cdn.npmmirror.com/binaries/phantomjs"
electron_mirror="https://cdn.npmmirror.com/binaries/electron/"
sqlite3_binary_host_mirror="https://foxgis.oss-cn-shanghai.aliyuncs.com/"
chromedriver_cdnurl="https://cdn.npmmirror.com/binaries/chromedriver"

构建中使用全局缓存

构建时,使用一下命令设置npm的全局缓存路径,并在流水线自定义缓存配置中添加/root/.npm缓存路径,并使用以下命令安装依赖包,优先从本地缓存获取依赖包:

npm config set cache ~/.npm
npm install --prefer-offline --no-audit

Golang构建加速

使用go proxy

使用Goproxy从中国内地下载外部依赖:

export GOPROXY=https://goproxy.cn

添加go mod缓存

持久化Go Mod缓存,Flow中Go构建环境的默认缓存目录是/go/pkg/mod。用户需要在流水线自定义缓存中添加该缓存路径即可。 在构建过程中将会优先使用/go/pkg/mod中的本地缓存,从而减少由于外部网络请求导致的构建慢的问题。

Python构建加速

使用pypi镜像

使用阿里云pypi镜像

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

添加pip依赖缓存

缓存pip本地依赖,流水线自定义缓存配置中添加缓存目录/root/.cache/pip,确保pip install时优先从版本缓存中获取依赖包

镜像构建加速

如果你的Dockerfile中FROM了dockerhub的镜像或者其他海外镜像,比如:

FROM nginx:1.19.1
.....

由于跨境网络问题,这种Dockerfile的构建会不稳定或者比较慢,可以使用以下方式解决:将境外镜像pull到本地,然后push到阿里云镜像仓库(cr.console.aliyun.com)的中国内地region(比如北京、上海等),然后修改你的Dockerfile中的FROM。比如:

docker pull nginx:1.19.1
docker tag nginx:1.19.1
 registry.cn-beijing.aliyuncs.com/yournamespace/nginx:1.19.1
docker push registry.cn-beijing.aliyuncs.com/yournamespace/nginx:1.19.1

  • 本页导读 (0)
文档反馈