使用Docker Agent了解Jenkins管道步骤

Dav*_*itt 5 jenkins jenkins-plugins docker jenkins-pipeline

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

我已经安装了一个Docker容器来安装NodeJS和Gulp依赖项:

Docker文件

# 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
Run Code Online (Sandbox Code Playgroud)

Dockerfile的目标是缓存依赖项的安装,以便构建作业可以更快地运行。

Jenkinsfile使用Dockerfile,然后尝试运行npm构建脚本

詹金斯档案

pipeline {
  agent {
    dockerfile true
  }
  stages {
    stage('Compile static assets') {
      steps {
        sh 'node --version'
        sh 'npm --version'
        sh 'pwd'
        sh 'ls'
        sh 'npm run build'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

运行Jenkins Pipeline时,初始化(使用并运行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

/
Run Code Online (Sandbox Code Playgroud)

阶段编译静态资产的输出

[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
Run Code Online (Sandbox Code Playgroud)

因此,关于执行上下文似乎有些不清楚。我的假设是,一旦初始化Docker容器,整个管道就会在该现有上下文中执行。事实并非如此。

目前,我只有一个阶段,但最终将有多个阶段,可以进行lint,测试和部署。希望所有这些阶段都将在相同的上下文中执行。

And*_*llo 5

即使您更改WORKDIRDockerfile 中的 ,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'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

此外,您还可以使用管道作业的管道语法部分中的指令生成器来获取配置agent

更多信息: https: //jenkins.io/doc/book/pipeline/syntax/#common-options