gpt4 book ai didi

docker - 使用 docker 代理了解 Jenkins 管道步骤

转载 作者:行者123 更新时间:2023-12-04 21:03:30 25 4
gpt4 key购买 nike

我正在尝试使用 Jenkins Pipelines 设置自动构建器/部署器作业。

我已经设置了一个安装 NodeJS 和 Gulp 依赖项的 docker 容器:

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 构建脚本

Jenkins 文件
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

/

来自阶段编译静态 Assets 的输出
[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、测试和部署。希望所有这些阶段都将在相同的上下文中执行。

最佳答案

即使你改变了WORKDIR在 Dockerfile 中,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

关于docker - 使用 docker 代理了解 Jenkins 管道步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52996958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com