我将尝试使用Jenkins Pipeline设置自动化的构建/部署作业。
我已经设置了一个Docker容器,用于安装NodeJS和Gulp依赖项:
Dockerfile
# Node 8.9 running on lean version of alpine Linux
FROM node:8.9-alpine
# Commented out setting production since
# we need devDependencies to build
# ENV NODE_ENV production
# Set working directory to root of container
WORKDIR /
# Copy package.json and install dependencies first
# These can be cached as a step so long as they haven't changed
COPY ["./package.json", "./package-lock.json*", "/"]
RUN npm install --no-audit --silent
RUN npm install node-sass --silent
RUN npm install gulp-cli --silent
RUN npm install gulp@3.9 --silent
# Copy code to root, thise is a separate step from
# dependencies as this step will not be cached since code
# will always be different
COPY . .
# Debugging information
RUN ls
RUN pwd
RUN ./node_modules/.bin/gulp --version
Dockerfile 的目的是为了缓存依赖项的安装,以便构建作业可以更快地运行。
Jenkinsfile 使用 Dockerfile,然后尝试运行一个 npm 构建脚本。 Jenkinsfile
pipeline {
agent {
dockerfile true
}
stages {
stage('Compile static assets') {
steps {
sh 'node --version'
sh 'npm --version'
sh 'pwd'
sh 'ls'
sh 'npm run build'
}
}
}
}
当运行Jenkins Pipeline时,初始化(其中使用Dockerfile并运行)似乎与步骤不匹配。 请参见每个 pwd 和 ls 的输出:
第一步设置容器输出:
Step 9/10 : RUN ls
---> Running in 74b7483a2467
AO18_core
Jenkinsfile
bin
dev
dev_testing
etc
gulpfile.js
home
lib
media
mnt
node_modules
opt
package-lock.json
package.json
proc
root
run
sbin
srv
sys
tmp
usr
var
Removing intermediate container 74b7483a2467
---> e68a07c2bb45
Step 10/10 : RUN pwd
---> Running in 60a3a09573bc
/
从“编译静态资源”阶段输出
[ao_test-jenkins-YCGQYCUVORUBPWSQX4EDIRIKDJ72CXV3G5KXEDIGIY6BIVFNNVWQ] Running shell script
+ pwd
/var/lib/jenkins/workspace/ao_test-jenkins-YCGQYCUVORUBPWSQX4EDIRIKDJ72CXV3G5KXEDIGIY6BIVFNNVWQ
[ao_test-jenkins-YCGQYCUVORUBPWSQX4EDIRIKDJ72CXV3G5KXEDIGIY6BIVFNNVWQ] Running shell script
+ ls
AO18_core
Dockerfile
Jenkinsfile
README.md
dev_testing
docker-compose.debug.yml
docker-compose.yml
gulpfile.js
jsconfig.json
package.json
看起来有关执行上下文的某些内容我并不清楚。我的假设是一旦初始化docker容器,整个流水线将在该现有上下文中执行。但事实似乎并非如此。
目前我只有一个阶段,但最终会有多个阶段用于linting、testing和deployment。希望所有这些阶段都在同一上下文中执行。