UPDATE
我改变了这个问题的方向,最后利用Docker图像层来缓存npm安装,除非package.config,see here发生了变化。
注意,关于这个问题,我仍然在奴隶Jenkins Docker镜像中构建我的AngularJs Docker镜像,但我不再在Docker slave中运行npm install,我将我的app文件复制到我的AngularJs Docker镜像并运行npm install in AngularJs Docker镜像,从而获得了npm安装的Docker缓存层,灵感来自这个伟大的idea/answer here。
------------------------------- END UPDATE ----------------- -------------
好吧,我应该添加一个警告,我在Docker容器中,但真的应该没关系,我不会停止容器,我有npm缓存文件夹的卷和/ home文件夹用户运行npm命令。
安装了npm的Docker容器的目的是它是一个构建从属,由Jenkins开始构建一个AngularJs应用程序。问题是,每次下载所有必需的npm软件包都非常慢。
jenkins
是用户,构建服务器上的jenkins帐户是“谁”正在运行npm install
我有运行npm install
cmd的用户的npm文件夹的卷:/home/jenkins/.npm
以及命令npm config get cache
所说的文件夹是我的缓存目录:/root/.npm
。不是容器卷应该重要,因为我没有在运行npm install
后停止容器。
好的,我开始调试的步骤,开始,我用这个命令“猛烈闯入容器”:
docker exec -it <container_id> bash
我从这一点开始运行的所有命令我已经安装了npm连接到正在运行的容器。
echo "$HOME"
导致/root
npm config get cache
导致root/.npm
任何时候jenkins
在这个容器中运行npm install
,在该命令成功完成后,我运行npm cache ls
,它总是产生空,没有缓存:~/.npm
然而,我们可以通过ls -a /home/jenkins/.npm/
:看到许多软件包
所以我尝试将cache-min设置为很长的到期时间:npm config set cache-min 9999999
没有帮助。
我不知道还能做什么,似乎没有我的npm软件包被缓存,我如何让npm缓存软件包?
这是一个截断的npm安装输出:
Downloading binary from https://github.com/sass/node-sass/releases/download/v4.5.3/linux-x64-48_binding.node
Download complete
Binary saved to /home/jenkins/workspace/tsl.frontend.development/node_modules/node-sass/vendor/linux-x64-48/binding.node
Caching binary to /home/jenkins/.npm/node-sass/4.5.3/linux-x64-48_binding.node
Binary found at /home/jenkins/workspace/tsl.frontend.development/node_modules/node-sass/vendor/linux-x64-48/binding.node
Testing binary
Binary is fine
typings WARN deprecated 3/24/2017: "registry:dt/core-js#0.9.7+20161130133742" is deprecated (updated, replaced or removed)
[?25h
+-- app (global)
`-- core-js (global)
这是我的Dockerfile:
FROM centos:7
MAINTAINER Brian Ogden
RUN yum update -y && \
yum clean all
#############################################
# Jenkins Slave setup
#############################################
RUN yum install -y \
git \
openssh-server \
java-1.8.0-openjdk \
sudo \
make && \
yum clean all
# gen dummy keys, centos doesn't autogen them like ubuntu does
RUN /usr/bin/ssh-keygen -A
# Set SSH Configuration to allow remote logins without /proc write access
RUN sed -ri 's/^session\s+required\s+pam_loginuid.so$/session optional pam_loginuid.so/' /etc/pam.d/sshd
# Create Jenkins User
RUN useradd jenkins -m -s /bin/bash
# Add public key for Jenkins login
RUN mkdir /home/jenkins/.ssh
COPY /files/id_rsa.pub /home/jenkins/.ssh/authorized_keys
#setup permissions for the new folders and files
RUN chown -R jenkins /home/jenkins
RUN chgrp -R jenkins /home/jenkins
RUN chmod 600 /home/jenkins/.ssh/authorized_keys
RUN chmod 700 /home/jenkins/.ssh
# Add the jenkins user to sudoers
RUN echo "jenkins ALL=(ALL) ALL" >> etc/sudoers
#############################################
# Expose SSH port and run SSHD
EXPOSE 22
#Technically, the Docker Plugin enforces this call when it starts containers by overriding the entry command.
#I place this here because I want this build slave to run locally as it would if it was started in the build farm.
CMD ["/usr/sbin/sshd","-D"]
#############################################
# Docker and Docker Compose Install
#############################################
#install required packages
RUN yum install -y \
yum-utils \
device-mapper-persistent-data \
lvm2 \
curl && \
yum clean all
#add Docker CE stable repository
RUN yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#Update the yum package index.
RUN yum makecache fast
#install Docker CE
RUN yum install -y docker-ce-17.06.0.ce-1.el7.centos
#install Docker Compose 1.14.0
#download Docker Compose binary from github repo
RUN curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#Apply executable permissions to the binary
RUN chmod +x /usr/local/bin/docker-compose
#############################################
ENV NODE_VERSION 6.11.1
#############################################
# NodeJs Install
#############################################
RUN yum install -y \
wget
#Download NodeJs package
RUN wget https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
#extract the binary package into our system's local package hierarchy with the tar command.
#The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option.
#We will specify the target directory of our command with the -C command:
#This will install all of the components within the /usr/local branch
RUN tar --strip-components 1 -xzvf node-v* -C /usr/local
#############################################
#############################################
# npm -setup volume for package cache
# this will speed up builds
#############################################
RUN mkdir /home/jenkins/.npm
RUN chown jenkins /home/jenkins/.npm .
RUN mkdir /root/.npm
RUN chown jenkins /root/.npm .
#for npm cache, this cannot be expressed in docker-compose.yml
#the reason for this is that Jenkins spins up slave containers using
#the docker plugin, this means that there
VOLUME /home/jenkins/.npm
VOLUME /root/.npm
#############################################
当您运行docker exec -it <container> bash
时,您将以root
用户身份访问Docker容器。 npm install
因此将缓存保存到/root/.npm
,这不是容器保存的卷。另一方面,Jenkins使用jenkins
用户,该用户保存到缓存的/home/jenkins/.npm
。因此,为了模拟实际Jenkins工作流的功能,您需要在su jenkins
之前使用npm install
。
话虽这么说,npm缓存并不是一个完美的解决方案(特别是如果你有大量的自动Jenkins构建)。要研究的一些事情是更好的长期解决方案:
sinopia
这样的本地NPM缓存。我发现this guide特别有帮助。