[英]Understanding Jenkins pipeline steps with docker agent
我正在嘗試使用Jenkins Pipelines設置自動化構建器/部署程序。
我已經設置了一個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管道時,初始化(Dockerfile的使用和運行)似乎與步驟不匹配。 見每個的pwd和ls:
設置Container的第一步輸出
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,測試和部署。 希望所有這些階段都將在相同的環境中執行。
即使您更改了Dockerfile中的WORKDIR
,Jenkins也會在容器內部使用已分配的工作區,就像在普通代理中運行構建一樣。
您可以使用代理定義中的customWorkspace
選項來更改:
pipeline {
agent {
dockerfile {
customWorkspace '/test'
filename 'Dockerfile'
}
}
stages {
stage('Compile static assets') {
steps {
sh 'node --version'
sh 'npm --version'
sh 'pwd'
sh 'ls'
sh 'npm run build'
}
}
}
}
此外,您可以使用管道作業的“管道語法”部分中的“指令生成器”來獲取agent
配置。
更多信息: https : //jenkins.io/doc/book/pipeline/syntax/#common-options
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.