【问题标题】:Understanding Jenkins pipeline steps with docker agent使用 docker 代理了解 Jenkins 管道步骤
【发布时间】:2019-03-30 12:28:14
【问题描述】:

我正在尝试使用 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 构建脚本

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:

设置容器的第一步的输出

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、测试和部署。希望所有这些阶段都将在相同的上下文中执行。

【问题讨论】:

    标签: docker jenkins jenkins-plugins jenkins-pipeline


    【解决方案1】:

    即使您更改 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

    【讨论】:

    • 我认为这里的 Jenkinsfile 语法不正确:WorkflowScript:2:每个代理部分仅允许一种代理类型@第 2 行第 3 列。WorkflowScript:2:未指定代理类型。必须是 [any, docker, dockerfile, label, none] @ line 2, column 3 之一。
    • 你是对的@DavidWitt 我的错。我会编辑评论。
    最近更新 更多