一、问题出现原因
使用gitlab-runner部署前端js项目时,下载chromedriver@2.46.0
有时候相当的慢。
主要原因是npm install老去国外github下载
> chromedriver@2.45.0 install /app/node_modules/chromedriver
> node install.js
Current existing ChromeDriver binary is unavailable, proceding with download and extraction.
Downloading from file: https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
Saving to file: /app/node_modules/chromedriver/chromedriver/chromedriver_linux64.zip
...
后来去放置gitlab-runner那台主机wget一下,结果一直响应中
cuiyf@gitlabrunner:~$ wget https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip
--2019-08-05 17:46:32-- https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 216.58.200.48, 2404:6800:4008:801::2010
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|216.58.200.48|:443... connected.
HTTP request sent, awaiting response...
二、解决思路
设法使其去淘宝镜像源地址下载
https://registry.npm.taobao.org/
三、解决方案
由于我是用docker部署的,修改Dockerfile添加如下配置
npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver
FROM cuiyf/node:8.12.0-alpine as build-stage
COPY . /app
WORKDIR /app
RUN npm config set registry http://172.18.0.1:9000/repository/node-public \
&& npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver\
&& npm install
RUN npm run build
FROM cuiyf/nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
最终效果,快速的去淘宝源下载去了
> chromedriver@2.46.0 install /app/node_modules/chromedriver
> node install.js
Current existing ChromeDriver binary is unavailable, proceding with download and extraction.
Downloading from file: http://cdn.npm.taobao.org/dist/chromedriver/2.46/chromedriver_linux64.zip
Saving to file: /app/node_modules/chromedriver/2.46/chromedriver/chromedriver_linux64.zip
Received 808K...
Received 1652K...
Received 2459K...
Received 3258K...
Received 4092K...
Received 4922K...
Received 5277K total.
Extracting zip contents
Copying to target path /app/node_modules/chromedriver/lib/chromedriver
Fixing file permissions
Done. ChromeDriver binary available at /app/node_modules/chromedriver/lib/chromedriver/chromedriver
四、终极方案
新建一个镜像吧,包括下载node-sass
、phantomjs
、electron
等
FROM node:8.12.0-alpine
LABEL QianJia.cuiyufeng <1627999813@qq.com>
ENV SASS_BINARY_SITE=http://172.17.3.95:9000/repository/node-saas/ \
PHANTOMJS_CDNURL=http://172.17.3.95:9000/repository/phantomjs/ \
ELECTRON_MIRROR=http://172.17.3.95:9000/repository/electron/ \
CHROMEDRIVER_CDNURL=http://172.17.3.95:9000/repository/chromedriver/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add --no-cache tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache