使用Docker代理了解Jenkins流水线步骤

8

我将尝试使用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。希望所有这些阶段都在同一上下文中执行。

1个回答

5
即使您在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'
      }
    }
  }
}

此外,您可以在管道作业的Pipeline Syntax部分中使用指令生成器来获取agent配置信息。
更多信息请参考:https://jenkins.io/doc/book/pipeline/syntax/#common-options

1
我认为这里的Jenkinsfile语法不正确:WorkflowScript: 2:每个代理部分只允许一种代理类型@第2行,第3列。 WorkflowScript: 2:未指定代理类型。必须是[any,docker,dockerfile,label,none]之一@第2行,第3列。 - David Witt
你是对的 @DavidWitt,我的错。我会编辑评论。 - Andrés Cidoncha Carballo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接